← back to knowledge-hub

.NET AI Essentials: The Core Building Blocks, Explained

Reach for a vendor’s SDK and it works — until you want to swap a model, add logging, or target a second provider. Then you’re rewriting plumbing instead of building.

.NET’s answer is four building blocks that sit above the vendors:

  1. Microsoft.Extensions.AI — one API across every LLM provider
  2. Microsoft.Extensions.VectorData — embeddings and semantic search
  3. Microsoft Agent Framework — agentic workflows
  4. Model Context Protocol (MCP) — interoperability

Most of the work lives in the first one.

Microsoft.Extensions.AI: one API, many providers

If you’ve written ASP.NET Core, this feels like home — same DI, same builder pattern. OpenAI, OllamaSharp, Azure OpenAI and more all sit behind one IChatClient. Switching providers becomes a registration detail, not a rewrite. Retries, token limits, and a middleware pipeline come for free.

Structured output, the easy way

Ask for a typed result; the library generates the schema and deserializes for you. No JSON string-wrangling.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
class Family
{
    public List<Person> Parents { get; set; }
    public List<Person>? Children { get; set; }

    class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
}

var family = await client.GetResponseAsync<Family>(
    [
        new ChatMessage(
            ChatRole.System,
            "You are an AI assistant that creates families."),
        new ChatMessage(
            ChatRole.User,
            "Create a family with 2 parents and 2 children."
        )]);

Standardised requests and responses

Per-vendor parameters get normalised through ChatOptions (temperature, max tokens), and responses carry UsageDetails for token accounting. Temperature is just a randomness dial: low = predictable, high = creative.

Middleware is where the power is

Chat clients are built, so you wrap any client in a pipeline — security, throttling, telemetry, tracing — applied uniformly across providers.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
public IChatClient BuildEnhancedChatClient(
            IChatClient innerClient,
            ILoggerFactory? loggerFactory = null)
        {
            var builder = new ChatClientBuilder(innerClient);

            if (loggerFactory is not null)
            {
                builder.UseLogging(loggerFactory);
            }

            var sensitiveData = false; // true for debugging

            builder.UseOpenTelemetry(
                configure: options =>
                    options.EnableSensitiveData = sensitiveData);
            return builder.Build();
        }

Multi-modal with DataContent

Richer payloads extend AIContent: ErrorContent, UserInputRequestContent, FunctionCallContent, HostedFileContent, UriContent, and DataContent (bytes + media type). The last one lets you hand a model an image — multi-modal and typed output in one call:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
var instructions = "You are a photo analyst able to extract the utmost detail from a photograph and provide a description so thorough and accurate that another LLM could generate almost the same image just from your description.";

var prompt = new TextContent("What's this photo all about? Please provide a detailed description along with tags.");

var image = new DataContent(File.ReadAllBytes(@"c:\photo.jpg"), "image/jpeg");

var messages = new List<ChatMessage>
            {
                new(ChatRole.System, instructions),
                new(ChatRole.User, [prompt, image])
            };

record ImageAnalysis(string Description, string[] tags);

var analysis = await chatClient.GetResponseAsync<ImageAnalysis>(messages);

No vendor-specific encoding, no manual contract — pass bytes, get a typed ImageAnalysis.

The other three blocks

  • VectorData — embeddings and semantic search; the base for RAG over your own data.
  • Agent Framework — multi-step agentic workflows where the model plans and acts.
  • MCP — a shared standard so tools and models from different vendors interoperate.

Most projects start and stay on Microsoft.Extensions.AI, reaching for the rest when they actually need retrieval, autonomy, or interop.

Final thought

These libraries don’t do anything a vendor SDK can’t. They put a stable, .NET-shaped seam between your code and a market that shifts every few months — so the next model is a registration line, not a weekend.

Adapted from Jeremy Likness’s .NET AI Essentials – The Core Building Blocks Explained on the .NET Blog.

graph cloud