.NET 9 and Microsoft.Extensions.AI: the new way to integrate AI into your applications
Microsoft.Extensions.AI is the library you've been waiting for to integrate AI into your .NET applications without locking yourself into a specific provider. With .NET 9, you get a unified API, clean abstractions and an ecosystem you already know.
Fernando Luján
Fullstack Developer • • 5 min read
Contents
Why is .NET 9 a turning point for AI development?
Generative AI has gone from experimental technology to a core component of many modern applications. Until now, the problem was always the same: integrating a large language model (LLM) into your .NET application meant dealing with provider-specific SDKs, tightly coupled logic and code that was hard to test or replace.
With the release of .NET 9 in November 2024, Microsoft gave a clear answer to that problem: Microsoft.Extensions.AI.
What is Microsoft.Extensions.AI?
Microsoft.Extensions.AI is a set of .NET libraries that provide unified abstractions for integrating AI services, both chat models and embedding generation, into .NET applications and libraries.
If you've worked with Microsoft.Extensions.Logging or Microsoft.Extensions.DependencyInjection, the concept will feel familiar right away:
Extension | Main abstraction |
|---|---|
|
|
|
|
|
|
|
|
The library is easy to install via NuGet and plugs into the dependency injection pipeline you already know.
The two key abstractions
IChatClientThis is the core interface for interacting with language models. It lets you build chat clients that work with any backend (OpenAI, Azure OpenAI, local models) without changing your application code.
Implementing IChatClient gives you:
Structured message exchange
Support for streaming responses
Tool calling
Compatibility with logging and telemetry middleware
IEmbeddingGeneratorIt abstracts embedding generation: vector representations of text or images that capture semantic concepts. They are the building block for use cases such as semantic search, document similarity and RAG (Retrieval-Augmented Generation) systems.
Why does the abstraction matter?
The key to this library isn't just simpler code; it's the design model it proposes. Just like with logging or DI, your application code doesn't depend on a specific provider.
That means you can:
Develop against a local model and deploy with Azure OpenAI without touching your business code.
Test with mocks of
IChatClientwithout making real API calls.Switch providers when a better one comes along, without refactoring the whole application.
Add middleware (caching, telemetry, rate limiting) in a declarative, reusable way.
Supported use cases
Microsoft.Extensions.AI is designed to cover the most common usage patterns in modern AI applications:
Chat and conversation: From simple chatbots to assistants with conversation history and a defined personality.
Retrieval-Augmented Generation (RAG): Connect your AI systems to your own business data. Combining IEmbeddingGenerator + vector search lets the model answer with context specific to your organization.
Image processing: AI applications can go beyond text. The library supports visual inputs to extract structured information from visual data.
Functions and integration with external systems: Functions (tool calling) give the AI system the ability to perform concrete actions: query a database, call an API, run business logic.
Benefits at a glance
Unified API: A consistent set of conventions for integrating any AI service. Learn it once, apply it everywhere.
Provider flexibility: You write code against the abstraction, not against OpenAI or Azure. The provider is a configuration detail.
Easy experimentation: Try different models and providers using the same underlying abstractions. Experimenting doesn't mean rewriting.
Componentization and testing: Standard middleware for caching, telemetry and tool invocation. Components that are easy to isolate, test and reuse.
Implementation examples
Basic IChatClient usage
First, install the required packages:
dotnet add package Microsoft.Extensions.AI
dotnet add package Microsoft.Extensions.AI.OpenAIThen register the client in the DI container, just like any other service:
// Program.cs
builder.Services.AddChatClient(new OpenAIClient("tu-api-key")
.AsChatClient(modelId: "gpt-4o-mini"));And use it in any class by injecting IChatClient, without knowing anything about OpenAI:
public class AssistantService(IChatClient chatClient)
{
public async Task<string> AskAsync(string question)
{
var response = await chatClient.CompleteAsync(question);
return response.Message.Text ?? string.Empty;
}
}To switch from OpenAI to Azure OpenAI, you only change the registration. The rest of the code stays untouched:
// Switching providers: only this line changes
builder.Services.AddChatClient(new AzureOpenAIClient(
new Uri("https://tu-recurso.openai.azure.com/"),
new DefaultAzureCredential())
.AsChatClient(deploymentName: "gpt-4o-mini"));Adding telemetry and caching middleware
One of the most tangible advantages is being able to chain behaviors without modifying your logic:
builder.Services.AddChatClient(inner =>
inner
.UseLogging() // logs every call automatically
.UseOpenTelemetry() // metrics and traces ready to export
.UseDistributedCache() // caches responses for repeated prompts
.Use(new OpenAIClient("tu-api-key").AsChatClient("gpt-4o-mini")));Generating embeddings with IEmbeddingGenerator
builder.Services.AddEmbeddingGenerator(
new OpenAIClient("tu-api-key")
.AsEmbeddingGenerator(modelId: "text-embedding-3-small"));
// In your service:
public class SemanticSearchService(IEmbeddingGenerator<string, Embedding<float>> embedder)
{
public async Task<float[]> GetVectorAsync(string text)
{
var result = await embedder.GenerateAsync(text);
return result[0].Vector.ToArray();
}
}These vectors are the foundation for implementing semantic search or RAG systems on top of your own database.
Where to start?
If you want to explore Microsoft.Extensions.AI in your own environment, the official starting point is:
📦 Download .NET 9: aka.ms/get-dotnet-9
From there, Microsoft's official documentation and the Microsoft.Extensions.AI and Microsoft.Extensions.AI.Abstractions NuGet packages are the natural entry point.
Conclusion
Microsoft.Extensions.AI isn't just another library for calling GPT. It's a design bet: that AI in .NET applications should follow the same principles of abstraction, composition and testability that already govern the rest of the ecosystem.
For teams working with .NET, this means being able to adopt AI incrementally, without hard dependencies and with the confidence that the code will remain maintainable over the long term.
At Devlights, we bring these kinds of architecture decisions into our day-to-day work with clients. If you're thinking about how to bring AI into your .NET applications in a sustainable way, let's talk.
About this DevTalk
Fernando Luján
Fullstack Developer


