Many AI applications send almost the same prompt again and again. A support bot includes the same 8,000-token policy manual with every question. A coding agent resends the same tool definitions and project instructions at every step. A document assistant asks twenty questions about the same 100-page contract.
Without caching, the model processes that repeated content from scratch every time, and you pay full price for it. Prompt caching lets Claude reuse the work done on a repeated prompt prefix, cutting both cost and latency. For the right workloads, it is the single biggest optimisation available.
This article explains how prompt caching works in the Claude API, how it is priced and how to structure prompts to benefit.
How prompt caching works
When Claude processes a prompt, it builds internal representations of every token. Prompt caching stores those representations for a prefix of your prompt. If a later request starts with exactly the same prefix, Claude reads from the cache instead of recomputing.
Two words in that description matter a lot:
- Prefix: caching works from the start of the prompt up to a cache point. Content after the cache point is processed normally.
- Exactly: the cached part must match exactly, byte for byte. Change one character early in the prompt and everything after it misses the cache.
The order of the prompt
Claude builds the prompt in a fixed order:
1. tools
2. system
3. messages
A cache point covers everything before it in this order. So if you cache at the end of the system prompt, the cached prefix includes all tool definitions plus the system prompt. This means changing your tool definitions invalidates the system prompt cache too.
Enabling caching
There are two ways to enable caching.
Automatic caching
You can add a cache_control field at the top level of your request. Claude then applies a cache point automatically to the last cacheable block and moves it forward as the conversation grows. This is the simplest option for multi-turn conversations.
Explicit cache breakpoints
For fine control, you place cache_control directly on specific content blocks, such as the end of a long system prompt or a large document. You can use up to four explicit breakpoints in one request.
Here is a system prompt marked for caching:
response = client.messages.create(
model="claude-sonnet-5-5",
matokens=1024,
system=[{
"type": "text",
"text": LONG_POLICY_MANUAL,
"cache_control": {"type": "ephemeral"},
}],
messages=[{"role": "user", "content": question}],
)
The type ephemeral is currently the cache type used by the API.
Cache lifetime
By default, a cache entry lives for five minutes, and the timer refreshes each time the entry is used. A busy application keeps its cache warm naturally.
For workloads with gaps longer than five minutes, such as an agent that pauses for user approval or a batch of requests spread over an hour, you can request a one-hour lifetime by adding a ttl value of 1h inside cache_control.
Pricing
Caching changes how input tokens are billed:
- Cache write, five minutes: about 1.25 times the base input price.
- Cache write, one hour: about 2 times the base input price.
- Cache read: about 0.1 times the base input price, a 90 percent discount.
- Tokens after the last cache point are billed at the normal input rate.
- Output tokens are billed normally.
The arithmetic is simple. With a five-minute cache, you pay a small premium once, and every subsequent hit saves 90 percent. Caching pays for itself after just one or two reuses.
Here is an illustrative example with a 10,000-token cached prefix used 100 times within the cache lifetime:
- without caching: 100 x 10,000 = 1,000,000 input tokens at full price
- with caching: 10,000 tokens at 1.25 times, plus 99 x 10,000 at 0.1 times, roughly equal to 111,500 full-price tokens
That is close to a 90 percent reduction on that part of the bill. Always confirm current prices on Anthropic's pricing page.
Minimum cacheable size
Very short prompts cannot be cached. Each model has a minimum prefix length, typically between 1,024 and 4,096 tokens depending on the model. If your prefix is shorter, the request still works but nothing is cached, and the API does not return an error. This catches many developers out, so check the usage figures.
Checking that caching works
Every response includes usage figures. Look at three fields:
- cache_creation_input_tokens: tokens written to the cache in this request.
- cache_read_input_tokens: tokens read from the cache.
- input_tokens: tokens processed normally, after the last cache point.
usage = response.usage
log.info("cache write %d, cache hit %d, normal %d",
usage.cache_creation_input_tokens,
usage.cache_read_input_tokens,
usage.input_tokens)
On the first request you should see tokens written. On the next request within the lifetime, you should see tokens read. If you see neither, the prefix is too short or changing between requests.
What breaks the cache
Common causes of unexpected cache misses:
- Timestamps or dates in the system prompt. Putting the current time at the top changes the prefix every request. Move dynamic values to the end, inside the user message.
- Random ordering. Tool lists or documents serialised in a different order each time, for example from an unordered dictionary or a database without a stable sort.
- Per-user data early in the prompt. Put the shared content first and the user-specific content later.
- Changing tool definitions. Any change to tools invalidates everything after them.
- Changing images or thinking settings. These can also affect the cached prefix.
- Too long between requests. The default five-minute lifetime expired.
Pattern 1, static first, dynamic last
Order every prompt from most stable to least stable:
1. tool definitions
2. system instructions
3. reference documents
4. conversation history
5. the latest user message
Place cache points at the boundary between stable and changing parts.
Pattern 2, cache the growing conversation
In multi-turn chats, put a cache point on the latest message each turn, or use automatic caching. Each new request reads the cached history and only processes the new turn. The system checks backwards a limited number of blocks from a breakpoint to find a previous cache entry, so long gaps between breakpoints can reduce hits; placing breakpoints regularly helps.
Pattern 3, document question answering
Load a long document once at the start of the conversation, cache it, then ask many questions. Every question after the first reads the document from cache.
Pattern 4, agents with many tools
Coding and research agents resend large tool sets and instructions on every step. Caching them makes long agent runs dramatically cheaper and faster. Claude Code itself relies heavily on prompt caching.
Pattern 5, shared prefix across users
If many users share the same system prompt and knowledge base, they can share the cache within your organisation, as long as the prefix is identical. Keep user-specific details after the cache point.
Latency benefits
Caching is not only about cost. Reading from cache is much faster than processing the tokens again, so time to first token drops noticeably for long prompts. For interactive applications with big system prompts or documents, users feel the difference.
Caching and batch processing
The Message Batches API, which processes requests asynchronously at a discount, can be combined with caching. Because batch requests may be processed at different times, the one-hour cache lifetime can improve hit rates for batches that share a prefix.
Privacy and isolation
Caches are isolated between organisations, so another customer cannot read your cached content. Cached data is held for the lifetime you request. Check Anthropic's documentation for the current details on cache isolation and data handling relevant to your compliance needs.
A quick checklist
- Is the stable prefix longer than the model's minimum?
- Is everything before the cache point identical across requests?
- Are dynamic values such as dates and user names placed after the cache point?
- Are tools and documents serialised in a stable order?
- Do usage figures show cache reads on repeat requests?
- Would the one-hour lifetime improve hit rates for your traffic pattern?
Key takeaways
- Prompt caching reuses processing of an identical prompt prefix.
- Cache reads cost about a tenth of normal input; writes cost a little more.
- The prefix order is tools, then system, then messages.
- Use automatic caching or up to four explicit breakpoints.
- Default lifetime is five minutes, refreshed on use; one hour is available.
- Put stable content first and verify with usage fields.
Few changes deliver savings as large as prompt caching for so little effort. If your application sends long, repeated context, enabling caching should be near the top of your to-do list.
Related articles on MindStick
- Tokens, Cost and Latency, Budgeting an AI Application
- Context Windows Explained and Why They Limit What AI Can Do
- Getting Started With the Claude API, A Complete Developer Guide
- Cost Optimisation Architecture for High-Volume AI Workloads
Prices, minimum token counts and caching features may change. Check Anthropic's current prompt caching documentation and pricing page before planning costs.
Leave a Comment