Prompt caching
Also known as: context caching
A service feature that saves the work done on the start of a prompt, so later requests starting the same way are faster and cheaper.
Draft - this entry has not been reviewed yet.
Formal
A model-serving feature that stores the KV cache for the opening part of a prompt for a short time and reuses it when a later request begins with exactly the same tokens, so only the new remainder has to be read; providers usually bill reused tokens at a lower rate.
In plain English
Like a lawyer who has already read the thick case file - each new question about it takes minutes, not a whole day of rereading.
In practice
At a Danish webshop, the developer behind the customer chat puts the 40 pages of delivery and return rules first in every request and the customer's question last; the provider reuses the stored start, so the chat's monthly bill falls sharply.
Why it matters
It only pays off if the fixed part comes first and stays word-for-word the same. Providers keep the stored work for minutes up to a day and should never share it between customers - where it is shared, answer speed can reveal what others sent.
Technical deep dive
Mechanically, prompt caching is KV-cache persistence across requests. After a request's prefill, the serving system keeps the key and value tensors for a prefix of the prompt, indexed by a hash of the exact token sequence, often in fixed-size blocks so that any block-aligned prefix can be matched. When a new request arrives, the scheduler looks up the longest cached prefix, skips prefill for those tokens and computes only the remainder. Because every token's keys and values depend on all preceding tokens, a single changed token invalidates the cache from that point onward; identical content in a different position is not reusable. Open-source engines expose the same idea as automatic prefix caching (vLLM) or RadixAttention (SGLang, which organises cached prefixes in a radix tree).
Commercial APIs implement it in two styles, and the details change between model generations, so current documentation is the authority. Anthropic's API uses explicit cache_control breakpoints (up to four per request) or an automatic mode; the prefix is hashed in the order tools, system, messages; the default lifetime is five minutes, refreshed on each hit, with an optional one-hour lifetime; at the time of writing cache writes cost 1.25× the base input price (2× for one hour) and cache reads around 0.1×, with model-dependent minimum prefix lengths. OpenAI applies caching automatically once a prompt passes a minimum length (1,024 tokens on many models), routes requests by a hash of the initial tokens, optionally steered by a prompt_cache_key, and bills cached input tokens at a discount. Google's Gemini API offers both implicit caching and explicit cached-content objects with a configurable TTL.
Getting hits is a prompt-engineering discipline. Stable content goes first (tool definitions, system prompt, reference documents, few-shot examples) and variable content last; timestamps, request IDs, randomised example order or non-deterministic JSON key ordering early in the prompt silently destroy the hit rate. Changing the tool list or model version also invalidates the prefix. Teams should monitor the provider's usage fields for cached versus uncached input tokens instead of assuming savings, since low-traffic endpoints may see entries expire between requests.
The security dimension is a timing side channel. A cache hit makes time to first token measurably shorter, so if a cache is shared between users, an attacker can probe whether a given prefix was recently sent by someone else. Gu et al. (ICML 2025) audited commercial APIs with statistical timing tests and detected global cache sharing across all users at seven providers, OpenAI among them at the time of the study. Current major providers state that caches are isolated per organisation or workspace, and self-hosted multi-tenant deployments should apply the same isolation. Prompt caching is also distinct from response caching, which stores whole answers for identical or semantically similar queries and returns them without running the model at all.
What to learn first
Everything this builds on, foundations first.
Relationships
Sources & further reading
Official documentation
- Anthropic documentation - Prompt caching · Anthropic
- OpenAI API documentation - Prompt caching · OpenAI
Reference works
- Gu et al. (2025), Auditing Prompt Caching in Language Model APIs
Where this data comes from
This entry was drafted by an AI from the sources above and has not yet been checked by a person. Treat it as a starting point, and check anything important against the sources.
See the review queueSuggest a correction on GitHubThis term as JSON
Check yourself
Loading…