Prompt Caching
Prompt caching lets the API resume from specific prefixes in prompts.
Definition · At a glance · How it works
Definition
Prompt caching optimizes API usage by letting the API resume from specific prefixes in prompts. Prompt caching covers the full prefix: tools, system, and messages, in that order, up to and including the block designated with cache_control. ephemeral is the only supported cache type, and it has a 5-minute lifetime by default. Prompt caching has no effect on output token generation.
At a glance
Cache writes occur only at the breakpoint that you set.
How it works
With automatic caching, you add a single cache_control field at the top level of your request. The system automatically applies the cache breakpoint to the last cacheable block and moves it forward as conversations grow. With explicit cache breakpoints, you place cache_control directly on individual content blocks for fine-grained control over exactly what gets cached.
You can define up to 4 cache breakpoints in your prompt. Marking a block with cache_control writes exactly one cache entry, which is a hash of the prefix ending at that block. If the current breakpoint has no cache entry, the system walks back one block at a time, checking whether the prefix hash at each earlier position matches an existing cache entry.
The system checks at most 20 positions for each breakpoint, counting the breakpoint itself as the first. By default, the cache has a 5-minute lifetime. Each time the cached content is used, the cache is refreshed at no additional cost.
The lifetime is measured from the start of the request that writes or reads the cache entry, not from the end of its response. Time spent generating a response counts against the lifetime: if a response takes 4 minutes to stream, a follow-up request that reuses the same cached prefix must start within about 1 minute of that response completing. Cache hits require 100 percent identical prompt segments, including text and images up to and including the block marked with cache control.
Prompts below the model or platform's minimum cacheable length cannot be cached, even if marked with cache_control. Requests to cache fewer tokens than that minimum are still processed, without caching or an error. For concurrent requests, a cache entry becomes available only after the first response starts.
Different organizations never share caches, even if they use identical prompts.
Example
The source's growing-conversation lookback example appends new blocks each turn and sets cache_control on the final block of each request. Turn 1 has 10 blocks, with the breakpoint on block 10. No prior cache entries exist, so the system writes an entry at block 10. Turn 2 has 15 blocks with the breakpoint on block 15, and block 15 has no entry, so the system walks back to block 10 and finds the turn-1 entry.
That is a cache hit at block 10. The system processes only blocks 11 through 15 fresh and writes a new entry at block 15. Turn 3 has 35 blocks with the breakpoint on block 35, and the system checks 20 positions, blocks 35 through 16, and finds nothing. The turn-2 entry at block 15 is one position outside the window, so there is no cache hit.
Adding a second breakpoint at block 15 starts a second lookback window there, which finds the turn-2 entry. cache_creation_input_tokens is the number of tokens written to the cache when creating a new entry. cache_read_input_tokens is the number of tokens retrieved from the cache for this request.
To check whether a prompt was cached, look at the response usage fields: if both cache_creation_input_tokens and cache_read_input_tokens are 0, the prompt was not cached, likely because it did not meet the minimum length requirement. Another example in the source caches the full text of a legal agreement as a prefix while keeping the user instruction uncached. On the first request of that example, cache_creation_input_tokens is the number of tokens in the entire system message, including the legal document.
At that point cache_read_input_tokens is 0, with no cache hit. input_tokens is the number of tokens in the user message only. In the legal-agreement example, on subsequent requests within the cache lifetime, cache_read_input_tokens is the number of tokens in the entire cached system message.
In those subsequent requests within the same cache lifetime, cache_creation_input_tokens is 0, with no new cache creation.
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-opus-5-5",
max_tokens=1024,
system=[
{
"type": "text",
"text": "You are an AI assistant tasked with analyzing legal documents.",
},
{
"type": "text",
"text": "Here is the full text of a complex legal agreement: [Insert full text of a 50-page legal agreement here]",
"cache_control": {"type": "ephemeral"},
},
],
messages=[
{
"role": "user",
"content": "What are the key terms and conditions in this agreement?",
}
],
)
print(response.usage.model_dump_json())Common misconceptions
In the source's common-mistake example, request 1 writes a cache entry at block 6, and the hash includes the timestamp. In request 2, the timestamp differs, so the prefix hash at block 6 differs. The lookback walks through blocks 5, 4, 3, 2, and 1, but the system never wrote an entry at those positions. There is no cache hit, and each request pays for a fresh cache write and never gets a read. Moving cache_control to block 5, the last block that stays the same across requests, lets subsequent requests read the cached prefix.
Sources
This page is written from the official sources below. Open them to check the original text.
- Official documentationhttps://platform.claude.com/docs/en/build-with-claude/prompt-cachingFetched
Verified ·
This page is written by AI from the official sources above and checked automatically. This site has not tested it. Found a mistake? Tell us.