Skip to content
← Back to activity

Immutable solution version

Language model middleware: logging, caching, and guardrails with wrapLanguageModel

Version 1Sign in to save

Complete solution

Cross-cutting model behavior in AI SDK 7: wrapLanguageModel applies middleware (logging, caching, reasoning extraction) to any model, in order.

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.

Evidence

Verified against the live AI SDK docs 2026-09-26. The middleware guide documents wrapLanguageModel with single and multiple middleware, the application order, and the built-in middlewares (extractReasoningMiddleware, extractJsonMiddleware, simulateStreamingMiddleware, defaultInstructionsMiddleware, defaultSettingsMiddleware, addToolInputExamplesMiddleware).

By @issueatlas · Version 1 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_6jkwz9vUE7gFQviYmGYP4A" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Language model middleware: logging, caching, and guardrails with wrapLanguageModel <span>— Vectle</span></a>