Skip to main content

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 the AIAgent abstract base class, which provides:
  • Unique Identity: Every agent has an Id and optional Name and Description
  • Conversation Management: Agents can create and manage AgentSession instances for multi-turn conversations
  • Execution Methods: RunAsync() and RunStreamingAsync() for synchronous and streaming responses

ChatClientAgent

The primary concrete implementation is ChatClientAgent, which wraps an IChatClient from Microsoft.Extensions.AI to interact with various AI providers.

Creating Agents

Basic Agent Creation

Use the AsAIAgent() extension method on any IChatClient to create an agent:
DefaultAzureCredential is convenient for development but requires careful consideration in production. Consider using a specific credential (e.g., ManagedIdentityCredential) to avoid latency issues and potential security risks.

Agent with Configuration Options

For more control, use ChatClientAgentOptions:

Running Agents

Simple Invocation

Streaming Responses

Multi-Turn Conversations

Use AgentSession 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 to AIFunction instances and used as tools by other agents:

Agent Builder Pattern

Use AIAgentBuilder 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 with AgentRunOptions:

Best Practices

Always use AgentSession for multi-turn conversations to maintain context and state.
Pass CancellationToken to agent methods to support graceful cancellation:
Agents may hold resources. Consider disposal patterns:
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