How prompt caching works
To process a prompt, the model computes intermediate key-value (KV) states for every input token, which let it attend to earlier tokens while it generates. Prompt caching stores the KV states for a prompt’s prefix, so a later request with the same prefix skips that computation. The prefix is the prompt exactly as the model sees it, after the model’s chat template renders your request into tokens. Everything the chat template renders counts toward the prefix:- The system prompt and every earlier message, including images.
- Tool definitions in
tools. - Request settings the chat template renders into the prompt, such as
reasoning,reasoning_effort, andchat_template_kwargs.
Maximize cache hits
Any change that alters the rendered prompt ends the match at the point of the change. Structure requests so the part that changes comes last:- Put stable content first: Place instructions, tool definitions, reference documents, and few-shot examples at the start of the prompt. Put per-request content, such as the user’s question, timestamps, or retrieved snippets, at the end. A timestamp or username in the system prompt makes every request’s prefix unique from that point on.
- Append instead of rewriting: In a multi-turn conversation, add new messages to the end of the history and keep earlier turns byte-identical, so the reusable prefix grows with each turn. Summarizing, truncating, or reordering the history changes the prefix from the first edited message onward.
- Keep tools stable: Send the same tool definitions, with the same names, descriptions, schemas, and order, on every request. Tool definitions are part of the prefix, so changing any of them ends the match where the chat template renders them.
- Set reasoning options once per conversation: Choose
reasoningandreasoning_effortbefore the conversation starts. Changing them partway through changes the rendered prompt. - Pass reasoning back unchanged: For models that use preserved thinking, return prior reasoning exactly as the model generated it. Editing or reordering it changes the prefix.
- Serialize content deterministically: If your code builds tool schemas or JSON message content, keep the key order the same between requests. The same data serialized in a different order renders as different tokens.
- Stay on one model: KV states are specific to the model that computed them, so a cache entry only serves requests to the same model.
Route related requests with prompt_cache_key
Set prompt_cache_key to a stable string on requests that share a prefix, such as a conversation ID or a version name for your application’s system prompt. Together uses the key to route those requests as a group, which raises the chance that each one lands where its prefix is already cached. The key improves the odds of a cache hit but doesn’t guarantee one.
On dedicated endpoints, prompt_cache_key also serves as the request’s sampling key for routing stickiness, so requests with the same key always route to the same deployment.
In the Python SDK, pass prompt_cache_key in extra_body, as shown in the example in the next section. In the TypeScript SDK and the REST API, pass it as a top-level request field.
Check cached tokens
Theusage object on each response reports how many prompt tokens came from the cache. This example sends two requests that share a long system prompt and prints the cached count for each:
usage.prompt_tokens_details.cached_tokens. Some models return cached_tokens at the top level of usage instead, so the example checks both. See OpenAI compatibility for the full shape of the usage object.
To track your cache hit rate, divide the total cached_tokens by the total prompt_tokens across your requests.