How Prompt Caching Works: OpenAI's Automatic Cache and Anthropic's cache_control
Why prompt caching saves money, how the two vendors' mechanisms and lifetimes differ, how to structure prompts for reliable hits, and when caching actually makes a request more expensive.
The same system prompt, the same tool definitions, the same reference documents — sent again with every request. Prompt caching lets the model service keep these repeated prefixes for a while; the next time they hit, they're billed at a cache-read price far below the base input price, and responses come back faster. For long conversations and coding agents, caching is often the single most effective way to save.
How caching works
When a model processes input it computes intermediate results for every token. If two requests start with exactly the same content, the second can reuse the first one's computation.
The key words are "prefix" and "exactly the same":
- Caching matches prefixes: from the first token up to the first difference.
- Change a single character near the start — say, insert the current time into the system prompt — and nothing after it can hit.
- Caches expire and must be written again.
OpenAI: automatic, priced by lifetime
OpenAI caches automatically: once a prompt reaches the official minimum length, repeated prefixes hit without any extra field.
On GPT-5.6 and later (gpt-5.6-*, gpt-6-*, gpt-6.1-*), cache writes are priced in two lifetimes:
| Lifetime | How to request | Endpoints |
|---|---|---|
| 30 minutes (default) | Nothing, or prompt_cache_options.ttl: "30m" | Chat Completions, Responses |
| 12 hours | prompt_cache_options.ttl: "12h" | Responses only |
The cache-read price doesn't depend on the lifetime chosen at write time. The older gpt-5.5, gpt-5.4, gpt-5.4-mini
and gpt-5.2 don't charge for cache writes; the Pro line has no cache-read discount.
Anthropic: explicit markers, with a write premium
Claude caches only what you mark: add cache_control to a content block and everything up to it is cached.
{
"model": "claude-opus-5-5",
"max_tokens": 2048,
"system": [
{
"type": "text",
"text": "(a long system prompt and reference documents)",
"cache_control": { "type": "ephemeral", "ttl": "1h" }
}
],
"messages": [{ "role": "user", "content": "Using the documents, answer: ..." }]
}Anthropic's cache prices are relative to the base input price:
| Item | Price |
|---|---|
| 5-minute write (default) | 1.25× base input |
| 1-hour write | 2× base input |
| Cache read | 10% of base input on most models; 5% on Opus 5.5 and Sonnet 5.5; 2.5% on Fable 5.1 |
In other words, on Claude content has to be read back once or twice before caching starts to pay off.
Structuring prompts for reliable hits
- Stable first, variable last: system prompt, tool definitions and reference documents first; the user's question, the current time and random IDs last.
- Keep tool definitions in a fixed order: reorder the list and the prefix changes.
- Don't put per-request content at the start: timestamps, request numbers and random examples invalidate everything after them.
- Append, don't rewrite, in multi-turn chats: editing an early message invalidates every cache after it.
- Reuse within one tier: Claude's Fast and Standard don't share a cache, so the first request after switching writes again.
Checking for hits
Look at usage:
| API | Cache reads | Cache writes |
|---|---|---|
| Chat Completions | prompt_tokens_details.cached_tokens | prompt_tokens_details.cache_write_tokens |
| Responses | input_tokens_details.cached_tokens | input_tokens_details.cache_write_tokens |
| Messages | cache_read_input_tokens | cache_creation_input_tokens |
Send two requests with the same prefix in a row; the second's cache reads should be close to the prefix length. If they stay at 0, look for something that changes near the start.
When caching makes things more expensive
- Long content used once: on Claude, writes are priced above base input, so a single read doesn't pay back.
- Gaps longer than the lifetime: if every request arrives after the cache expired, you pay the write premium every time. Batch work inside the lifetime, or choose a longer one.
- Prompts that are too short: shorter than the official minimum length, nothing is cached.
Caching on UnioAPI
Cache fields are identical to the official APIs and take effect as sent. Cache reads and each write lifetime are priced separately — see each model page — and cache usage shows up in your console request logs. The full rules are in Prompt caching & TTL.
Cache hit rate matters a great deal for coding agents; see Why coding agents use so many tokens.