> For the complete documentation index, see [llms.txt](https://docs.umbraco.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.umbraco.com/ai-in-umbraco/extending/middleware.md).

# Middleware

Add cross-cutting concerns to AI operations with 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
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/BY6U5aSc8WkjTxgoIPgC" %}
[Chat Middleware](/ai-in-umbraco/extending/middleware/chat-middleware.md)
{% endcontent-ref %}

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

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


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
