Use this file to discover all available pages before exploring further.
Headroom integrates with the Vercel AI SDK through three patterns: a one-liner wrapper, composable middleware, and standalone message compression. All three compress messages before they reach the underlying language model, with no changes to streaming, tool calling, or structured output behavior.
The simplest integration. Wraps any Vercel AI SDK language model with automatic compression and works with any provider (@ai-sdk/openai, @ai-sdk/anthropic, @ai-sdk/google, etc.):
import { withHeadroom } from 'headroom-ai/vercel-ai';import { openai } from '@ai-sdk/openai';import { generateText } from 'ai';const model = withHeadroom(openai('gpt-4o'));const { text } = await generateText({ model, messages: [ { role: 'user', content: 'Summarize these results...' }, ],});
withHeadroom() calls wrapLanguageModel + headroomMiddleware() under the hood.
Compress Vercel-format messages directly without wrapping a model. Useful for custom pipelines where you want to compress before passing to any downstream consumer:
import { compressVercelMessages } from 'headroom-ai/vercel-ai';const result = await compressVercelMessages(messages, { model: 'gpt-4o',});console.log(`Saved ${result.tokensSaved} tokens`);// result.messages is in Vercel format, ready for the AI SDK
Messages are converted from Vercel’s ModelMessage[] format to OpenAI format.
2
Compress via proxy
Headroom compresses the messages via the proxy’s /v1/compress endpoint.
3
Convert back to Vercel format
Compressed messages are converted back to Vercel’s message format.
4
Pass to model
The original model receives the smaller prompt. All other model behavior — tool calling, structured output, streaming — is unchanged.
The middleware integrates at the transformParams level, which means it runs before every doGenerate and doStream call. This covers generateText, streamText, generateObject, streamObject, and any other Vercel AI SDK function.