Prompt caching reuses the system prompt, tools, and earlier messages that each request sends again. The provider bills those tokens once while the cache is valid.
chat() turns it on by default. A cache hit also makes the answer start sooner.
This setup takes about 2 minutes.
chat() uses the threadId as the cache key:
import {
chat,
chatParamsFromRequest,
toServerSentEventsResponse,
} from "@tanstack/ai";
import { openaiText } from "@tanstack/ai-openai";
export async function POST(request: Request) {
const { messages, threadId } = await chatParamsFromRequest(request);
const stream = chat({
adapter: openaiText("gpt-6.1-sol"),
systemPrompts: ["You are a support agent for Acme."],
messages,
threadId,
});
return toServerSentEventsResponse(stream);
}The client does not change. useChat already sends one threadId for each conversation.
Log the cached tokens in the onUsage hook of a middleware. The hook runs one time for each model call:
import { chat } from "@tanstack/ai";
import type { ChatMiddleware } from "@tanstack/ai";
import { anthropicText } from "@tanstack/ai-anthropic";
const cacheLog: ChatMiddleware = {
name: "cache-log",
onUsage: (ctx, usage) => {
const read = usage.promptTokensDetails?.cachedTokens ?? 0;
console.log(`Call ${ctx.iteration}: ${read} of ${usage.promptTokens} tokens from the cache`);
},
};
const stream = chat({
adapter: anthropicText("claude-sonnet-5-5"),
messages: [{ role: "user", content: "Hello!" }],
threadId: "thread-1",
middleware: [cacheLog],
});From the second call of a conversation, most of the input comes from the cache.
Set promptCache on chat():
| Value | What you get |
|---|---|
| 'short' | The default. The provider keeps the cache for a short time, 5 minutes on Claude. |
| 'long' | A longer cache, 1 hour on Claude. For other providers, see What each provider gets. |
| 'none' | No caching. Most adapters send no cache fields. On gpt-5.6 and later, openaiText sends prompt_cache_options: { mode: 'explicit' }, so OpenAI caches nothing. |
To set your own key too, pass an object. Both fields are optional:
import { chat } from "@tanstack/ai";
import { openaiText } from "@tanstack/ai-openai";
const stream = chat({
adapter: openaiText("gpt-6.1-sol"),
messages: [{ role: "user", content: "Where is my order?" }],
threadId: "thread-1",
promptCache: { retention: "long", key: "acme-support" },
});The types come from @tanstack/ai: PromptCacheRetention, PromptCacheOptions, and ResolvedPromptCache.
To change the cache for one model call only, use a middleware. See Change the prompt cache of a call.
On OpenAI and Mistral, a cache write is free and a cache read costs less. Keep the default.
Claude bills the cache write, and a read costs much less than normal input:
Two cases change the choice:
import { chat } from "@tanstack/ai";
import { anthropicText } from "@tanstack/ai-anthropic";
export function reviewContract(contract: string) {
return chat({
adapter: anthropicText("claude-sonnet-5-5"),
messages: [{ role: "user", content: `Review this contract:\n\n${contract}` }],
threadId: "thread-1",
// Sent one time only: skip the cache write.
promptCache: "none",
});
}Claude caches a start only when it is longer than a minimum for the model (512 to 4096 tokens). A shorter start costs nothing extra.
| Adapter | Default ('short') | 'long' |
|---|---|---|
| anthropicText, anthropicVertexText | cache_control markers on the last start tool, the last block of the last user or mid-conversation system message, and the system blocks. At most 4. The last start tool is the tool before the deferred placeholder, or the last tool. | The same markers with ttl: '1h' |
| bedrockText (Converse), Claude models only | A cachePoint after the system prompt and at the end of the last user message | Both with ttl: '1h' |
| openRouterText | sessionId set to the key. anthropic/* models also get markers on the system message, the last tool, and the last message. | The markers with ttl: '1h' |
| openaiText (Responses) | prompt_cache_key, cut to 64 characters, only to api.openai.com and its regional hosts | Also prompt_cache_retention: '24h'. On gpt-5.6 and later, prompt_cache_options: { ttl: '30m' } in its place. |
| openaiChatCompletions | prompt_cache_key, only to api.openai.com and its regional hosts | The key and prompt_cache_retention: '24h' |
| mistralText | prompt_cache_key, only to api.mistral.ai, on chat() and on structured output | The same |
Other adapters send no cache fields.
Each adapter page has the details: Anthropic, Amazon Bedrock, OpenAI, OpenRouter, and Mistral.
Your own cache fields win. Then chat() adds no automatic fields of that kind to the request:
The tools sit near the start of each request. If you add a tool during a run, the next call cannot read the cache from that point. Some GPT and Claude models take the new tool inside the conversation, so the start stays the same. See Mid-Conversation Changes.