27 November 2026

Microsoft Agent Framework for Beginners: Build an AI Study Agent in C#

  Asp.Net CS By Example       27 November 2026

Sending a single prompt to an AI model is useful for quick answers, but real applications often need an agent that follows persistent system instructions, invokes C# functions when needed, and formats helpful replies. In .NET 11, the Microsoft Agent Framework (Microsoft.Agents.AI) builds directly on IChatClient so you can wrap a model and your C# tools into one clean AIAgent.

How the agent runs

Student Question → ChatClientAgent (Instructions + Model) → AIFunctionFactory Tool Call → Final Answer

1. Install the Packages

Create a new console application and add the Microsoft Agent Framework and OpenAI adapter packages:

dotnet new console -n StudyAgentDemo
cd StudyAgentDemo
dotnet add package Microsoft.Agents.AI --prerelease
dotnet add package OpenAI --prerelease
dotnet add package Microsoft.Extensions.AI.OpenAI --prerelease

2. Create a Study Assistant with ChatClientAgent

Set your OPENAI_API_KEY environment variable and update Program.cs with an AIAgent backed by ChatClientAgent:

using System.ClientModel;
using System.ComponentModel;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
using OpenAI;

string apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY")
    ?? throw new InvalidOperationException("Set OPENAI_API_KEY first.");

IChatClient chatClient = new OpenAIClient(new ApiKeyCredential(apiKey))
    .GetChatClient("gpt-4o-mini")
    .AsIChatClient();

[Description("Look up upcoming homework due dates for a subject.")]
static string GetAssignmentDeadline(
    [Description("Course subject such as CSharp or SQL")] string subject)
{
    return subject.ToUpperInvariant() switch
    {
        "CSHARP" => "LINQ Lab 4 is due Friday at 6:00 PM.",
        "SQL" => "Joins Worksheet is due Monday at 5:00 PM.",
        _ => "No upcoming deadline found for that subject."
    };
}

AIAgent agent = new ChatClientAgent(
    chatClient: chatClient,
    instructions: "You are a concise study assistant. Always check assignment deadlines with tools before answering.",
    name: "StudyAssistant",
    tools: [AIFunctionFactory.Create(GetAssignmentDeadline)]
);

var response = await agent.RunAsync("When is my SQL assignment due?");
Console.WriteLine(response);

Understand the Key Parts

  1. IChatClient connects your agent to the model provider while keeping the rest of your code portable.
  2. ChatClientAgent implements AIAgent and packages your model, system instructions, agent name, and tool list into one object.
  3. agent.RunAsync(...) automatically runs the prompt, invokes GetAssignmentDeadline if needed, and returns the final answer (or use RunStreamingAsync for live streaming).

Practice

  1. Add a second tool named GetOfficeHours(string subject) and pass it inside the tools array.
  2. Replace agent.RunAsync(...) with await foreach (var update in agent.RunStreamingAsync(...)) to stream output word by word.
  3. Ask a question combining two subjects in one sentence to see how the agent calls your tool multiple times.

Continue learning: .NET Core and ASP.NET Core: Student Learning Path.

References: AI for .NET Developers (Microsoft Learn).

logoblog

Thanks for reading Microsoft Agent Framework for Beginners: Build an AI Study Agent in C#

Previous
« Prev Post

No comments:

Post a Comment

Please do not enter any spam link in the comment box.