Overview
This quickstart will guide you through creating your first AI agent using the Microsoft Agent Framework .NET SDK. You’ll learn the core concepts and patterns used throughout the framework.Make sure you’ve completed the Installation guide before proceeding.
Core Concepts
Before diving into code, let’s understand the key components:AIAgent
The main abstraction for interacting with AI models. Created from a chat client with instructions and optional tools.
ChatClient
The underlying client that communicates with AI providers (Azure OpenAI, OpenAI, etc.).
AgentSession
Manages conversation state and history for multi-turn interactions.
Tools
Functions that agents can call to perform actions or retrieve information.
Your First Agent
Let’s create a simple agent that can respond to questions:1
Import required namespaces
2
Configure the connection
3
Create the agent
4
Run the agent
Complete Example
Here’s the complete working example:Program.cs
Streaming Responses
For real-time feedback, use streaming to receive responses as they’re generated:Streaming is ideal for user-facing applications where you want to show progress in real-time.
Multi-Turn Conversations
UseAgentSession to maintain conversation context across multiple interactions:
How It Works
- CreateSessionAsync() initializes a new conversation session
- Pass the
sessionobject to subsequentRunAsync()calls - The session automatically maintains the conversation history
- The agent has full context from previous messages
Adding Tools (Functions)
Agents become more powerful when they can call functions:1
Define a function with Description attributes
2
Create an agent with tools
3
Run the agent
GetWeather function and incorporate the result into its response.Complete Tool Example
Program.cs
Key Patterns
Async/Await
All agent interactions are asynchronous:Extension Methods
The SDK uses extension methods for clean API design:Dependency Injection
The framework integrates with Microsoft.Extensions.DependencyInjection:Common Patterns
Reading configuration from environment variables
Reading configuration from environment variables
Error handling
Error handling
Using cancellation tokens
Using cancellation tokens
Example Use Cases
Customer Support Bot
Build agents that answer customer questions and route to human agents when needed.
Data Analysis Assistant
Create agents that query databases and generate insights from data.
Code Assistant
Develop agents that help with coding tasks, explain code, and suggest improvements.
Workflow Automation
Orchestrate complex multi-step workflows with agent coordination.
Next Steps
Now that you understand the basics:Create Your First Agent
Build a complete agent with advanced features
Working with Tools
Learn how to create and use agent tools
Agent Sessions
Master multi-turn conversations and context management
Browse Examples
Explore sample code and patterns
Best Practices
Production Credentials: Use
ManagedIdentityCredential instead of DefaultAzureCredential in production to avoid latency from credential probing.Async All The Way: Always use async/await patterns. Never use
.Result or .Wait() as it can cause deadlocks.Dispose Sessions: If you create many sessions, consider disposing them when done to free resources.