Building AI Agents with Azure AI Foundry
Azure AI Foundry Agent Service allows you to build autonomous AI agents that can reason, plan, and execute multi-step tasks using tools, functions, and grounded data.
What Makes an AI Agent Different from a Chatbot?
| Feature | Chatbot | AI Agent |
|---|---|---|
| Conversation | Responds to prompts | Plans and executes autonomously |
| Tools | None | Code interpreter, file search, custom functions |
| Memory | Session-based | Persistent threads with history |
| Data | Static training data | Grounded with your real-time data |
Prerequisites
- Azure AI Foundry project (see the Getting Started tutorial)
- An Azure OpenAI GPT-4o deployment
- (Optional) Azure AI Search for grounding
Step 1: Create an Agent
from azure.ai.projects import AIProjectClient
from azure.identity import DefaultAzureCredential
# Connect to your project
client = AIProjectClient.from_connection_string(
conn_str="your-connection-string",
credential=DefaultAzureCredential()
)
# Create an agent
agent = client.agents.create_agent(
model="gpt-4o",
name="my-research-agent",
instructions="""You are a helpful research agent.
Use your tools to find information and provide accurate,
well-cited answers."""
)
Step 2: Add Tools
Code Interpreter
Lets the agent write and execute Python code for data analysis, chart generation, and calculations.
from azure.ai.projects.models import CodeInterpreterTool
agent = client.agents.create_agent(
model="gpt-4o",
name="data-analyst-agent",
instructions="Analyze data using code interpreter.",
tools=[CodeInterpreterTool()]
)
File Search (RAG)
Connects the agent to your documents via Azure AI Search.
from azure.ai.projects.models import FileSearchTool
agent = client.agents.create_agent(
model="gpt-4o",
name="knowledge-agent",
instructions="Answer questions based on the provided documents.",
tools=[FileSearchTool(vector_store_ids=["vs_abc123"])]
)
Custom Functions
Define your own functions that the agent can call.
from azure.ai.projects.models import FunctionTool
tools = FunctionTool(functions=[{
"name": "get_weather",
"description": "Get current weather for a location",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string", "description": "City name"}
},
"required": ["location"]
}
}])
Step 3: Create a Thread and Run
# Create a conversation thread
thread = client.agents.create_thread()
# Add a user message
client.agents.create_message(
thread_id=thread.id,
role="user",
content="Analyze the Q4 sales data and create a summary chart."
)
# Run the agent
run = client.agents.create_and_process_run(
thread_id=thread.id,
assistant_id=agent.id
)
# Get the response
messages = client.agents.list_messages(thread_id=thread.id)
print(messages.data[0].content[0].text.value)
Step 4: Deploy to Production
- Test thoroughly in the Foundry playground
- Set up content filters and safety guardrails
- Deploy as an API endpoint
- Monitor with Azure Monitor and Application Insights
Resources
Video: Find the official walkthrough on Microsoft Developer YouTube — search for "Azure AI Agent Service"


