Semantic Kernel: AI Orchestration for .NET
Semantic Kernel is Microsoft's open-source SDK for integrating Large Language Models (LLMs) into your applications. Think of it as the "middleware" between your app and AI models — handling plugins, memory, planning, and function calling.
Why Semantic Kernel?
- Official Microsoft SDK for building AI agents in .NET (also Python & Java)
- Model agnostic — Works with Azure OpenAI, OpenAI, Hugging Face, local models
- Enterprise ready — Telemetry, dependency injection, async-first
- Used by Microsoft — Powers Copilot features internally
Getting Started
Install the NuGet Package
dotnet add package Microsoft.SemanticKernel
Create the Kernel
using Microsoft.SemanticKernel;
var builder = Kernel.CreateBuilder();
builder.AddAzureOpenAIChatCompletion(
deploymentName: "gpt-4o",
endpoint: "https://my-openai.openai.azure.com/",
apiKey: "your-key"
);
var kernel = builder.Build();
Simple Chat
var response = await kernel.InvokePromptAsync(
"What are the benefits of microservices architecture?"
);
Console.WriteLine(response);
Plugins: Extending the AI
Plugins give the AI access to your application's capabilities.
Native Functions (C# methods)
public class WeatherPlugin
{
[KernelFunction("get_weather")]
[Description("Get the current weather for a city")]
public async Task<string> GetWeatherAsync(
[Description("The city name")] string city)
{
// Call your weather API
var weather = await _weatherService.GetCurrentAsync(city);
return $"{city}: {weather.Temp}°C, {weather.Condition}";
}
}
// Register the plugin
kernel.Plugins.AddFromType<WeatherPlugin>();
Prompt Functions (AI-powered)
var summarize = kernel.CreateFunctionFromPrompt(
"Summarize the following text in {{$maxSentences}} sentences: {{$input}}",
new OpenAIPromptExecutionSettings { MaxTokens = 500 }
);
var result = await kernel.InvokeAsync(summarize, new()
{
["input"] = longDocument,
["maxSentences"] = "3"
});
Auto Function Calling
The most powerful feature — let the AI decide which plugins to use:
using Microsoft.SemanticKernel.Connectors.OpenAI;
// Register plugins
kernel.Plugins.AddFromType<WeatherPlugin>();
kernel.Plugins.AddFromType<CalendarPlugin>();
kernel.Plugins.AddFromType<EmailPlugin>();
// Enable automatic function calling
var settings = new OpenAIPromptExecutionSettings
{
FunctionChoiceBehavior = FunctionChoiceBehavior.Auto()
};
var chatService = kernel.GetRequiredService<IChatCompletionService>();
var history = new ChatHistory();
history.AddUserMessage(
"Check tomorrow's weather in Seattle and if it's rainy, " +
"email the team to reschedule the outdoor event."
);
// The AI will automatically:
// 1. Call GetWeather("Seattle")
// 2. Check if it's rainy
// 3. Call SendEmail() to the team if needed
var response = await chatService.GetChatMessageContentAsync(
history, settings, kernel
);
Memory & Embeddings
Add long-term memory to your AI using vector stores:
using Microsoft.SemanticKernel.Memory;
// Configure memory with Azure AI Search
var memoryBuilder = new MemoryBuilder();
memoryBuilder.WithAzureOpenAITextEmbeddingGeneration(
"text-embedding-ada-002", endpoint, apiKey
);
memoryBuilder.WithAzureAISearchMemoryStore(searchEndpoint, searchApiKey);
var memory = memoryBuilder.Build();
// Save information
await memory.SaveInformationAsync("company-policies",
id: "refund-policy",
text: "Customers can request a full refund within 30 days of purchase..."
);
// Recall relevant information
var results = memory.SearchAsync("company-policies",
"What is the refund policy?", limit: 3
);
Multi-Agent Patterns
Orchestrate multiple specialized agents:
using Microsoft.SemanticKernel.Agents;
// Define agents
var researcher = new ChatCompletionAgent
{
Name = "Researcher",
Instructions = "You research topics and provide detailed findings.",
Kernel = kernel
};
var writer = new ChatCompletionAgent
{
Name = "Writer",
Instructions = "You write polished articles based on research findings.",
Kernel = kernel
};
// Create a group chat
var chat = new AgentGroupChat(researcher, writer)
{
ExecutionSettings = new()
{
TerminationStrategy = new MaxTurnsTerminationStrategy(6)
}
};
chat.AddChatMessage(new ChatMessageContent(AuthorRole.User,
"Write a blog post about the future of AI in healthcare"));
await foreach (var message in chat.InvokeAsync())
{
Console.WriteLine($"{message.AuthorName}: {message.Content}");
}
Resources
Video: Watch the Semantic Kernel getting started series on Microsoft Developer YouTube.


