Overview
This quickstart guide walks you through creating a simple AI agent using the Microsoft Agent Framework. You’ll learn how to:- Create an agent with Azure OpenAI
- Run the agent with a simple query
- Use streaming responses
Before starting, ensure you’ve completed the Installation steps and configured your environment variables.
Create Your First Agent
Let’s create a simple agent that can answer questions.Import Required Modules
Create a new Python file called
hello_agent.py and import the necessary modules:Create the Agent
Create an Azure OpenAI client and configure it as an agent:
The
as_agent() method is a convenient way to wrap a chat client with agent capabilities.Complete Example
Here’s the complete code for your first agent:hello_agent.py
Run Your Agent
Execute your agent script:Understanding the Code
Why use async/await?
Why use async/await?
The Agent Framework uses asynchronous programming to efficiently handle I/O operations like API calls. All agent operations use
async/await patterns:- Define async functions with
async def - Call async functions with
await - Run async code with
asyncio.run()
What is AzureCliCredential?
What is AzureCliCredential?
AzureCliCredential from the azure-identity package uses your Azure CLI login for authentication. This means:- No API keys in your code
- Uses your existing Azure CLI session (
az login) - Automatically handles token refresh
- Works seamlessly with Azure services
Streaming vs Non-Streaming
Streaming vs Non-Streaming
Non-streaming (
await agent.run()):- Waits for the complete response
- Returns the full text at once
- Best for short responses or when you need the complete answer
async for chunk in agent.run(..., stream=True)):- Returns tokens as they’re generated
- Provides immediate feedback to users
- Best for longer responses or chat interfaces
Multi-Turn Conversations
To maintain context across multiple interactions, use anAgentSession:
The
AgentSession automatically tracks conversation history, allowing the agent to maintain context across multiple turns.Next Steps
Build Your First Agent
Add tools and function calling to your agent
Core Concepts
Learn about agents, tools, and middleware