Guides

Building an AI backend with C# and .NET: architecture and libraries in 2026

How to choose libraries and architecture for an AI backend in C# and .NET in 2026: Microsoft.Extensions.AI, Microsoft Agent Framework, the OpenAI and Anthropic SDKs, MCP, ONNX Runtime, plus streaming, resilience, OpenTelemetry, testing and cost.

Summary: what to use for a C# AI backend in 2026

For most AI backends written in C#, start with Microsoft.Extensions.AI (the IChatClient abstraction shared across model providers), add Microsoft Agent Framework only when the problem is genuinely a multi-step agent, and use the official OpenAI or Anthropic SDK as the connection layer underneath. The rest is familiar .NET backend engineering: dependency injection, Polly for resilience, OpenTelemetry for observability and automated tests.

That is also the order Microsoft recommends on its .NET + AI ecosystem overview: start with Microsoft.Extensions.AI for most app-level AI features, add DataIngestion and VectorData when grounding answers in your own data, use MCP when capabilities must be shared across process or product boundaries, and move to Agent Framework when one-step prompts become multi-step workflows.

This guide is written by Phan Phúc Nguyên, Director of EYC, who works primarily in C#/.NET. Every package and API name below was checked against Microsoft Learn, GitHub or NuGet at the time of writing (October 2026); the ecosystem moves fast, so pin versions and read release notes before upgrading.

The .NET AI library map (status as of October 2026)

The table groups the main components by role. The key mental model: Microsoft.Extensions.AI is the abstraction layer; Agent Framework is the orchestration layer; provider SDKs are the connection layer; ONNX Runtime and ML.NET are for models you run yourself.

Main libraries for an AI backend on .NET
ComponentRoleVerified statusWhen to use
Microsoft.Extensions.AI (MEAI)Abstractions: IChatClient, IEmbeddingGenerator, middleware (function invocation, caching, telemetry)Abstractions package at 10.x on Microsoft LearnDefault for any app-level AI feature
Microsoft Agent Framework (MAF)Agent orchestration, graph workflows, state, human-in-the-loopDirect successor to Semantic Kernel and AutoGen per Microsoft Learn; Microsoft.Agents.AI has been stable on NuGet since 1.0.0 (2 April 2026)Multi-step agents, handoffs between agents
Semantic KernelPrevious-generation orchestration SDKMicrosoft calls Agent Framework its next generation; official migration guide existsExisting projects; new projects should consider MAF
OpenAI (NuGet: OpenAI)Official SDK for the OpenAI APIopenai/openai-dotnet repo, .NET Standard 2.0 compatibleOpenAI models or compatible endpoints
Anthropic (NuGet: Anthropic)Official SDK for the Claude APIOfficial from version 10 onward; requires .NET Standard 2.0+Claude models
ModelContextProtocolOfficial C# SDK for MCP clients and serversStable 2.2.0 on NuGet; maintained in collaboration with MicrosoftTools shared across agents, IDEs and products
ONNX Runtime (Microsoft.ML.OnnxRuntime)Local inference for ONNX modelsC# API; runs on Linux, Windows, macOSSmall models, classification, embeddings on your own hardware
ML.NETTrain and consume classic ML models in .NETOpen source, cross-platform, can consume ONNX modelsForecasting and classification on tabular data

Microsoft.Extensions.AI: the abstraction every project should have

According to Microsoft Learn, the Microsoft.Extensions.AI.Abstractions package provides the core exchange types such as IChatClient and IEmbeddingGenerator, and any .NET library that provides an LLM client can implement IChatClient. The Microsoft.Extensions.AI package adds automatic function tool invocation, telemetry and caching through familiar dependency injection. These low-level APIs were extracted from Semantic Kernel.

The practical payoff: business code depends only on IChatClient. Switching from OpenAI to Claude or to a local model via Ollama is a DI registration change, not a logic change. IChatClient has two main methods: GetResponseAsync (complete response) and GetStreamingResponseAsync (an IAsyncEnumerable of ChatResponseUpdate).

Clients are composed into a pipeline with ChatClientBuilder: for example UseFunctionInvocation so the model can call C# functions, UseOpenTelemetry for tracing, and DistributedCachingChatClient to cache responses for repeated chat histories. The library also provides RoutingChatClient and FailoverChatClient to send a request to another client when one provider fails before producing output.

Microsoft is explicit that MEAI alone is not an agent framework. A one-shot call, a chat feature or a tool-call loop can be built with MEAI without becoming agentic.

Microsoft Agent Framework vs Semantic Kernel: which one?

Microsoft Learn's overview describes Agent Framework as the direct successor to Semantic Kernel and AutoGen, created by the same teams. It combines AutoGen's simple abstractions with Semantic Kernel's enterprise features such as session-based state management, type safety, middleware and telemetry, and adds graph-based workflows for multi-agent orchestration. Agent Framework can create agents from any service that implements IChatClient, so it sits directly on top of MEAI.

The Microsoft.Agents.AI package reached a stable 1.0.0 release on NuGet on 2 April 2026, after Release Candidates from February 2026; at the time of review (October 2026) the latest version is 1.24.0. Some integration packages are still prerelease: the Learn getting-started guide installs Microsoft.Agents.AI.Foundry with the --prerelease flag, so check the status of each package before going to production.

Microsoft itself warns against overuse: if you can write a function to handle the task, do that instead of using an AI agent; if a direct MEAI call or a simple tool-calling loop solves the problem, use the simpler approach. For the concept, see what is an AI agent.

  • New project needing multi-step agents or workflows: Microsoft Agent Framework.
  • Existing Semantic Kernel project running well: keep it and plan a migration using the official guide.
  • Only chat, summarization, extraction or a few tools: Microsoft.Extensions.AI is enough.

Provider SDKs, MCP and local models

Official OpenAI and Anthropic SDKs

The OpenAI library for .NET (repo openai/openai-dotnet) is the official library, compatible with any .NET Standard 2.0 application, supports streaming, offers async variants for every call, and is designed to be mockable in tests. For Claude, the NuGet package Anthropic from version 10 onward is the official C# SDK; versions 3.x and below belonged to the community tryAGI SDK, which moved to tryAGI.Anthropic. This trips people up during upgrades, so check the version number. The Anthropic SDK exposes streaming methods with a Streaming suffix, such as Messages.CreateStreaming.

MCP C# SDK

The official C# SDK for the Model Context Protocol is maintained in collaboration with Microsoft and ships as ModelContextProtocol.Core (minimal dependencies), ModelContextProtocol (hosting and DI) and ModelContextProtocol.AspNetCore (HTTP servers). The stable version on NuGet is 2.2.0. The 2.0.0 release notes say it remains backward-compatible with the 2024-11-05 protocol but marks APIs for capabilities deprecated by the new specification as Obsolete, so older tutorials may be out of date. MCP tools plug straight into IChatClient because McpClientTool inherits from AIFunction. Microsoft notes that if a tool is used only inside one app, in-process function calling is simpler than MCP. See what is MCP.

ONNX Runtime and ML.NET for local models

ONNX Runtime is an inference engine for ONNX models that runs on Linux, Windows and macOS, has a C# API and integrates hardware accelerators such as TensorRT, OpenVINO and DirectML. ML.NET is an open-source, cross-platform ML framework for training and consuming custom models in .NET, and it can consume ONNX models through ONNX Runtime. Both fit narrow tasks such as text classification, embeddings or forecasting on tabular data, where calling an LLM API is overkill and expensive.

A reference architecture for an ASP.NET Core AI backend

Illustrative example: an API that helps customer-support staff answer from internal documents and look up orders. A layered architecture lets you swap models, add tools and test each part independently.

  • API layer (ASP.NET Core): authentication, rate limiting, input validation, streamed responses.
  • Application layer: use cases such as AnswerQuestion and SummarizeTicket, depending only on IChatClient and business interfaces.
  • AI layer: an IChatClient pipeline registered in DI (function invocation, telemetry, caching, failover); prompts and model settings come from configuration, never hardcoded.
  • Knowledge layer: Microsoft.Extensions.DataIngestion to read and chunk documents; Microsoft.Extensions.VectorData to store and retrieve vectors (see what is RAG).
  • Tool layer: C# functions registered via AIFunctionFactory, or an MCP server when tools must be shared across apps; each tool gets least privilege.
  • Operations layer: OpenTelemetry, health checks, Polly on every outbound HTTP call, long tasks pushed to a background queue.

Streaming, resilience and observability

Streaming

Long answers can take seconds; the OpenAI .NET repo notes that streaming lets you start processing the beginning of a completion before it finishes. In ASP.NET Core, return the IAsyncEnumerable from GetStreamingResponseAsync over Server-Sent Events or SignalR, and pass the request's CancellationToken down so the model call is cancelled when the user disconnects, instead of paying for tokens nobody reads.

Resilience with Microsoft.Extensions.Http.Resilience and Polly

Microsoft.Extensions.Http.Resilience, built on Polly, provides AddStandardResilienceHandler for HttpClient. Per the .NET Blog, the standard pipeline chains five strategies: rate limiter, total request timeout, retry, circuit breaker and attempt timeout. For LLM calls, tune two things: the attempt timeout must allow for large responses, and retries should not apply to a streaming request that has already started returning data. Microsoft Learn notes that Polly throws TimeoutRejectedException, not the standard TimeoutException, which matters when you configure retry conditions yourself. We have not verified whether the OpenAI or Anthropic SDKs use an IHttpClientFactory-managed HttpClient by default; check each SDK's documentation for how to supply a custom transport or HttpClient.

OpenTelemetry for AI calls

UseOpenTelemetry in MEAI adds OpenTelemetryChatClient to the pipeline, implementing the OpenTelemetry Semantic Conventions for Generative AI (version 1.41 per the API docs); the specification is still experimental, so the telemetry output may change. Attributes such as gen_ai.request.model, gen_ai.usage.input_tokens and gen_ai.usage.output_tokens let you measure tokens and estimate cost per request. Per OpenTelemetry, prompt content and tool arguments are not captured by default because they may contain sensitive data; enable EnableSensitiveData only in development. Agent Framework emits traces, logs and metrics following the same conventions.

Testing, deployment and cost control

Testing

Because business code depends on IChatClient, unit tests can swap in a fake client that returns fixed responses. The OpenAI .NET SDK is designed for mocking with virtual methods and model factories. For answer quality, Microsoft provides Microsoft.Extensions.AI.Evaluation to compare outputs repeatably and catch regressions when prompts, models or tools change. Keep a golden question set running in CI before every model change.

Deployment

A .NET AI backend deploys like any ASP.NET Core service: Linux containers, configuration and API keys from environment variables or a secret store, never in source. Microsoft presents Aspire as the orchestration, service-wiring and observability layer for distributed .NET apps, useful once the system spans an API, workers, a vector store and MCP servers. When models must run close to the data, Microsoft offers Foundry Local for local-first scenarios. For infrastructure choices, see choosing a cloud architecture.

Cost

Cost comes mainly from tokens, not frameworks: the libraries above are open source. The levers are: measure tokens per request with OpenTelemetry, cache repeated answers, use small models or local ONNX for narrow tasks, cap tool-call iterations, and cancel calls when the client disconnects. Details in LLM cost control.

.NET lessons from the author's open-source work

Outside his work at EYC, the author maintains two personal open-source projects on GitHub: Nalix, a low-latency binary networking framework for .NET 10 that is Native AOT compatible (Apache-2.0), and Zalo.Net, an unofficial .NET SDK for the Zalo API. Neither is a component of EYC products. The lessons that carry over to AI backends: measure before optimizing, keep layers clean, and document when not to use a tool, as the Nalix README does with its own "When NOT to use" section.

If your company runs .NET systems and wants to add AI, EYC can help through its software and SaaS and AI agents and automation services; SLA and source-code ownership commitments are available under contract.

If your AI backend needs to connect to Zalo, see calling the Zalo API from C#/.NET.

Frequently asked questions

Is C# a good choice for AI, or do I need Python?

Python remains more common for training models. But most AI backends today call models over APIs and connect data and tools, where C# and .NET have full official support: Microsoft.Extensions.AI, Microsoft Agent Framework, the OpenAI and Anthropic SDKs, the MCP C# SDK, and ONNX Runtime for local models.

How is Microsoft.Extensions.AI different from Semantic Kernel?

Microsoft.Extensions.AI is the low-level abstraction layer (IChatClient, IEmbeddingGenerator, middleware); those APIs were extracted from Semantic Kernel. Semantic Kernel, and now Microsoft Agent Framework, are higher orchestration layers built on those abstractions.

Should I migrate my Semantic Kernel project to Microsoft Agent Framework now?

Microsoft calls Agent Framework the direct successor and publishes an official migration guide. For a project running well, plan a tested migration rather than rushing; for a new project that needs agents, start with Agent Framework.

Which package is the official Claude SDK for C#?

The NuGet package named Anthropic, version 10 and above, is the official SDK. Versions 3.x and below belonged to a community SDK now renamed tryAGI.Anthropic.

When do I need MCP instead of normal function calling?

When the same tool set must serve several different apps, agents or IDEs. If a tool is used only inside one app, in-process function calling through AIFunction is simpler.

How do I track token cost in a .NET backend?

Enable UseOpenTelemetry on the IChatClient pipeline. GenAI-convention telemetry records the model and input and output token counts for each call; export it to an OTLP-compatible observability tool and aggregate by feature or customer.

Share article:
Key point

In 2026, building an AI backend in C# no longer means hand-writing HttpClient calls to an API. The .NET ecosystem has a standard abstraction layer, an official agent framework, official SDKs from the major providers and an MCP SDK. What remains is backend discipline: layering, measurement, error handling, testing and cost control. Pin your versions, read the release notes, and start with the simplest solution that works.

Sources

  1. .NET + AI ecosystem tools and SDKs — Microsoft Learn
  2. Microsoft.Extensions.AI libraries — Microsoft Learn
  3. Use the IChatClient interface — Microsoft Learn
  4. IChatClient Interface (API reference) — Microsoft Learn
  5. OpenTelemetryChatClient Class — Microsoft Learn
  6. Microsoft Agent Framework Overview — Microsoft Learn
  7. Agent Framework observability — Microsoft Learn
  8. Migrate your Semantic Kernel and AutoGen projects to Microsoft Agent Framework Release Candidate — Microsoft Agent Framework Blog
  9. openai/openai-dotnet: The official .NET library for the OpenAI API — GitHub (OpenAI)
  10. C# SDK — Claude Platform Docs (Anthropic)
  11. modelcontextprotocol/csharp-sdk — GitHub (Model Context Protocol)
  12. ModelContextProtocol package — NuGet Gallery
  13. Build resilient HTTP apps: Key development patterns — Microsoft Learn
  14. Building resilient cloud services with .NET 8 — .NET Blog
  15. ONNX Runtime and Models — Microsoft Learn (Azure Machine Learning)
  16. Make predictions with an AutoML ONNX model in .NET (ML.NET) — Microsoft Learn
  17. C# | onnxruntime — ONNX Runtime
  18. Inside the LLM Call: GenAI Observability with OpenTelemetry — OpenTelemetry
  19. Nalix — GitHub (ppn-systems)
  20. Microsoft.Agents.AI package — NuGet Gallery

Related solutions

  • Software & SaaS — Design and build web platforms, business software and subscription products.
  • AI & Automation — Coordinate agents, automate repeatable workflows and keep people in control.
  • Cloud, Data & Infrastructure — Architecture, hosting, processing, storage and data foundations for modern systems.

Continue reading