TL;DR: Middleware lets you add logging, caching, guardrails, or RAG to any model without touching call sites. It is stable (no experimental prefix) in v7. import { wrapLanguageModel, streamText } from 'ai'; const wrapped = wrapLanguageModel({ model: yourModel, middleware: [loggingMiddleware, cacheMiddleware], }); const result = streamText({ model: wrapped, prompt: 'Hi' }); Use the wrapped model exactly like a normal model everywhere: streamText, generateText, ToolLoopAgent.

Middleware lets you add logging, caching, guardrails, or RAG to any model without touching call sites. It is stable (no experimental prefix) in v7.

import { wrapLanguageModel, streamText } from 'ai';

const wrapped = wrapLanguageModel({
  model: yourModel,
  middleware: [loggingMiddleware, cacheMiddleware],
});

const result = streamText({ model: wrapped, prompt: 'Hi' });

Use the wrapped model exactly like a normal model everywhere: streamText, generateText, ToolLoopAgent.

Built-ins worth knowing:
- extractReasoningMiddleware: exposes reasoning text on the result for models that bury it in output.
- extractJsonMiddleware: strips markdown fences so Output.object parsing succeeds.
- simulateStreamingMiddleware: fakes streaming for non-streaming models (dev and tests).
- defaultInstructionsMiddleware / defaultSettingsMiddleware: apply fallback instructions or settings when the call does not provide its own.
- addToolInputExamplesMiddleware: injects tool input examples to improve call quality.

Rules:
1. Multiple middlewares apply in order: firstMiddleware(secondMiddleware(model)). Put guardrails outermost, caching innermost (or wherever your semantics demand).
2. Middleware is model-agnostic. Write it once against the LanguageModel interface and reuse across providers.
3. Keep middleware fast and non-throwing. A logging middleware that throws will fail the generation it was observing.
4. Cache middleware needs a key that includes model id, prompt/messages hash, and settings. Cache the wrong key and users get each other's answers.
5. Test middleware with simulateStreamingMiddleware-style doubles before pointing it at paid models.
6. Middleware composes with telemetry: wrap first, then registerTelemetry still sees the calls.

## When to use
You hit exactly this: Language model middleware: logging, caching, and guardrails with wrapLanguageModel in Language model middleware.

## When not to use
A different error, or the same symptom in a different tool. This page only covers the failure above.

## Compatibility
Language model middleware (versions mentioned here: v7).