How to Build Your First AI Agent with the Microsoft Agent Framework in .NET

By

Introduction

Welcome back to the building blocks for AI in .NET series. So far, we've covered Microsoft Extensions for AI (MEAI) for unified LLM interaction and Microsoft.Extensions.VectorData for semantic search and RAG. Now, we reach the third pillar: the Microsoft Agent Framework. This production-ready SDK (1.0 released April 2026) lets you create AI agents that don't just chat—they reason, use tools, remember context, and coordinate with other agents. In this how-to guide, you'll build your first simple agent using C# and the Agent Framework.

How to Build Your First AI Agent with the Microsoft Agent Framework in .NET
Source: devblogs.microsoft.com

What You Need

Step-by-Step Guide

Step 1: Create a New Console Application

Open your terminal and run:

dotnet new console -o MyFirstAgent
cd MyFirstAgent

Step 2: Install the Microsoft Agent Framework Package

Add the Microsoft.Agents.AI package:

dotnet add package Microsoft.Agents.AI

This package provides the AIAgent class and the essential AsAIAgent() extension method that wraps any IChatClient into an agent.

Step 3: Set Up Environment Variables

Set the two required variables in your system or in a .env file (if using a loader). For example, on Windows PowerShell:

$env:AZURE_OPENAI_ENDPOINT = "https://your-resource.openai.azure.com/"
$env:AZURE_OPENAI_DEPLOYMENT_NAME = "gpt-5.4-mini"

On macOS/Linux:

export AZURE_OPENAI_ENDPOINT="https://your-resource.openai.azure.com/"
export AZURE_OPENAI_DEPLOYMENT_NAME="gpt-5.4-mini"

Step 4: Write the Agent Code

Open Program.cs and replace its contents with the following:

using Azure.AI.OpenAI;
using Azure.Identity;
using Microsoft.Agents.AI;

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-5.4-mini";

AIAgent agent = new AzureOpenAIClient(
    new Uri(endpoint),
    new DefaultAzureCredential())
    .GetChatClient(deploymentName)
    .AsAIAgent(
        instructions: "You are good at telling jokes.",
        name: "Joker");

Console.WriteLine(await agent.RunAsync("Tell me a joke about a pirate."));

Step 5: Understand the Code

Step 6: Run the Agent

In your terminal, execute:

How to Build Your First AI Agent with the Microsoft Agent Framework in .NET
Source: devblogs.microsoft.com
dotnet run

You should see a joke printed to the console, for example: "Why did the pirate go to the movie? Because he heard it had a good arrr-ating!"

Step 7: (Optional) Experiment with Different Instructions and Inputs

Change the instructions to something like "You are a helpful assistant that answers in rhymes." or the user input to a complex question. The agent framework automatically handles multi-turn conversation history; each RunAsync call maintains context as long as you reuse the same agent instance.

Tips and Conclusion

You've now built your first AI agent. Unlike a simple chatbot, this agent can decide how to accomplish a task within its instructions. In the next part of the series, we'll add tools and show how agents can interact with external systems autonomously.

Tags:

Related Articles

Recommended

Discover More

Anatomy of a Nation-State Cyber Espionage Campaign: Understanding SHADOW-EARTH-053Exploring the Iconic Heroes and Villains of Masters of the UniverseGitHub Copilot Shifts to Token-Based Pricing in Mid-2026Securing Cargo Against Directory Permission Escalation AttacksSecurity Blocks ClickHouse Deployment Amid Base Image Vulnerabilities; Docker Hardened Images Emerge as Solution