Skip to main content

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.
1

Import Required Modules

Create a new Python file called hello_agent.py and import the necessary modules:
2

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.
3

Run the Agent

Send a message to the agent and print the response:
4

Add Streaming Support

For longer responses, use streaming to receive tokens as they’re generated:

Complete Example

Here’s the complete code for your first agent:
hello_agent.py

Run Your Agent

Execute your agent script:
You should see output similar to:

Understanding the Code

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()
This allows your application to handle multiple agents and requests concurrently without blocking.
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
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
Streaming (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 an AgentSession:
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