Skip to main content

AI Agents in C# with Semantic Kernel: Full Guide

Learn how to build AI agents in C# using Semantic Kernel. Step-by-step tutorial with runnable code, best practices, and pitfalls. Start building today!

Building AI agents in C# is no longer the exclusive territory of Python developers. With Microsoft's Semantic Kernel, .NET engineers can now create autonomous, task-solving agents that reason, call tools, and complete multi-step workflows—all in the language and ecosystem they already know. In this tutorial, you'll learn exactly how to build AI agents in C# using Semantic Kernel, from your first "hello world" agent to production-grade patterns used in real applications.

Whether you're a beginner searching for "how to build an AI agent," an intermediate developer looking for Semantic Kernel best practices, or a senior engineer exploring advanced autonomous agent design in C#, this guide walks through the concepts, the code, and the pitfalls to avoid.

What Are AI Agents in C#?

An AI agent is a program that uses a large language model (LLM) as its reasoning engine to decide what to do next. Unlike a simple chatbot that answers a single question, an agent can plan, invoke external tools (functions, APIs, databases), observe the results, and loop until a goal is achieved. This "reason → act → observe" cycle is what makes an agent autonomous.

Semantic Kernel is Microsoft's open-source SDK for orchestrating LLMs with your own code. It sits between your C# application and models like Azure OpenAI, OpenAI, or local models, and gives you three core building blocks:

  • Kernel — the central dependency-injection container that wires up models, plugins, and services.
  • Plugins (functions) — native C# methods the agent can call to take real-world actions.
  • Agents & planners — the layer that lets the model choose which functions to invoke, in what order, to solve a task.

The reason C# is a strong choice for building AI agents is type safety and enterprise integration. Your agent's tools are strongly typed methods, so the whole pipeline—from LLM output to database call—benefits from compile-time checks, dependency injection, and the mature .NET hosting ecosystem.

Setting Up Semantic Kernel in .NET

Start by creating a console project and adding the Semantic Kernel NuGet package. Everything below targets .NET 8 and the current 1.x line of Semantic Kernel.

// Terminal commands
// dotnet new console -n AgentDemo
// cd AgentDemo
// dotnet add package Microsoft.SemanticKernel

Next, build a Kernel and connect it to a chat completion model. Never hard-code API keys—read them from environment variables or user secrets. This is one of the most common security pitfalls when building AI agents in C#.

using Microsoft.SemanticKernel;

var builder = Kernel.CreateBuilder();

// Option A: OpenAI
builder.AddOpenAIChatCompletion(
    modelId: "gpt-4o",
    apiKey: Environment.GetEnvironmentVariable("OPENAI_API_KEY")!);

// Option B: Azure OpenAI
// builder.AddAzureOpenAIChatCompletion(
//     deploymentName: "gpt-4o",
//     endpoint: Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT")!,
//     apiKey: Environment.GetEnvironmentVariable("AZURE_OPENAI_KEY")!);

Kernel kernel = builder.Build();

At this point you have a working kernel. The magic of an autonomous agent comes from giving the model tools and letting it decide when to use them.

Building Your First Autonomous Agent

Step 1: Create a Plugin (the Agent's Tools)

A plugin is just a C# class with methods decorated with [KernelFunction]. The [Description] attributes are critical—they are the only way the LLM understands what each tool does and when to call it. Treat these descriptions as the agent's instruction manual.

using System.ComponentModel;
using Microsoft.SemanticKernel;

public class WeatherPlugin
{
    [KernelFunction]
    [Description("Gets the current temperature in Celsius for a given city.")]
    public string GetTemperature(
        [Description("The city name, e.g. London")] string city)
    {
        // In production, call a real weather API here.
        return city.ToLower() switch
        {
            "london" => "14",
            "sydney" => "23",
            "toronto" => "19",
            _ => "20"
        };
    }

    [KernelFunction]
    [Description("Recommends clothing based on a temperature in Celsius.")]
    public string RecommendClothing(
        [Description("Temperature in Celsius")] int celsius)
        => celsius < 15 ? "Wear a warm jacket." : "A t-shirt is fine.";
}

Step 2: Enable Automatic Function Calling

This is the heart of an autonomous agent. By setting FunctionChoiceBehavior.Auto(), you tell Semantic Kernel to let the model call your functions on its own, feed the results back into the conversation, and continue reasoning until it produces a final answer.

using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI;

kernel.Plugins.AddFromType<WeatherPlugin>();

var chat = kernel.GetRequiredService<IChatCompletionService>();

var settings = new OpenAIPromptExecutionSettings
{
    FunctionChoiceBehavior = FunctionChoiceBehavior.Auto()
};

var history = new ChatHistory();
history.AddUserMessage(
    "I'm in London today. What should I wear?");

var result = await chat.GetChatMessageContentAsync(
    history, settings, kernel);

Console.WriteLine(result.Content);
// The agent autonomously calls GetTemperature("London"),
// then RecommendClothing(14), then answers:
// "It's 14°C in London, so wear a warm jacket."

Notice what happened: you never told the code to call GetTemperature then RecommendClothing. The model planned that sequence itself. That is the difference between a scripted workflow and a genuine AI agent.

Using the Agent Framework for Persistent Agents

For more structured scenarios, Semantic Kernel offers a dedicated Agent Framework via the ChatCompletionAgent class. It lets you give an agent a persistent name, instructions (its "system prompt"), and a reusable identity—ideal when you're building multiple specialized agents that collaborate.

using Microsoft.SemanticKernel.Agents;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI;

ChatCompletionAgent travelAgent = new()
{
    Name = "TravelAssistant",
    Instructions =
        "You are a concise travel assistant. Use the available " +
        "tools to check weather and give clothing advice. " +
        "Always state the temperature you found.",
    Kernel = kernel,
    Arguments = new(new OpenAIPromptExecutionSettings
    {
        FunctionChoiceBehavior = FunctionChoiceBehavior.Auto()
    })
};

var thread = new ChatHistoryAgentThread();

await foreach (var response in travelAgent.InvokeAsync(
    "Packing for Sydney tomorrow — what do I wear?", thread))
{
    Console.WriteLine(response.Message.Content);
}

The Agent Framework shines when you build multi-agent systems: for example, a "researcher" agent that gathers data and a "writer" agent that formats it, coordinated by an orchestration layer. Each agent has its own instructions and tool set but shares the same kernel infrastructure.

AI Agent Best Practices in C#

Once you move beyond demos, the following best practices separate reliable production agents from fragile prototypes.

  • Write descriptions for the model, not for humans. The [Description] text is the agent's decision-making input. Ambiguous descriptions cause the model to call the wrong tool. Be explicit about inputs, outputs, and units.
  • Keep functions small and single-purpose. One tool = one clear action. Fine-grained functions give the model more precise control and produce more predictable plans.
  • Set a maximum iteration limit. An autonomous loop can spiral. Guard against runaway tool-calling by capping turns and adding timeouts.
  • Validate all LLM-supplied arguments. Treat parameters the model passes to your functions as untrusted input—validate ranges, sanitize strings, and never pass them straight into SQL or shell commands.
  • Use dependency injection. Register the kernel and plugins in your IServiceCollection so agents integrate cleanly with ASP.NET Core apps, logging, and configuration.
  • Add telemetry. Semantic Kernel emits OpenTelemetry traces. Log which functions were called and with what arguments to debug agent behavior.

Common Pitfalls to Avoid

  • Leaking API keys by hard-coding them—always use environment variables, user secrets, or Azure Key Vault.
  • Uncontrolled cost. Auto function calling can trigger many model round-trips. Monitor token usage and prefer smaller models for simple routing tasks.
  • Overloading a single agent with 30 tools. Models degrade when given too many choices; split responsibilities across specialized agents.
  • Ignoring prompt injection. If your agent reads external web content or user files, that content can contain instructions. Isolate untrusted data and constrain what tools it can influence.

Advanced: Adding Memory and RAG to Your C# Agent

Real autonomous agents need context beyond a single conversation. Semantic Kernel supports vector-store connectors (Azure AI Search, Qdrant, Redis, and more) so your agent can retrieve relevant documents—a pattern known as Retrieval-Augmented Generation (RAG). You expose retrieval as just another [KernelFunction], and the agent decides when it needs to look something up.

public class KnowledgePlugin
{
    private readonly IVectorSearch _search;
    public KnowledgePlugin(IVectorSearch search) => _search = search;

    [KernelFunction]
    [Description("Searches the company knowledge base for relevant info.")]
    public async Task<string> SearchDocsAsync(
        [Description("The search query")] string query)
    {
        var results = await _search.QueryAsync(query, top: 3);
        return string.Join("\n---\n", results);
    }
}

Because retrieval is exposed as a tool, the agent grounds its answers in your data instead of hallucinating—dramatically improving accuracy for enterprise use cases like support bots and internal assistants.

Conclusion: Key Takeaways

You now have a complete mental model for building AI agents in C# with Semantic Kernel. Instead of writing rigid, hard-coded workflows, you give an LLM a set of well-described tools and let it reason its way to the goal autonomously.

  • Semantic Kernel gives .NET developers a first-class way to build autonomous AI agents with full type safety and DI support.
  • Plugins with clear [Description] attributes are the agent's tools—descriptions drive behavior.
  • FunctionChoiceBehavior.Auto() turns a chat model into a task-solving agent that plans and calls functions on its own.
  • The Agent Framework (ChatCompletionAgent) enables persistent, named, and multi-agent systems.
  • Follow best practices—validate inputs, cap iterations, secure keys, and add telemetry—to move from demo to production.

The best way to master building AI agents in C# is to start small: create one plugin, enable auto function calling, and watch your agent reason. From there, add memory, RAG, and multiple collaborating agents. Semantic Kernel gives you everything you need to build powerful, autonomous AI agents in the .NET ecosystem—so open your IDE and start building today.

About csharp-coder.com
Your go-to resource for C#, .NET, and modern software development. Follow along for daily tutorials, tips, and real-world examples.

Comments

Popular posts from this blog

.NET MAUI Tutorial 2026: Build Cross-Platform Apps in C#

Learn .NET MAUI in 2026 to build iOS, Android, Windows & Mac apps from one C# codebase. Start this cross-platform tutorial with code examples today. .NET MAUI (Multi-platform App UI) is Microsoft's framework for building native iOS, Android, Windows, and macOS apps from a single C# codebase . If you've ever wanted to ship a mobile app without learning Swift, Kotlin, and Win32 separately, this .NET MAUI tutorial for 2026 is your starting point. In this guide you'll learn what .NET MAUI is, why it matters for cross-platform app development in C#, and how to build your first working app — with runnable code examples and the best practices senior engineers actually use in production. What Is .NET MAUI and Why Use It in 2026? .NET MAUI is the evolution of Xamarin.Forms, fully integrated into the modern .NET runtime. With one project and one language — C# — you target four platforms. The framework compiles to native UI controls on each device, so a button on iOS...

Angular 14 : 404 error during refresh page after deployment

In this article, We will learn how to solve 404 file or directory not found angular error in production.  Refresh browser angular 404 file or directory not found error You have built an Angular app and created a production build with ng build --prod You deploy it to a production server. Everything works fine until you refresh the page. The app throws The requested URL was not found on this server message (Status code 404 not found). It appears that angular routing not working on the production server when you refresh the page. The error appears on the following scenarios When you type the URL directly in the address bar. When you refresh the page The error appears on all the pages except the root page.   Reason for the requested URL was not found on this server error In a Multi-page web application, every time the application needs to display a page it has to send a request to the web server. You can do that by either typing the URL in the address bar, clicking on the Me...

Angular 14 CRUD Operation with Web API .Net 6.0

How to Perform CRUD Operation Using Angular 14 In this article, we will learn the angular crud (create, read, update, delete) tutorial with ASP.NET Core 6 web API. We will use the SQL Server database and responsive user interface for our Web app, we will use the Bootstrap 5. Let's start step by step. Step 1 - Create Database and Web API First we need to create Employee database in SQL Server and web API to communicate with database. so you can use my previous article CRUD operations in web API using net 6.0 to create web API step by step. As you can see, after creating all the required API and database, our API creation part is completed. Now we have to do the angular part like installing angular CLI, creating angular 14 project, command for building and running angular application...etc. Step 2 - Install Angular CLI Now we have to install angular CLI into our system. If you have already installed angular CLI into your system then skip this step.  To install angular CLI ope...