Build a Chat API with ASP.NET Core and an LLM A developer outlined a step-by-step approach to building a chat API with ASP.NET Core, C#, and Microsoft.Extensions.AI, showing how to wrap an LLM behind the IChatClient abstraction so the application service stays independent of the model provider. The guide starts with a minimal POST endpoint, then adds a scoped ChatService layer, custom request/response DTOs to keep the API contract stable, and plans for conversation history, ownership, persistence, and failure handling as the prototype scales to multiple users. The author stresses that the frontend never talks to the model provider directly and that no agent framework or vector database is needed for a working chat endpoint. Build a Chat API with ASP.NET Core and an LLM Learn how to build a chat API with ASP.NET Core and an LLM using Microsoft.Extensions.AI: conversation history, ownership, failure handling, and testing. build-chat-api-aspnet-core-llm chat API with ASP.NET Core In the previous guide, we built the foundation of an AI application with ASP.NET Core and kept the LLM as a dependency inside the application, not the owner of authentication, business logic, and data access. Now we'll build something users can interact with: a chat API with ASP.NET Core. A chat interface is usually the first feature people associate with an AI application, but sending a message to a model is only one part of the workflow. The API has to accept input, maintain a conversation, call the model, handle failures, and return a response the frontend can consume. Once several users arrive, conversation ownership and persistence matter too. We'll use ASP.NET Core, C , and Microsoft.Extensions.AI, starting with a basic endpoint and then adding conversation history, a separate service layer, and fixes for problems that appear when a prototype becomes a real application. The architecture stays small: no agent framework or vector database is needed to make a chat endpoint work. Imagine an AI assistant in a developer support platform. A user asks, "What is dependency injection in ASP.NET Core?", then "Explain scoped versus singleton services," then "Show me a small example using an interface." The assistant should understand these belong to one conversation. The API exposes two operations: create a conversation, and send a message to an existing one. Frontend → ASP.NET Core API → Chat Service → IChatClient → LLM One boundary doesn't change: the frontend never talks to the model provider directly. All model requests go through our backend. dotnet new webapi -n FoundryChat cd FoundryChat Install Microsoft.Extensions.AI and the provider package for your model Ollama, Azure OpenAI, or another supported integration . The examples use the IChatClient abstraction, so the application service stays independent of the provider, which helps if you later move from a local model to a hosted one. Decide what the API accepts and returns first: public sealed record SendMessageRequest string Message ; public sealed record ChatResponse Guid ConversationId, string Message ; Returning your own DTO instead of the model's raw response keeps the API contract stable and leaves room to add metadata later. The client belongs in dependency injection: js builder.Services.AddSingleton