How to Build Your First AI Agent with Microsoft Agent Framework

By

Introduction

Welcome to this practical guide on creating an intelligent AI agent using the Microsoft Agent Framework. If you've followed our earlier discussions on Microsoft Extensions for AI (MEAI) and Microsoft.Extensions.VectorData, you already have a solid foundation. MEAI provides a unified way to communicate with large language models, while VectorData enables semantic search and retrieval-augmented generation (RAG). Now, it's time to move beyond simple question-answering and build an agent that can take actions, use tools, maintain context, and even collaborate with other agents. This step-by-step tutorial will walk you through setting up your first agent in .NET, from project creation to running a working example. By the end, you'll have a clear understanding of the agent lifecycle and how the framework simplifies orchestration.

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

What You Need

Step-by-Step Instructions

Step 1: Create a New Console Application

Open your terminal or command prompt and run the following command to scaffold a new .NET console project:

dotnet new console -n MyFirstAgent
cd MyFirstAgent

This creates a directory with a Program.cs file where you'll write your agent code.

Step 2: Install the Microsoft Agent Framework NuGet Package

Add the agent framework package to your project. The package name is Microsoft.Agents.AI and it includes all the core abstractions and the Azure OpenAI integration:

dotnet add package Microsoft.Agents.AI

This command pulls the latest stable version (the framework reached 1.0 in April 2026). If you plan to use other model providers, you may need additional packages, but for this guide, Azure OpenAI is sufficient.

Step 3: Set Environment Variables

Before running your agent, you must provide your Azure OpenAI endpoint and deployment name. The simplest way is to set environment variables. On Windows (Command Prompt):

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

On macOS/Linux (bash):

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

Replace the placeholder values with your actual resource details. The DefaultAzureCredential will try to authenticate using your Azure CLI login; if you prefer an API key, you can alter the code accordingly.

Step 4: Write the Agent Creation Code

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

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."));

Let's break down what's happening:

The key insight here is that the agent framework builds directly on top of IChatClient. If you've used MEAI, this pattern will feel familiar. The agent inherits all the capabilities of the underlying chat model but adds the ability to maintain state and use tools.

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

Step 5: Build and Run the Agent

Compile the project and execute it:

dotnet run

If everything is configured correctly, you should see a joke about pirates printed in the console. For example:

Why did the pirate go to the movie theater? Because he heard they had an 'Arrrr' rating system!

Congratulations, you've just built your first AI agent! While this example is simple, note that the agent already has autonomy: it doesn't just echo responses; it uses the provided instructions to generate contextually appropriate humor.

Tips for Success

This guide provided a minimal but practical starting point. In future articles, we'll dive into adding custom tools, handling multi-turn conversations, and orchestrating agent teams. Happy building!

Tags:

Related Articles

Recommended

Discover More

10 Reasons Why Mac mini Is the Ultimate Platform for Perplexity's AI Personal ComputerApple's Expanding F1 Footprint: Miami GP, Streaming, and a Movie SequelCANopenTerm: A Terminal-Based Power Tool for CAN Network Monitoring and AnalysisKubernetes and the Rise of Persistent AI Agents: How Agent Sandbox Bridges the GapOpenAI Prevents ChatGPT Goblin Obsession Before GPT-5.5 Launch