Featured image of post How to Create AI Agents Using Semantic Kernel’s Agentic Framework

How to Create AI Agents Using Semantic Kernel’s Agentic Framework

Leverage the power of Microsoft Semantic Kernel to build smart, modular, and scalable AI agents with C#. This blog walks you through building your first AI agent using the Agentic Framework.

🔍 What is the Semantic Kernel Agentic Framework?

Microsoft’s Semantic Kernel is an open-source SDK designed to simplify integrating AI into your applications. The Agentic Framework within Semantic Kernel helps you build intelligent, task-driven agents that:

  • Autonomously plan and execute tasks
  • Use plugins (functionality modules)
  • Interact via AI services (like OpenAI or Azure OpenAI)
  • Maintain conversation context

The agents are powered by LLMs and enriched through planning, memory, function calling, and more.


🛠️ Prerequisites

To get started, ensure you have:

  • .NET 8 SDK
  • Visual Studio Code or Visual Studio
  • OpenAI or Azure OpenAI API keys
  • Basic knowledge of C#

Install the core Semantic Kernel SDK:

1
2
dotnet add package Microsoft.SemanticKernel
dotnet add package Microsoft.SemanticKernel.Agents.OpenAI

🧱 Architecture Overview

At its core, an AI agent in Semantic Kernel involves:

  1. Kernel – The runtime for all services and plugins.
  2. Plugins – Reusable sets of functions exposed to the LLM.
  3. Agent – A class implementing IAgent, capable of interacting via ReceiveAsync.
  4. Planner (Optional) – For complex, multi-step goals.
  5. Chat Completion – Connects to an LLM (e.g., GPT-4).

🚀 Step-by-Step: Building an Agent

1. 🔌 Define a Plugin

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
using Microsoft.SemanticKernel;
using System.ComponentModel;

public class WeatherPlugin
{
    [KernelFunction("get_weather")]
    [Description("Gets today's weather for a specific city.")]
    public string GetWeather(string city)
    {
        return $"The weather in {city} is sunny and 25°C.";
    }
}

2. 🧠 Set Up the Kernel

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using Microsoft.Extensions.Logging;

var builder = Kernel.CreateBuilder();
builder.AddOpenAIChatCompletion("gpt-4", "<your-api-key>"); // Or AzureOpenAI
builder.Plugins.AddFromType<WeatherPlugin>();

builder.Services.AddLogging(logging =>
    logging.AddConsole().SetMinimumLevel(LogLevel.Information));

var kernel = builder.Build();

3. 🤖 Create a Custom Agent

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using Microsoft.SemanticKernel.Agents;
using Microsoft.SemanticKernel.ChatCompletion;

public class WeatherAgent : IAgent
{
    public string Name => "Weather Agent";
    public string Description => "I can tell you the weather for any city.";
    private readonly Kernel _kernel;
    private readonly IChatCompletionService _chat;

    public WeatherAgent(Kernel kernel)
    {
        _kernel = kernel;
        _chat = kernel.GetRequiredService<IChatCompletionService>();
    }

    public async Task<AgentThought> ReceiveAsync(AgentThought input, Kernel kernel, CancellationToken cancellationToken = default)
    {
        var history = new ChatHistory();
        history.AddUserMessage(input.Input);

        var response = await _chat.GetChatMessageContentAsync(history, kernel: _kernel, cancellationToken: cancellationToken);

        return new AgentThought(response?.Content ?? "I'm not sure.");
    }
}

4. 🚪 Run the Agent

1
2
3
4
5
var agent = new WeatherAgent(kernel);
var input = new AgentThought("What's the weather like in Delhi?");
var response = await agent.ReceiveAsync(input, kernel);

Console.WriteLine($"🤖 Agent: {response.Input}");

🧠 What Just Happened?

  • You registered a plugin (WeatherPlugin) with a function the model can call.
  • Created an Agent class (WeatherAgent) that wraps the kernel.
  • Sent a message (AgentThought) to the agent, who routed it through the kernel and got a response from the LLM.
  • The model auto-selected get_weather and generated a meaningful reply.

📎 Optional: Enable Planning

Want your agent to plan steps before answering?

Add the planner using:

1
dotnet add package Microsoft.SemanticKernel.Planning.Handlebars

Configure like this:

1
2
3
4
5
6
using Microsoft.SemanticKernel.Planning.Handlebars;

var planner = new HandlebarsPlanner(kernel);

var plan = await planner.CreatePlanAsync("Tell me the weather and suggest an outfit.");
await plan.InvokeAsync(kernel);

🔐 Best Practices

  • Keep plugin methods descriptive with [Description] for better LLM comprehension.
  • Limit token size in plugins to avoid context overflow.
  • Use FunctionChoiceBehavior.Auto() to let LLM decide what to call.
  • Add filters or logging for observability.

📚 Conclusion

The Agentic Framework in Semantic Kernel brings powerful abstractions for building AI-native applications. You can create rich AI agents capable of decision-making, task execution, and integrating into real-world systems.

Whether you’re building a chatbot, automation tool, or an autonomous assistant — Semantic Kernel provides all the primitives you need.

comments powered by Disqus