Beginner
π€ Hello Agent
Create your very first AI agent using Microsoft Agent Framework in just a handful of lines of C#.
The AIAgent abstraction is the core building block of Agent Framework. It wraps any
IChatClient implementation β Azure OpenAI, OpenAI, Anthropic, Ollama β into a
consistent, feature-rich agent interface. You set the agent's instructions (its system prompt),
then call RunAsync() with a user message to get a response.
Under the hood, Agent Framework manages the message format, tool call loops, and response extraction so you can focus on building your application rather than orchestrating API calls.
Key Concepts
- AIAgent β the unified agent interface; all agent types implement it
- AsAIAgent() β extension method on
ChatClientthat creates an agent - instructions β the system prompt; sets the agent's persona and behaviour
- RunAsync() β sends a message and awaits the complete response
- AzureOpenAIClient β Azure OpenAI SDK client; swap for
OpenAIClientto use OpenAI directly
NuGet Packages
dotnet add package Microsoft.Agents.AI.OpenAI --prerelease
dotnet add package Azure.AI.OpenAI --prerelease
dotnet add package Azure.Identity
Code Sample
using Azure.AI.OpenAI;
using Azure.Identity;
using Microsoft.Agents.AI;
// 1. Create an Azure OpenAI client using your endpoint and Azure CLI credentials.
// Set the AZURE_OPENAI_ENDPOINT environment variable before running.
AIAgent agent = new AzureOpenAIClient(
new Uri(Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT")!),
new AzureCliCredential())
// 2. Specify the deployed model name (e.g. "gpt-4o-mini").
.GetChatClient("gpt-4o-mini")
// 3. Wrap the chat client as an AIAgent with a system instruction.
.AsAIAgent(instructions: "You are a friendly assistant. Keep your answers brief.");
// 4. Send a message and print the response.
Console.WriteLine(await agent.RunAsync("What is the largest city in France?"));
Step-by-Step Explanation
-
Create the Azure OpenAI client β
AzureOpenAIClienttakes your endpoint URI and a credential.AzureCliCredentialuses your local Azure CLI login, which is convenient during development. -
Get a chat client β
GetChatClient("gpt-4o-mini")specifies the model deployment you want to use. This returns anOpenAI.Chat.ChatClient. -
Create the agent β
.AsAIAgent(instructions: "...")is an extension method provided by Agent Framework that wraps theChatClientin a fully featuredAIAgent. Theinstructionsparameter sets the system prompt. -
Run the agent β
agent.RunAsync("...")sends the user message to the model and returns anAgentResponse. When printed directly, it outputs the agent's text reply.
Expected Output
Paris
Next Steps
All Examples
- π€ Hello Agent
- π§ Function Tools
- π¬ Multi-Turn Conversations
- β‘ Streaming Responses
- π¦ Structured Output
- π Sequential Workflows
- πΈοΈ Multi-Agent Orchestration
- π¦ Ollama β Local AI
- π₯οΈ LM Studio β Local AI
- π§ Agent Memory
- π RAG
- π MCP Tools
- π OpenTelemetry
- π§ Customer Support Triage
- π¬ Research Pipeline
- π€ Tools vs Sub-Agents
Concepts Used
AIAgent
AsAIAgent()
RunAsync()
Azure OpenAI
AzureCliCredential
π Official Docs