Build Your First Autonomous AI Agent with .NET: A Step-by-Step Guide

Introduction

Welcome to the hands-on guide for creating your first AI agent using the Microsoft Agent Framework. If you've already explored Microsoft Extensions for AI (MEAI) and the VectorData library, you're ready to take the next leap: building an agent that doesn't just answer questions but acts on your behalf. Released as a production-ready 1.0 in April 2026, the Microsoft Agent Framework lets you create intelligent agents in C# (and Python) that can reason, use tools, remember context, and even collaborate with other agents—all without writing rigid, step-by-step code.

Build Your First Autonomous AI Agent with .NET: A Step-by-Step Guide
Source: devblogs.microsoft.com

In this guide, you'll create a simple joke-telling agent called "Joker." You'll learn the core patterns and see how the framework builds directly on the familiar IChatClient abstraction from MEAI.

What You Need

Step-by-Step Instructions

Step 1: Create a New Console Application

Open your terminal and run the following command to scaffold a new console app:

dotnet new console -n MyFirstAgent
cd MyFirstAgent

This creates a simple project with a Program.cs file.

Step 2: Install the Microsoft Agent Framework Package

The agent framework lives in the Microsoft.Agents.AI NuGet package. Install it with:

dotnet add package Microsoft.Agents.AI

This also pulls in the necessary MEAI dependencies because the agent framework uses IChatClient under the hood.

Step 3: Set Up Environment Variables

You'll need two values from your Azure OpenAI resource:

Set these in your terminal (or IDE) so the application can read them at runtime. For example, in bash:

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

In Windows Command Prompt:

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

Step 4: Write the Agent Code

Replace the contents of Program.cs 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."));

What’s happening here?

Notice that the agent doesn't require any explicit tool definitions yet—this is the simplest possible agent, a pure conversational one.

Build Your First Autonomous AI Agent with .NET: A Step-by-Step Guide
Source: devblogs.microsoft.com

Step 5: Run Your Agent

Back in the terminal, execute:

dotnet run

If everything is configured correctly, you'll see a joke printed to the console. For example:

Why are pirates called pirates? Because they just arrr!

Congratulations—you've just deployed your first autonomous AI agent! While it behaves like a simple chatbot in this example, the same pattern can be extended to give the agent tools, memory, and multi-step reasoning.

Tips & Next Steps

For the complete code sample and additional resources, see the official Microsoft Agent Framework documentation.

Recommended

Discover More

How to Prepare Your Crate for docs.rs‘s New Default Target BehaviorExploring CSS Color Palettes Beyond Tailwind: Resources and GeneratorsKubernetes v1.36 Enhances Pod Resource Management with Beta In-Place Vertical ScalingGlobal Momentum Away from Fossil Fuels: Santa Marta Summit and Key Climate Developments10 Ways Reality Fracture Redefines Magic: The Gathering's Multiverse