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 ChatClient that 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 OpenAIClient to 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

  1. Create the Azure OpenAI client β€” AzureOpenAIClient takes your endpoint URI and a credential. AzureCliCredential uses your local Azure CLI login, which is convenient during development.
  2. Get a chat client β€” GetChatClient("gpt-4o-mini") specifies the model deployment you want to use. This returns an OpenAI.Chat.ChatClient.
  3. Create the agent β€” .AsAIAgent(instructions: "...") is an extension method provided by Agent Framework that wraps the ChatClient in a fully featured AIAgent. The instructions parameter sets the system prompt.
  4. Run the agent β€” agent.RunAsync("...") sends the user message to the model and returns an AgentResponse. When printed directly, it outputs the agent's text reply.

Expected Output

Paris

Next Steps