Building Your First AI Agent with Microsoft Agent Framework

Building Your First AI Agent with Microsoft Agent Framework

The Microsoft Agent Framework represents a unified step forward by consolidating the evolution of Semantic Kernel and AutoGen into one powerful and modern platform.

Built with enterprise needs in mind, the framework enables developers to design autonomous AI agents, conversational experiences, and complex multi-agent workflows, establishing a new benchmark for AI orchestration in corporate environments.

Its strategic relevance comes from providing a solid foundation for developing scalable, secure, and highly interactive AI applications across the .NET ecosystem.

 

What Is the Microsoft Agent Framework?

The Microsoft Agent Framework is an SDK for building AI agents and multi-agent workflows using .NET and Python.

It unifies the ideas behind Semantic Kernel and AutoGen, combining clean abstractions with enterprise-ready capabilities.

At its core, it focuses on two things: creating individual AI agents powered by LLMs, and orchestrating complex workflows that coordinate agents, tools, and long-running processes.


Now that we understand these foundations, we can start with the first pillar: building a basic AI agent.

				dotnet add package Azure.AI.OpenAI --prerelease
dotnet add package Azure.Identity
dotnet add package Microsoft.Agents.AI.OpenAI --prerelease
			

Creating and Running a Basic Agent with OpenAI

Getting a basic agent up and running is straightforward and follows a simple three-step flow.

First, configure the AI client by pointing it to Ollama’s compatible endpoint running locally or your provider endpoint url, like in the example. No real credentials are required, which makes local development fast and frictionless.

Next, create the agent by defining its behavior through the instructions parameter. This is where you set the agent’s personality, boundaries, and decision-making style.

Finally, run the agent by invoking RunAsync with a user prompt and let the local LLM generate the response.

Check this Daily/Standup Joke Agent Endpoint:

C#
				using System;
using Azure.AI.OpenAI;
using Azure.Identity;
using Microsoft.Agents.AI;
using OpenAI;

AIAgent agent = new AzureOpenAIClient(
    new Uri("https://openai.openai.azure.com"),
    new AzureCliCredential())
    .GetChatClient("gpt-4o-mini")
    .CreateAIAgent(
                name: "Joker",
                instructions: """
                    You are a standup/daily joker.
                    Generate a short, clean, work-safe joke related to software development,
                    agile ceremonies, bugs, deadlines, or code reviews.
                    Keep it under two sentences.
                    """);

Console.WriteLine(await agent.RunAsync("Tell me today's joke"));
			

This makes it very clear that Microsoft Agent Framework is about orchestration and behavior.

An agent is not the same as a chatbot because while a chatbot is reactive and limited to generating responses to messages, an agent is goal-driven, capable of planning, making decisions, invoking tools, maintaining state, and executing actions within structured workflows.

Here’s an AI-generated joke to brighten your day:

				Code review is the only place where everyone agrees the code works - just not like that.
			

Running the Agent with Streaming

You can run your agent in streaming mode to get partial responses as they are generated:

C#
				await foreach (var update in agent.RunStreamingAsync("Tell me a joke about a bug in production."))
{
    // Each update returns a part of the generated text
    Console.Write(update.Text);
}

			

Here’s what the output should look like.

				Why 
did 
the 
developer 
go 
broke
?


Because 
he 
used 
up 
all 
his 
cache 
fixing 
bugs
!

			

Running the Joker Agent with ChatMessages

You can also provide structured messages instead of plain strings:

C#
				// Single user message
ChatMessage userMessage = new(ChatRole.User, "Tell me a joke about code reviews.");
Console.WriteLine(await agent.RunAsync(userMessage));

// System + user messages
ChatMessage systemMessage = new(ChatRole.System, """
If the user asks you to tell a joke, refuse, and share a fun fact about software engineering instead.
""");
userMessage = new(ChatRole.User, "Tell me a joke about missing deadlines.");

Console.WriteLine(await agent.RunAsync(new [] { systemMessage, userMessage }));

			

Here’s what the output should look like.

				Here's a fun fact! The first computer bug was an actual moth stuck in a relay back in 1947.
			

Microsoft Agent Framework Is a Strong Choice for Modern .NET Applications

Choosing the right foundation for AI-driven features is becoming a critical architectural decision, especially in .NET-based systems.

Microsoft Agent Framework stands out by offering a clear separation between agent behavior, LLM providers, and execution flow.

Instead of coupling your application directly to a specific model or vendor, the framework promotes a provider-agnostic approach. This makes it easier to evolve your AI strategy over time, whether that means switching from local models like Ollama to cloud-based providers, or combining multiple models in the same system.

Another key advantage is how naturally the framework fits into existing .NET architectures. Agents can be treated as first-class components, integrated with dependency injection, logging, observability, and security patterns already familiar to .NET developers.

This alignment significantly lowers the barrier to adopting AI in production-grade applications.

Conclusion

Microsoft Agent Framework offers a clear and structured approach to building AI agents without compromising maintainability, flexibility, or architectural discipline.

By abstracting LLM interactions and emphasizing agent behavior and orchestration, it allows developers to go beyond simple, prompt-driven solutions.

In this article, we covered the fundamentals by creating a first AI agent, understanding how instructions shape behavior, and seeing how easily different LLM providers can be integrated. This foundation is crucial for unlocking more advanced capabilities.

In the upcoming articles, we’ll dive deeper into real-world scenarios, exploring workflows, branching logic, conditional execution, and other advanced patterns that make the Microsoft Agent Framework a powerful choice for building enterprise-grade AI solutions in .NET.

Thank you for reading.

See you next time!

Share the Post:
plugins premium WordPress