Introduction
AI agents are transforming how modern applications automate tasks, make decisions, and interact with users. Unlike traditional chatbots that simply respond to prompts, AI agents can plan actions, invoke external tools, and complete multi-step workflows.
Microsoft Semantic Kernel simplifies AI application development in .NET by providing a unified programming model for integrating Large Language Models (LLMs), plugins, memory, and orchestration. In this article, you'll build a simple AI agent using .NET 10 and learn the production practices required to deploy it in real-world applications.
What Are AI Agents?
An AI agent is an application that can:
Understand user requests.
Make decisions based on context.
Execute functions or APIs.
Generate intelligent responses.
Complete tasks autonomously.
Semantic Kernel provides the building blocks for creating these agents without managing complex prompt orchestration manually.
Prerequisites
Before getting started, ensure you have:
.NET 10 SDK (Preview or later)
Visual Studio 2022 or Visual Studio Code
An OpenAI or Azure OpenAI API key
Basic knowledge of C#
Install the required NuGet packages:
dotnet add package Microsoft.SemanticKernel
dotnet add package Microsoft.SemanticKernel.Connectors.OpenAI
Building Your First AI Agent
The following example creates a simple assistant capable of answering developer questions.
using Microsoft.SemanticKernel;
var builder = Kernel.CreateBuilder();
builder.AddOpenAIChatCompletion(
modelId: "gpt-4.1",
apiKey: Environment.GetEnvironmentVariable("OPENAI_API_KEY"));
var kernel = builder.Build();
var result = await kernel.InvokePromptAsync(
"Explain Dependency Injection in ASP.NET Core in simple terms.");
Console.WriteLine(result);
This example creates a Semantic Kernel instance, connects to an AI model, sends a prompt, and prints the response. While simple, the same pattern scales to production applications where agents can invoke plugins, query databases, or interact with external services.
Production Considerations
Building a working prototype is easy. Building a reliable AI agent requires production-ready practices.
Dependency Injection
Avoid creating a new Kernel instance for every request. Register Semantic Kernel through ASP.NET Core's dependency injection container so it can be reused across your application.
This improves maintainability, simplifies testing, and keeps your AI services centrally configured.
Configuration
Never hardcode API keys or model names.
Store configuration in appsettings.json or environment variables.
{
"OpenAI": {
"Model": "gpt-4.1"
}
}
Keep secrets in Azure Key Vault, Secret Manager, or your preferred secret store instead of source control.
Logging
AI applications can fail due to rate limits, network issues, or invalid prompts.
Use ILogger<T> to record:
Request duration
Errors
Retry attempts
Token usage (when available)
Avoid logging sensitive prompts or personal information.
Error Handling
Wrap AI calls inside try-catch blocks to handle transient failures gracefully.
try
{
var response = await kernel.InvokePromptAsync(prompt);
}
catch(Exception ex)
{
logger.LogError(ex, "AI request failed.");
}
For production workloads, implement retry policies with exponential backoff to handle temporary service failures.
Security
Security becomes increasingly important when AI interacts with business data.
Follow these practices:
Join the conversation! Your thoughts help the community grow.