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.
At its core, an AI agent in Semantic Kernel involves:
Kernel – The runtime for all services and plugins.
Plugins – Reusable sets of functions exposed to the LLM.
Agent – A class implementing IAgent, capable of interacting via ReceiveAsync.
Planner (Optional) – For complex, multi-step goals.
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
usingMicrosoft.SemanticKernel;usingSystem.ComponentModel;publicclassWeatherPlugin{ [KernelFunction("get_weather")] [Description("Gets today's weather for a specific city.")]publicstringGetWeather(stringcity){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
usingMicrosoft.SemanticKernel;usingMicrosoft.SemanticKernel.ChatCompletion;usingMicrosoft.SemanticKernel.Connectors.OpenAI;usingMicrosoft.Extensions.Logging;varbuilder=Kernel.CreateBuilder();builder.AddOpenAIChatCompletion("gpt-4","<your-api-key>");// Or AzureOpenAIbuilder.Plugins.AddFromType<WeatherPlugin>();builder.Services.AddLogging(logging=>logging.AddConsole().SetMinimumLevel(LogLevel.Information));varkernel=builder.Build();
usingMicrosoft.SemanticKernel.Agents;usingMicrosoft.SemanticKernel.ChatCompletion;publicclassWeatherAgent:IAgent{publicstringName=>"Weather Agent";publicstringDescription=>"I can tell you the weather for any city.";privatereadonlyKernel_kernel;privatereadonlyIChatCompletionService_chat;publicWeatherAgent(Kernelkernel){_kernel=kernel;_chat=kernel.GetRequiredService<IChatCompletionService>();}publicasyncTask<AgentThought>ReceiveAsync(AgentThoughtinput,Kernelkernel,CancellationTokencancellationToken=default){varhistory=newChatHistory();history.AddUserMessage(input.Input);varresponse=await_chat.GetChatMessageContentAsync(history,kernel:_kernel,cancellationToken:cancellationToken);returnnewAgentThought(response?.Content??"I'm not sure.");}}
4. 🚪 Run the Agent
1
2
3
4
5
varagent=newWeatherAgent(kernel);varinput=newAgentThought("What's the weather like in Delhi?");varresponse=awaitagent.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.
usingMicrosoft.SemanticKernel.Planning.Handlebars;varplanner=newHandlebarsPlanner(kernel);varplan=awaitplanner.CreatePlanAsync("Tell me the weather and suggest an outfit.");awaitplan.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.