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.
Let’s create a simple agent that can respond to questions:
1
Import required namespaces
using Azure.AI.OpenAI;using Azure.Identity;using Microsoft.Agents.AI;using OpenAI.Chat;
2
Configure the connection
var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set.");var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini";
3
Create the agent
AIAgent agent = new AzureOpenAIClient( new Uri(endpoint), new DefaultAzureCredential()) .GetChatClient(deploymentName) .AsAIAgent( instructions: "You are a helpful assistant that provides clear, concise answers.", name: "Assistant");
4
Run the agent
string response = await agent.RunAsync("What is the Microsoft Agent Framework?");Console.WriteLine(response);
using Azure.AI.OpenAI;using Azure.Identity;using Microsoft.Agents.AI;using OpenAI.Chat;var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set.");var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini";AIAgent agent = new AzureOpenAIClient( new Uri(endpoint), new DefaultAzureCredential()) .GetChatClient(deploymentName) .AsAIAgent( instructions: "You are a helpful assistant that provides clear, concise answers.", name: "Assistant");string response = await agent.RunAsync("What is the Microsoft Agent Framework?");Console.WriteLine(response);
Use AgentSession to maintain conversation context across multiple interactions:
// Create a session to preserve conversation historyAgentSession session = await agent.CreateSessionAsync();// First turnConsole.WriteLine(await agent.RunAsync("My name is Alice.", session));// Second turn - the agent remembers the contextConsole.WriteLine(await agent.RunAsync("What's my name?", session));
Agents become more powerful when they can call functions:
1
Define a function with Description attributes
using System.ComponentModel;[Description("Get the current weather for a location.")]static string GetWeather( [Description("The city name")] string location){ return $"The weather in {location} is sunny with a high of 22°C.";}
2
Create an agent with tools
using Microsoft.Extensions.AI;AIAgent agent = new AzureOpenAIClient( new Uri(endpoint), new DefaultAzureCredential()) .GetChatClient(deploymentName) .AsAIAgent( instructions: "You are a helpful weather assistant.", tools: [AIFunctionFactory.Create(GetWeather)]);
3
Run the agent
Console.WriteLine(await agent.RunAsync("What's the weather in Seattle?"));
The agent will automatically call your GetWeather function and incorporate the result into its response.
using System.ComponentModel;using Azure.AI.OpenAI;using Azure.Identity;using Microsoft.Agents.AI;using Microsoft.Extensions.AI;using OpenAI.Chat;var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set.");var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini";[Description("Get the weather for a given location.")]static string GetWeather([Description("The location to get the weather for.")] string location) => $"The weather in {location} is cloudy with a high of 15°C.";AIAgent agent = new AzureOpenAIClient( new Uri(endpoint), new DefaultAzureCredential()) .GetChatClient(deploymentName) .AsAIAgent( instructions: "You are a helpful assistant", tools: [AIFunctionFactory.Create(GetWeather)]);Console.WriteLine(await agent.RunAsync("What is the weather like in Amsterdam?"));
The framework integrates with Microsoft.Extensions.DependencyInjection:
services.AddSingleton<AIAgent>(sp =>{ var chatClient = new AzureOpenAIClient( new Uri(endpoint), new DefaultAzureCredential()) .GetChatClient(deploymentName); return chatClient.AsAIAgent( instructions: "You are helpful.", name: "MyAgent");});
var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set.");var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini";
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30));var response = await agent.RunAsync( "Write a long story", cancellationToken: cts.Token);