Artificial Intelligence is rapidly transforming how modern applications are built. Instead of just writing static logic, developers can now create intelligent systems that can understand, reason, and act. One of the most powerful tools for this in the .NET ecosystem is Semantic Kernel.
Semantic Kernel is an open-source SDK by Microsoft that helps developers integrate Large Language Models (LLMs) like OpenAI into their applications. It allows you to build AI agents that can perform tasks, automate workflows, and make decisions using natural language.
In this guide, we will understand what Semantic Kernel is, how it works, and how you can build your own AI agent using C# in a simple and practical way.
What is Semantic Kernel?
Semantic Kernel is a lightweight SDK that acts as a bridge between your C# application and AI models.
Instead of directly calling APIs again and again, Semantic Kernel provides a structured way to:
Connect to AI models (OpenAI / Azure OpenAI)
Create reusable AI functions (called skills or plugins)
Combine AI + traditional code logic
Build intelligent workflows
In simple words, Semantic Kernel helps you mix "AI thinking" with "normal programming logic."
Key Concepts of Semantic Kernel
To understand Semantic Kernel properly, you need to know a few core concepts.
1. Kernel
The Kernel is the central engine.
It manages:
AI service connections
Plugins (functions)
Execution flow
Think of it as the brain of your AI application.
2. Plugins (Skills)
Plugins are reusable functions.
They can be:
Example:
Summarize text
Translate language
Generate email
3. Prompts
Prompts define how the AI should behave.
Example:
"Summarize this text in 3 lines"
Semantic Kernel allows you to manage prompts cleanly instead of hardcoding them everywhere.
4. Memory
Memory allows your AI agent to remember past interactions.
This is useful for:
5. Planner (AI Agent Brain)
Planner is what makes your app an AI agent.
It decides:
What steps to take
Which function to call
In what order
This enables automation without hardcoding every step.
Why Use Semantic Kernel in .NET?
Here are some strong reasons:
Clean architecture for AI integration
Reusable AI components
Supports OpenAI and Azure OpenAI
Easy to combine with existing .NET apps
Helps build AI agents instead of simple API calls
Step-by-Step: Build an AI Agent Using Semantic Kernel in C#
Now let’s build a simple AI agent step by step.
Step 1: Create a .NET Project
dotnet new console -n SemanticKernelDemo
cd SemanticKernelDemo
Step 2: Install Semantic Kernel Package
dotnet add package Microsoft.SemanticKernel
Step 3: Configure OpenAI or Azure OpenAI
Add your API key in appsettings.json:
{
"OpenAI": {
"ApiKey": "your-api-key",
"Model": "gpt-4o-mini"
}
}
Step 4: Initialize the Kernel
using Microsoft.SemanticKernel;
var builder = Kernel.CreateBuilder();
builder.AddOpenAIChatCompletion(
modelId: "gpt-4o-mini",
apiKey: "your-api-key"
);
var kernel = builder.Build();
This connects your application with the AI model.
Step 5: Create a Simple AI Function (Prompt-Based)
var prompt = "Summarize the following text in simple words: {{$input}}";
var summarizeFunction = kernel.CreateFunctionFromPrompt(prompt);
var result = await kernel.InvokeAsync(summarizeFunction, new()
{
["input"] = "Artificial Intelligence is transforming the world..."
});
Console.WriteLine(result);
This is your first semantic function.
Step 6: Create a Native Plugin (C# Function)
public class MathPlugin
{
public int Add(int a, int b)
{
return a + b;
}
}
Register it:
kernel.Plugins.AddFromType<MathPlugin>();
Now your AI agent can use real C# logic.
Step 7: Build an AI Agent with Planner
Planner allows the AI to decide actions automatically.
using Microsoft.SemanticKernel.Planning;
var planner = new FunctionCallingStepwisePlanner();
var goal = "Summarize a paragraph and then translate it into Hindi";
var plan = await planner.CreatePlanAsync(kernel, goal);
var result = await planner.ExecuteAsync(kernel, plan);
Console.WriteLine(result);
Now your system behaves like an AI agent instead of a simple function.
Step 8: Add Memory (Optional but Powerful)
builder.Services.AddSingleton<IMemoryStore, VolatileMemoryStore>();
Memory helps your agent remember previous conversations.
Step 9: Real-World Example: AI Assistant
Let’s combine everything.
Use case:
Console.WriteLine("Ask something:");
var input = Console.ReadLine();
var response = await kernel.InvokePromptAsync(input);
Console.WriteLine(response);
This creates a simple AI assistant.
Best Practices for Semantic Kernel
Keep prompts clear and specific
Avoid very long inputs (token limits)
Use plugins for reusable logic
Secure API keys using environment variables
Add logging and error handling
Difference Between Traditional Code vs AI Agent
| Feature | Traditional Code | AI Agent (Semantic Kernel) |
|---|
| Logic | Fixed | Dynamic |
| Decision Making | Manual | AI-driven |
| Flexibility | Low | High |
| Automation | Limited | Advanced |
Real-World Use Cases
You can build:
Conclusion
Semantic Kernel is a powerful tool for .NET developers who want to build intelligent AI applications. Instead of writing complex logic manually, you can let AI handle reasoning and decision-making.
By combining prompts, plugins, memory, and planners, you can build full AI agents using C# in a clean and scalable way.
Start with small experiments, then gradually build advanced AI-powered systems that can automate real-world tasks efficiently.