> 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/17.latest/extending/middleware.md).

# 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/NPI0LcEOTsvdmJECnO1R" %}
[Chat Middleware](/ai-in-umbraco/17.latest/extending/middleware/chat-middleware.md)
{% endcontent-ref %}

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

{% content-ref url="/pages/R99Ri2dtER6qVYOI74iA" %}
[Middleware Ordering](/ai-in-umbraco/17.latest/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 dynamically by asking a question.

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

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
