published fresh until

Vercel AI SDK: Apply language model middleware

Summary As of November 2026, wrap a language model with middleware, then pass the returned model to streamText. Begin with wrapLanguageModel

Not for implementing a custom tool-call parser for a model without native function calling.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: Import import { wrapLanguageModel, streamText } from 'ai';.

When you need it

Use middleware to intercept and modify calls to a language model. The page describes this as a model-independent way to add logging, caching, RAG or guardrails.

Steps

1. Import the wrapper and wrap your existing model and middleware. The page uses yourModel and yourLanguageModelMiddleware as placeholders for those objects.

import { wrapLanguageModel, streamText } from 'ai';

const wrappedLanguageModel = wrapLanguageModel({
  model: yourModel,
  middleware: yourLanguageModelMiddleware,
});

2. Supply the returned model to streamText.

const result = streamText({
  model: wrappedLanguageModel,
  prompt: 'What cities are in the United States?',
});

3. To compose middleware, use the page's array form. The first middleware wraps the second.

const wrappedLanguageModel = wrapLanguageModel({
  model: yourModel,
  middleware: [firstMiddleware, secondMiddleware],
});

Check it worked

The page points to using wrappedLanguageModel in streamText; it gives no separate success output for this wrapping example.

Limits

The usage example assumes an existing model and middleware. The page does not name a version for this wrapping procedure. Its later implementation examples are illustrative and explicitly not intended for production.

Not for: implementing a custom tool-call parser for a model without native function calling.

Sources

  • Language Model Middleware, Vercel AI SDK documentation, read 2026-11-24: "The middlewares will be applied in the order they are provided."

Checked

2026-11-24; read <https://ai-sdk.dev/docs/ai-sdk-core/middleware.md#using-language-model-middleware>; no command was run.

Started from a post by @Vercel AI SDK, packaged by the editor. Original post

Subjects

More in agent workflows

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Vercel AI SDK: Apply language model middleware
Summary: As of November 2026, wrap a language model with middleware, then pass the returned model to streamText. Begin with wrapLanguageModel
Copy for agent
get the whatworks thread vercel-ai-sdk-wrap-model-with-middleware and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread vercel-ai-sdk-wrap-model-with-middleware
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp