Part 1 of 2 · Framework introduction · C# / .NET 10 · Updated 1 November 2026
You already know how to write a C# service, call an API, and read data. How do those skills translate into building an AI agent? Start with the framework’s role, then make one small model request before introducing tools and a complete business application.
This article explains Microsoft Agent Framework, the vocabulary you will encounter, the packages needed for a first .NET project, and a short program you can run. In Part 2: User Ticket Agent using Microsoft Agent Framework, we apply these ideas to a practical support-ticket assistant.
1. What is Microsoft Agent Framework?
Microsoft Agent Framework is an open-source framework for building agents and orchestrating agent workflows. Its building blocks connect model clients, tools, conversation sessions, and application code. .NET developers use the Microsoft.Agents.AI APIs, with integration packages for their chosen provider. Microsoft positions it as the successor to the agent work in Semantic Kernel and AutoGen. Read the official overview for the framework’s scope.
Think of it as application infrastructure around a model. The model generates responses; your .NET application decides what data, tools, credentials, and execution boundaries are available.
It is not a new C# language, a model you train, or an automatic connection to your business database. You still write the application, choose a model deployment, and implement any business operations it can perform.
2. What is an AI agent?
For our learning path, an agent is an application component that accepts a goal or question, uses a model to decide how to respond, and may call permitted tools to gather facts or perform work.
Consider three requests:
| Request | What the application needs |
|---|---|
| “Explain dependency injection” | A model response with suitable instructions |
| “What is the status of ticket TKT-1042?” | A model plus a tool that reads authorized ticket data |
| “Escalate this ticket after manager approval” | A controlled process that records approval and performs an authorized write |
The first request is enough for a tiny introductory program. The second introduces useful tool calling. The third needs stronger process controls. You do not need to start with all three.
3. Understand the vocabulary
| Term | Meaning | Example in this series |
|---|---|---|
| Model | The system that interprets input and generates output | A model reached through a provider client |
| Provider/client | The integration used to reach that model or agent service | The OpenAI Responses client used by this example |
| Agent | The configured component that handles requests | DotNetTutor in the first program |
| Instructions | Guidance about the agent’s role and response style | “Explain .NET concepts to beginners” |
| Prompt | The request for a particular run | “Explain dependency injection” |
| Tool | An operation exposed to the agent | The ticket lookup in Part 2 |
| Session | Conversation context shared across turns | Remembering which ticket “it” refers to |
| Workflow | Explicit coordination of steps | Lookup, approval, then escalation |
The framework supports agents, workflows, sessions, middleware, and integrations. Tools and provider features vary; check the relevant integration rather than assuming every model supports every capability. See the tools overview.
Keep the framework separate from the provider
Microsoft Agent Framework gives our application its agent, instructions, tools, sessions, and run methods. OpenAI supplies the model access used in this introductory example. The provider is a connection choice; the framework concepts are what we are learning.
We use direct OpenAI access with an API key. The provider setup stays small so we can spend time on the agent itself. Part 2 uses the same provider connection to focus on tools and a practical application.
4. What happens during a basic run?
C# application
│ instructions + user prompt
▼
Configured agent
│ provider client
▼
Model provider
│ generated response
▼
Console output
In this first program there are no tools, no database, and no ticket records. The goal is to understand configuration and execution with the smallest useful example.
5. Required software and packages
Install the .NET 10 SDK and use your preferred C# editor. For this example, you also need an OpenAI API key and access to a model that supports the Responses API. Requests can incur API charges. See the OpenAI API quickstart for account and key setup.
There is one direct NuGet package:
| Package | Pinned version | Responsibility |
|---|---|---|
Microsoft.Agents.AI.OpenAI |
1.27.0 |
Connect Agent Framework to the OpenAI .NET client |
NuGet also restores the core Microsoft.Agents.AI APIs, Microsoft.Extensions.AI abstractions, and the OpenAI SDK as dependencies. You do not need a separate package for each namespace in the program. Pin versions and retain a dependency lock file when adopting the example.
The framework owns agent behavior; the SDK makes provider requests. For function tools later in the article, AIFunctionFactory comes from Microsoft.Extensions.AI and DescriptionAttribute comes from the .NET libraries.
We use the Responses client, following Microsoft’s current OpenAI provider guidance. The pinned SDK marks that client surface as experimental, so the program explicitly suppresses OPENAI001. Review this when updating dependencies; a stable framework version does not mean every SDK surface has the same status.
6. Create your first project
Run these commands in PowerShell:
dotnet new console -n HelloAgent -f net10.0
cd HelloAgent
dotnet add package Microsoft.Agents.AI.OpenAI --version 1.27.0
Set these variables in the same terminal where you will run the application:
$env:OPENAI_API_KEY = "YOUR-API-KEY"
$env:OPENAI_MODEL = "YOUR-MODEL-ID"
Replace the model placeholder with a Responses-compatible model ID available to your API project. We deliberately keep it configurable: learning the framework should not depend on one model name. Keep the key in environment configuration and out of source control and browser code. The sample reads the variables directly; it does not load a .env file.
7. A small, complete C# program
Replace Program.cs with this code:
using Microsoft.Agents.AI;
using OpenAI;
using OpenAI.Responses;
// The pinned OpenAI SDK marks its Responses surface as experimental.
#pragma warning disable OPENAI001
var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY")
?? throw new InvalidOperationException("Set OPENAI_API_KEY.");
var model = Environment.GetEnvironmentVariable("OPENAI_MODEL")
?? throw new InvalidOperationException("Set OPENAI_MODEL to a model available to your API project.");
var responsesClient = new OpenAIClient(apiKey).GetResponsesClient();
AIAgent agent = responsesClient
.AsAIAgent(
model: model,
name: "DotNetTutor",
instructions: "Explain .NET concepts to beginners using short, practical examples.");
using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(60));
var response = await agent.RunAsync(
"Explain dependency injection in three short sentences.",
cancellationToken: timeout.Token);
Console.WriteLine(response);
Read the framework calls
| Code | What you learn |
|---|---|
new OpenAIClient(apiKey).GetResponsesClient() |
Create the provider connection |
AsAIAgent(...) |
Wrap that connection in an Agent Framework agent |
name |
Give the agent an application-level identity |
instructions |
Define the role and response style |
RunAsync(prompt) |
Execute a request through the agent |
The instructions describe how the tutor should answer in general. The prompt asks a particular question. Keeping these separate makes it easy to reuse the agent for another .NET topic.
CancellationTokenSource is ordinary .NET code that bounds the request duration. The using OpenAI.Responses import brings the Responses-client extension methods into scope for the pinned packages.
8. Run and inspect the response
dotnet run
An illustrative answer might be:
Dependency injection supplies a class with the services it needs.
For example, an OrderService can receive an ILogger in its constructor.
This makes dependencies explicit and easier to replace in tests.
Your model may phrase the explanation differently. This text is an example, not a captured response or an exact-output test. Check whether the answer is accurate and follows the requested style.
Try changing only the prompt to “Explain an interface with a tiny C# example.” Then change the instructions to address an experienced developer. Comparing the two results helps distinguish a per-request question from persistent agent guidance.
The sample compiles with the pinned dependencies. Actual model execution requires your own API key, model access, and network connection.
9. What comes after a single prompt?
Add a conversation session
An isolated request has no shared application conversation state with another isolated run. To ask follow-up questions, create and reuse a session:
AgentSession session = await agent.CreateSessionAsync();
Console.WriteLine(await agent.RunAsync("Explain dependency injection.", session));
Console.WriteLine(await agent.RunAsync("Give me a C# example of that.", session));
This is an optional replacement for the single request above, not a separate standalone program. Session state is conversation context, not automatic durable memory or authorization. See sessions for lifecycle and persistence details.
Add one tiny C# tool
A tool is ordinary application code exposed to the model. To see the idea without building a business application, let the tutor calculate a simple study schedule.
Add these imports at the top of Program.cs:
using System.ComponentModel;
using Microsoft.Extensions.AI;
Replace the AIAgent agent = ... statement with this version, keeping the existing responsesClient and model variables:
AIAgent agent = responsesClient.AsAIAgent(
model: model,
name: "DotNetTutor",
instructions: "Help beginners study .NET. Use EstimateStudyDays for study-duration calculations.",
tools: [AIFunctionFactory.Create(EstimateStudyDays)]);
Add this local function at the end of the file:
[Description("Estimate whole study days from total study minutes and minutes available per day.")]
static int EstimateStudyDays(
[Description("Total study minutes, from 1 to 1000000.")] int totalMinutes,
[Description("Available minutes per day, from 1 to 1440.")] int minutesPerDay)
{
if (totalMinutes is < 1 or > 1_000_000)
throw new ArgumentOutOfRangeException(nameof(totalMinutes));
if (minutesPerDay is < 1 or > 1440)
throw new ArgumentOutOfRangeException(nameof(minutesPerDay));
return (int)Math.Ceiling((double)totalMinutes / minutesPerDay);
}
Change the prompt to “I have 150 minutes of .NET lessons and can study 40 minutes per day. How many days do I need?” The function returns 4 when called with those arguments. Inspect actual tool execution when evaluating the agent; a correct answer alone does not prove that the tool ran.
Description explains the operation and its parameters. AIFunctionFactory.Create turns it into a tool definition and executable function. Registering the tool makes it available; the model can request it, and the framework handles the function-call round trip. The C# validation still enforces the allowed numeric range.
Prompt → model requests tool → framework invokes C#
→ tool returns 4 → model explains the result
The model does not execute arbitrary C# from its response. It requests a registered operation. Our tool has no side effects. For a real operation, enforce authorization and input rules in application code. See Microsoft’s function-tool guide.
Stream a response
For a longer explanation, replace the single RunAsync call and Console.WriteLine with:
await foreach (var update in agent.RunStreamingAsync(
"Explain interfaces with a short C# example.",
cancellationToken: timeout.Token))
{
Console.Write(update.Text);
}
Console.WriteLine();
Streaming returns response updates as they arrive. It changes how the application displays output; it does not create another agent or automatically improve answer accuracy. This snippet and the session example are separate exercises—apply one change at a time.
Add a workflow when the order matters
If a process must read a record, obtain approval, and then write a change, make those transitions explicit. Start with ordinary functions for deterministic work. Introduce agent decisions where flexible interpretation provides a useful benefit.
Next: build the User Ticket Agent
You now have the essentials: a provider client, an agent, instructions, runs, sessions, and function tools. Continue to User Ticket Agent using Microsoft Agent Framework to add a C# function tool, ticket data, sessions, architecture diagrams, and practical checks.
References
- Microsoft Agent Framework overview
- OpenAI provider integration
- OpenAI API quickstart
- Tools overview
- C# function tools
- Conversation sessions
- Framework releases
The explanation and learning sequence are original; these sources support the framework concepts and API usage.