Reduce repeated generation
Repeated prompts or deterministic requests should avoid a new provider call.
AI Tools · Const
Cache AI provider responses temporarily and replay them for repeated requests. The recommended method uses language model middleware, with simulateReadableStream for cached streaming output.
Caching stores model responses temporarily and reuses them for matching inputs. The recommended current approach is language model middleware: cache complete generate results directly, while storing stream parts and replaying them with simulateReadableStream.
wrapLanguageModel({ model, middleware })| Name | Type | Description |
|---|---|---|
middleware | LanguageModelMiddleware | Language model middleware configuration containing wrapGenerate and/or wrapStream handlers. |
model | LanguageModel | The model supplied to the wrapped language-model helper. |
A wrapped language model whose generation and streaming operations can serve cached responses.
import { generateText, wrapLanguageModel, simulateReadableStream } from 'ai';
import { openai } from '@ai-sdk/openai';
const cache = new Map<string, any>();
const middleware = {
wrapGenerate: async ({ doGenerate, params }: any) => {
const key = JSON.stringify(params);
const cached = cache.get(key);
if (cached) return cached;
const result = await doGenerate();
cache.set(key, result);
return result;
},
wrapStream: async ({ doStream, params }: any) => {
const key = JSON.stringify(params);
const cached = cache.get(key);
if (cached) {
return { stream: simulateReadableStream({ chunks: cached }) };
}
const { stream, ...rest } = await doStream();
const chunks: any[] = [];
const response = stream.pipeThrough(new TransformStream({
transform(chunk, controller) { chunks.push(chunk); controller.enqueue(chunk); },
}));
cache.set(key, chunks);
return { stream: response, ...rest };
},
};
const model = wrapLanguageModel({ model: openai('gpt-4o-mini'), middleware });
await generateText({ model, prompt: 'Say hello.' });Wraps generation and streaming calls with an in-memory cache; cached stream chunks are replayed through simulateReadableStream.
Repeated prompts or deterministic requests should avoid a new provider call.
Streaming UI responses need to retain incremental delivery on cache hits.
Completed generation results should be stored through an SDK lifecycle hook.
Language Model MiddlewarewrapGeneratewrapStreamsimulateReadableStreamonEnd