Overview
In the Microsoft Agent Framework .NET SDK,AIAgent is the foundational abstraction for creating AI-powered agents. Agents encapsulate an AI model, instructions, tools, and conversation management capabilities.
Core Concepts
AIAgent Base Class
All agents derive from theAIAgent abstract base class, which provides:
- Unique Identity: Every agent has an
Idand optionalNameandDescription - Conversation Management: Agents can create and manage
AgentSessioninstances for multi-turn conversations - Execution Methods:
RunAsync()andRunStreamingAsync()for synchronous and streaming responses
ChatClientAgent
The primary concrete implementation isChatClientAgent, which wraps an IChatClient from Microsoft.Extensions.AI to interact with various AI providers.
Creating Agents
Basic Agent Creation
Use theAsAIAgent() extension method on any IChatClient to create an agent:
Agent with Configuration Options
For more control, useChatClientAgentOptions:
Running Agents
Simple Invocation
Streaming Responses
Multi-Turn Conversations
UseAgentSession to maintain conversation context across multiple turns:
Streaming Multi-Turn Conversations
Structured Output
Using ResponseFormat
Request structured JSON output matching a specific schema:Using Generic RunAsync<T>
Agent as a Tool
Agents can be converted toAIFunction instances and used as tools by other agents:
Agent Builder Pattern
UseAIAgentBuilder to compose agents with middleware and additional functionality:
Learn More
Explore middleware patterns for agents
Session Management
Creating Sessions
Serializing Sessions
Sessions can be serialized for persistence:Agent Run Options
Customize agent behavior per request withAgentRunOptions:
Best Practices
Use Sessions for Context
Use Sessions for Context
Always use
AgentSession for multi-turn conversations to maintain context and state.Handle Cancellation
Handle Cancellation
Pass
CancellationToken to agent methods to support graceful cancellation:Dispose Resources
Dispose Resources
Agents may hold resources. Consider disposal patterns:
Use Structured Output for Data Extraction
Use Structured Output for Data Extraction
When extracting data, use structured output with strongly-typed classes rather than parsing text.
Next Steps
Tools & Functions
Add function calling capabilities to agents
Middleware
Enhance agents with middleware pipelines
Memory & Context
Add memory and context providers
Providers
Connect to different AI providers