# Middleware

Middleware lets you wrap AI client operations to add cross-cutting concerns like logging, caching, rate limiting, and telemetry. Middleware is applied to all AI requests without modifying application code.

## How Middleware Works

Middleware wraps the underlying AI client, intercepting requests and responses:

{% @mermaid/diagram content="graph LR
A\[Your Code] --> M1\["Middleware 1\nLogging"]
M1 --> M2\["Middleware 2\nCaching"]
M2 --> C\["AI Client\nProvider"]
C --> D\[AI Service]" %}

Each middleware receives a client and returns a wrapped client with additional behavior.

## Middleware Types

| Type      | Interface                | Wraps                                           |
| --------- | ------------------------ | ----------------------------------------------- |
| Chat      | `IAIChatMiddleware`      | `IChatClient`                                   |
| Embedding | `IAIEmbeddingMiddleware` | `IEmbeddingGenerator<string, Embedding<float>>` |

## Quick Example

{% code title="LoggingMiddleware.cs" %}

```csharp
using Microsoft.Extensions.AI;
using Microsoft.Extensions.Logging;
using Umbraco.AI.Core.Chat;

public class LoggingChatMiddleware : IAIChatMiddleware
{
    private readonly ILoggerFactory _loggerFactory;

    public LoggingChatMiddleware(ILoggerFactory loggerFactory)
    {
        _loggerFactory = loggerFactory;
    }

    public IChatClient Apply(IChatClient client)
    {
        return client.AsBuilder()
            .UseLogging(_loggerFactory)
            .Build();
    }
}
```

{% endcode %}

{% code title="MyComposer.cs" %}

```csharp
using Umbraco.AI.Extensions;
using Umbraco.Cms.Core.Composing;

public class MyComposer : IComposer
{
    public void Compose(IUmbracoBuilder builder)
    {
        builder.AIChatMiddleware()
            .Append<LoggingChatMiddleware>();
    }
}
```

{% endcode %}

## Key Concepts

### Middleware Order Matters

Middleware is applied to the client in registration order. The first registered middleware wraps the underlying client (innermost), the second wraps that, and so on. As a result, the **last** registered middleware is the **outermost** wrapper and sees requests first.

```csharp
builder.AIChatMiddleware()
    .Append<LoggingMiddleware>()     // Innermost (wraps raw client first)
    .Append<CachingMiddleware>()     // Wraps Logging
    .Append<TracingMiddleware>();    // Outermost (sees requests first)
```

### Middleware Receives Dependencies

Middleware classes support constructor injection:

```csharp
public class MyMiddleware : IAIChatMiddleware
{
    private readonly ILogger<MyMiddleware> _logger;
    private readonly IMyService _myService;

    public MyMiddleware(ILogger<MyMiddleware> logger, IMyService myService)
    {
        _logger = logger;
        _myService = myService;
    }

    public IChatClient Apply(IChatClient client)
    {
        // Use injected services
    }
}
```

### Using M.E.AI Middleware

Microsoft.Extensions.AI includes built-in middleware. Use the `AsBuilder()` extension to apply them:

```csharp
public IChatClient Apply(IChatClient client)
{
    return client.AsBuilder()
        .UseLogging(_loggerFactory)           // Built-in logging
        .UseOpenTelemetry(_loggerFactory)     // OpenTelemetry tracing
        .UseFunctionInvocation()              // Tool calling support
        .Build();
}
```

## In This Section

{% content-ref url="/pages/sXSecmedsYTh4PJyzOXs" %}
[Chat Middleware](/ai-in-umbraco/extending/middleware/chat-middleware.md)
{% endcontent-ref %}

{% content-ref url="/pages/COnVgxKcw7XNWkVvKqKh" %}
[Embedding Middleware](/ai-in-umbraco/extending/middleware/embedding-middleware.md)
{% endcontent-ref %}

{% content-ref url="/pages/u9oEq3gWxwuFSqW9gvag" %}
[Middleware Ordering](/ai-in-umbraco/extending/middleware/middleware-ordering.md)
{% endcontent-ref %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.umbraco.com/ai-in-umbraco/extending/middleware.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
