Every request of a chat sends the same start again: the system prompt, the tools, and the earlier messages. You pay for those tokens on each call, and the model reads them again before it answers.
Prompt caching lets the provider keep that start. The next request with the same start reads it from the cache. It costs less, and the answer starts sooner.
chat() turns it on for you. You change nothing.
Pass the threadId of the request to chat(). chat() uses it 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);
}Nothing changes on the client. useChat already sends one threadId for each conversation.
The key is the first of these that you set:
A request reads the cache only up to the first token that differs. Text that changes on each call, such as the current time in the system prompt, ends the match there.
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?" }],
promptCache: { retention: "long", key: "acme-support" },
});The types come from @tanstack/ai: PromptCacheRetention, PromptCacheOptions, and ResolvedPromptCache.
Need a different cache for one model call only? A middleware can set it. See Change the prompt cache of a call.
On OpenAI and Mistral, a cache write is free and a cache read is cheaper. 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-opus-5-5"),
messages: [{ role: "user", content: `Review this contract:\n\n${contract}` }],
// Sent one time only: skip the cache write.
promptCache: "none",
});
}Claude caches a start only when it is longer than a model minimum (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. On gpt-5.6 and later, openaiText with 'none' sends prompt_cache_options: { mode: 'explicit' }, so OpenAI caches nothing.
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. Add a tool during a run, and the next call cannot read the cache from there. Some GPT and Claude models take the new tool inside the conversation instead, so the start stays the same. See Mid-Conversation Changes.
usage.promptTokens is the full input. Two fields split it:
Log them in the onUsage hook. It 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-opus-5-5"),
messages: [{ role: "user", content: "Hello!" }],
threadId: "thread-1",
middleware: [cacheLog],
});From the second call of a conversation on, most of the input now comes from the cache.