Semantic Kernel: Microsoft’s Agent Framework
What This Article Covers
- What Semantic Kernel is and how it works.
- Building agents with Semantic Kernel.
- Plugins, planners, and memory for agents.
- Real-world examples across different use cases.
- Best practices for enterprise integration and production.
Introduction: Understanding Semantic Kernel
Semantic Kernel is Microsoft’s framework for building AI agents. It connects LLMs with plugins (tools), planners (orchestration), and memory (context). Designed for enterprise applications in .NET and Python.
This article is for developers looking to build agents with Semantic Kernel. For foundational concepts, see AI Agent Frameworks and LangChain.
Why Use Semantic Kernel?
Consider building an enterprise agent that needs to work with your CRM, ERP, and internal APIs. Semantic Kernel provides enterprise integration, planners for complex workflows, and memory management for context. For Microsoft-based technology stacks, it’s the natural choice.
Semantic Kernel at a Glance
Semantic Kernel is Microsoft’s agent framework. It provides plugins for tooling, planners for orchestration, and memory for context management. Built for .NET and Python with an enterprise focus.
The core idea: enterprise-grade infrastructure for AI agents.
Who This Article Is For
- .NET developers building agents.
- Enterprise teams running on Microsoft stacks.
- Python developers using SK for agents.
- DevOps engineers deploying agents in enterprise environments.
Key Concepts
- Semantic Kernel - Microsoft’s agent framework. Use when: building enterprise agents.
- Plugin - Tool or integration. Use when: adding functionality.
- Planner - Orchestration engine. Use when: handling complex workflows.
- Memory - Context storage. Use when: maintaining state.
- Ollama - Local model server. Use when: running local models.
Setup
Python
pip install semantic-kernel
from semantic_kernel import Kernel
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
# Create kernel
kernel = Kernel()
# Add LLM (Ollama)
kernel.add_service(OpenAIChatCompletion(
service_id="ollama",
ai_model_id="llama3.1",
endpoint="http://ollama:11434/v1",
api_key="not-needed"
))
C# / .NET
using Microsoft.SemanticKernel;
// Create kernel
var kernel = Kernel.CreateBuilder()
.AddOpenAIChatCompletion(
modelId: "llama3.1",
endpoint: new Uri("http://ollama:11434/v1"),
apiKey: "not-needed")
.Build();
Example 1: Creating a Plugin
from semantic_kernel.functions import kernel_function
class MyPlugin:
"""Custom plugin for Semantic Kernel"""
@kernel_function(
name="get_weather",
description="Fetch weather information"
)
def get_weather(self, location: str) -> str:
"""Get weather for a location"""
# API call
return f"Weather in {location}: 22°C, sunny"
@kernel_function(
name="send_email",
description="Send an email"
)
def send_email(self, to: str, subject: str, body: str) -> str:
"""Send an email"""
# Email logic
return f"Email sent to {to}"
# Add plugin to kernel
kernel.add_plugin(MyPlugin(), plugin_name="my_plugin")
Example 2: Planner for Complex Workflows
from semantic_kernel.planners import SequentialPlanner
# Create planner
planner = SequentialPlanner(kernel)
# Define goal
goal = "Research the topic, analyze the data, and write a report."
# Create plan
plan = await planner.create_plan_async(goal)
# Execute plan
result = await plan.invoke_async()
Example 3: Memory for Context
from semantic_kernel.memory import SemanticTextMemory
from semantic_kernel.connectors.ai.open_ai import OpenAITextEmbedding
# Add memory
memory = SemanticTextMemory(
storage=VolatileMemoryStore(),
embeddings_generator=OpenAITextEmbedding(
service_id="embeddings",
ai_model_id="text-embedding-ada-002",
endpoint="http://ollama:11434/v1",
api_key="not-needed"
)
)
# Store information
await memory.save_information_async(
collection="documents",
text="Customer Max Mustermann placed an order on 15.03.",
id="doc1"
)
# Retrieve information
results = await memory.search_async(
collection="documents",
query="When did Max place an order?"
)
Example 4: Agent with Plugins and Memory
class SemanticKernelAgent:
"""Agent built with Semantic Kernel"""
def __init__(self):
self.kernel = Kernel()
self.setup_services()
self.setup_plugins()
self.setup_memory()
async def run(self, task):
"""Agent loop"""
# Retrieve memory (context)
context = await self.memory.search_async(
collection="context",
query=task
)
# Invoke plugin or LLM directly
if self.needs_tool(task):
result = await self.kernel.invoke_async(
plugin_name="my_plugin",
function_name="relevant_function",
task=task,
context=context
)
else:
result = await self.llm.complete_async(task, context)
# Save to memory
await self.memory.save_information_async(
collection="context",
text=f"Task: {task}, Result: {result}",
id=f"task_{datetime.now().isoformat()}"
)
return result
Semantic Kernel vs. LangChain
| Aspect | Semantic Kernel | LangChain |
|---|---|---|
| Languages | C#, Python | Python, JavaScript |
| Focus | Enterprise | General purpose |
| Planner | Yes, dedicated | No, manual |
| Memory | Yes, dedicated | Yes, various options |
| Integration | Microsoft Stack | Many integrations |
| Community | Growing | Large |
| Best for | Enterprise, .NET | General agents |
Security Considerations
- API Keys: For OpenAI APIs, use environment variables, never hardcode.
- Plugin Permissions: Plugins should validate permissions. See MCP Permissions.
- Memory Security: Memory may contain sensitive data. Store securely.
- Audit: Log all agent actions. See Audit Logging.
Common Pitfalls
- Complex Planners: Planners can generate intricate, hard-to-understand execution plans. Keep goals simple.
- Memory Growth: Memory can grow large over time. Plan a cleanup strategy.
- Plugin Dependencies: Plugins may depend on external services. Document these.
- Enterprise vs. Open Source: SK is enterprise-focused. For open-source projects, LangChain or CrewAI may be better fits.
- .NET vs. Python: SK is stronger in .NET. For Python-only projects, LangChain may be more suitable.
Further Reading
- Semantic Kernel - GitHub.
- AI Agent Frameworks - Overview.
- LangChain - Alternative.
- CrewAI - Multi-agent framework.
- LlamaIndex - For RAG.
- Ollama - Model server.
Key Takeaways:
- Semantic Kernel is Microsoft’s enterprise agent framework.
- Plugins provide tooling, planners handle orchestration, memory manages context.
- Built for .NET and Python with an enterprise focus.
- Ideal for Microsoft-based stacks and enterprise applications.
- For open-source projects, LangChain or CrewAI might be better choices.
FAQ
What is Semantic Kernel?
Semantic Kernel or LangChain?
What are plugins?
What is a planner?
What is memory?
Do I need .NET?
Is SK suitable for enterprise?
How much does Semantic Kernel cost?
Sources and Further Reading
- Semantic Kernel - GitHub.
- Semantic Kernel Docs - Documentation.
- Microsoft Copilot - Copilot integration.


