This content originally appeared on Level Up Coding – Medium and was authored by Chris Bao
Discover the Power of AI Agents

Introduction
In this short article, we will analyze how to develop a data analysis Agent based on the Azure AI Agent Service. If you don’t have a deep understanding of Agents yet, I believe the results of this demo will definitely amaze you with the capabilities of AI Agents!
AI Agents = Tools + LLM + Feedback Loops
Before getting started with building an Agent, I think it’s necessary to introduce some theory: how AI agents came about and what exactly they can do.
To clarify this, I found a great in-depth paper titled AI Agents vs. Agentic AI: A Conceptual Taxonomy, Applications and Challenges.
This paper actually explores a more complex question — the difference between AI Agents and Agentic AI. But in the beginning, it provides a very deep analysis of the background of AI Agents, which impressed me a lot:
Large language models (LLMs) are excellent at generating content, but they have a major limitation: they cannot autonomously interact with the external environment or use digital tools.
For example, they cannot browse the internet for information, handle real-time data, or directly interact with various APIs — they rely on humans to build auxiliary tools.
So, they cannot yet be considered true AI agents. A true AI agent is a system that integrates perception, decision-making, and the use of external tools into a closed-loop feedback system.
In short, this can be expressed by a simple formula:
AI Agents = Tools + LLM + Feedback Loops
Below, we will develop a data analysis Agent based on the Azure AI Agent Service platform, to give everyone an initial experience of the power of AI Agents.
Azure AI Agent Service
Our solution will be built on the Azure AI Foundry Agent Service, involving the following core components:
- Azure AI Foundry Agent Service: Hosts the LLM-powered agent, manages the agent’s lifecycle, and provides the execution context for tools like the Code Interpreter.
- Azure AI Foundry Model Deployment: The LLM that powers the agent’s natural language reasoning. In this article, we use gpt-4o.
- Code Interpreter Tool: Executes Python code within the agent session for data analysis, chart generation, and custom logic execution.
You can think of Azure AI Foundry Service as a factory on the Azure platform for developing AI Agents or LLM-powered applications. It is a very large topic and technical ecosystem, and this article only covers a part of it. However, it is a great start, and I will write more articles discussing it later.
For how to create an Azure AI Foundry project and deploy a gpt-4o model on that project, please refer to this link (to save space, I won’t elaborate here):
https://microsoftlearning.github.io/mslearn-ai-agents/Instructions/02-build-ai-agent.html
Code: Azure AI Agent app with Python
To develop an Azure AI Agent app using Python, the following environment is required:
- Python 3.12
- Library: azure-ai-projects (version 1.0.0) or azure-ai-agents (version 1.1.0)
Based on the above configuration, I can develop an AI Agent app with only about 100 lines of code
import os
from dotenv import load_dotenv
from typing import Any
from pathlib import Path
from azure.identity import DefaultAzureCredential
from azure.ai.agents import AgentsClient
from azure.ai.agents.models import FilePurpose, CodeInterpreterTool, ListSortOrder, MessageRole
def main():
os.system('cls' if os.name=='nt' else 'clear')
load_dotenv()
project_endpoint= os.getenv("PROJECT_ENDPOINT")
model_deployment = os.getenv("MODEL_DEPLOYMENT_NAME")
script_dir = Path(__file__).parent
file_path = script_dir / 'Premier_League_2024_25_Table.csv'
with file_path.open('r') as file:
data = file.read() + "\n"
print(data)
# Connect to the Agent client
agent_client = AgentsClient(
endpoint=project_endpoint,
credential=DefaultAzureCredential
(exclude_environment_credential=True,
exclude_managed_identity_credential=True)
)
with agent_client:
# Upload the data file and create a CodeInterpreterTool
file = agent_client.files.upload_and_poll(
file_path=file_path, purpose=FilePurpose.AGENTS
)
print(f"Uploaded {file.filename}")
code_interpreter = CodeInterpreterTool(file_ids=[file.id])
# Define an agent that uses the CodeInterpreterTool
agent = agent_client.create_agent(
model=model_deployment,
name="data-agent",
instructions="You are an AI agent that analyzes the data in the file that has been uploaded. Use Python to calculate statistical metrics as necessary.",
tools=code_interpreter.definitions,
tool_resources=code_interpreter.resources,
)
print(f"Using agent: {agent.name}")
# Create a thread for the conversation
thread = agent_client.threads.create()
# Loop until the user types 'quit'
while True:
# Get input text
user_prompt = input("Enter a prompt (or type 'quit' to exit): ")
if user_prompt.lower() == "quit":
break
if len(user_prompt) == 0:
print("Please enter a prompt.")
continue
# Send a prompt to the agent
message = agent_client.messages.create(
thread_id=thread.id,
role="user",
content=user_prompt,
)
run = agent_client.runs.create_and_process(thread_id=thread.id, agent_id=agent.id)
# Check the run status for failures
if run.status == "failed":
print(f"Run failed: {run.last_error}")
# Show the latest response from the agent
last_msg = agent_client.messages.get_last_message_text_by_role(
thread_id=thread.id,
role=MessageRole.AGENT,
)
if last_msg:
print(f"Last Message: {last_msg.text.value}")
# Get the conversation history
print("\nConversation Log:\n")
messages = agent_client.messages.list(thread_id=thread.id, order=ListSortOrder.ASCENDING)
for message in messages:
if message.text_messages:
last_msg = message.text_messages[-1]
print(f"{message.role}: {last_msg.text.value}\n")
if __name__ == '__main__':
main()
The above code roughly accomplishes the following tasks:
- Connects to the AI Foundry project using the project endpoint.
- Uploads the data file and creates a code interpreter tool that can access it. In this example, we uploaded a file: Premier_League_2024_25_Table.csv. Its content is the Premier League standings for the 2024 season. Later, we want the Agent to perform some data analysis on this standings table.

- Creates a new agent that uses the code interpreter tool and has explicit instructions to use Python as necessary for statistical analysis. This system prompt is very important, and I will verify it later.
- Runs a thread with a prompt message from the user, along with the data to be analyzed. Within the scope of Azure AI Foundry, threads are conversation sessions between an agent and a user. They store messages and automatically handle truncation to fit content into the model’s context.
- Checks the status of the run in case there’s a failure. In the Azure AI Foundry context, a run involves invoking the agent on the thread, where it processes the messages in the thread and may append new messages (responses from the agent). The agent uses its configuration and the thread’s messages to perform tasks by calling models and tools.
- Retrieves the messages from the completed thread and displays the last one sent by the agent.
Demo
Like all applications, the Azure AI Agent App also requires authentication. Here, a solution based on Azure Entra ID token is used:
- Run the az login command through the Azure CLI command-line tool to authenticate using your Azure account.
- After successful authentication, the Azure CLI stores these tokens locally on your machine, typically in a secure token cache directory.
- The Azure Identity library, such as the DefaultAzureCredential object used in the code above, can use this token to communicate with the Azure AI service.
Once the Agent program starts, you will see the Agent waiting for your prompt!

Let’s try two questions
- Calculate win, draw, loss percentages per team.

- Analyze how points are spread across teams and draw a text-based chart.

We got the desired results, but more importantly, how did the Agent do this? If you try the same prompts on the Azure AI Foundry Playground with the Agent, you can find the real secret. You will see that it first generates Python code, then uses the AI Agent’s code interpreter to execute the code, and finally obtains the corresponding data and visualization.

For example, the code for the above two questions is respectively:
# Calculate win, draw, and loss percentages
data['Win %'] = (data['Wins'] / data['Played']) * 100
data['Draw %'] = (data['Draws'] / data['Played']) * 100
data['Loss %'] = (data['Losses'] / data['Played']) * 100
# Select relevant columns to show
percentages = data[['Team', 'Win %', 'Draw %', 'Loss %']]
# Extract team names and points for analysis
points_distribution = data[['Team', 'Points']]
# Generate a text-based chart for the points distribution
chart = ""
scale_factor = 2 # Points scale factor for visualization
for index, row in points_distribution.iterrows():
chart += f"{row['Team']:25} | {'#' * int(row['Points'] / scale_factor)} ({row['Points']})\n"
chart
Summary
When I ran this AI Agent for the first time, I was deeply impressed because I realized it greatly expands the capabilities of large language models — or rather, it extends the language analysis and understanding ability of large models into actual task execution capabilities.
Take data analysis as an example. Traditionally, if you want to perform data analysis, a data analyst would need to manually write scripts, or software engineers would need to develop specialized software with predefined functions for data analysts to use as needed. This traditional workflow and model have now been disrupted by the capabilities of AI Agents.
Taking this a step further, the software and internet era have digitized the entire world, and the foundation of this digitalization is code. Now that large models can generate code and Agents can run code, it means the tools and software of the entire digital world can be utilized by Agents. This truly is a revolution.
Build a data analysis AI Agent with Azure AI Foundry was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding – Medium and was authored by Chris Bao