Claude Prompt Caching Implementation Guide
Prompt caching lets Claude reuse large chunks of a prompt — system instructions, tool definitions, long documents — across multiple requests instead of reprocessing them every time. Implementing it correctly means marking the right parts of your prompt with cache_control breakpoints, keeping those sections byte-for-byte identical between calls, and structuring requests so the cacheable content comes before anything that changes.
If you're here because your Claude API bill is dominated by repeated context — a 20-page policy document sent on every support ticket, a 5,000-token system prompt sent on every chat turn — prompt caching is the fix, and it's a config change, not a rewrite. This guide walks through the actual implementation: where to put breakpoints, how the TTL and pricing work, and how to verify it's actually hitting.
How prompt caching actually works
Claude's API lets you tag specific content blocks with cache_control: {"type": "ephemeral"}. When you send a request, Claude checks if a prefix matching that cached block already exists from a recent request. If it does, it skips reprocessing that content and just reads it from cache — much cheaper and faster than a full prompt evaluation.
A few mechanics matter for implementation:
- Caching is prefix-based. Everything before and including the cache breakpoint must be identical to a previous request, in the same order. Change one character earlier in the prompt and the cache misses.
- The cache has a TTL, typically around 5 minutes of inactivity (longer options exist on some tiers). Each cache hit refreshes the TTL, so active workloads stay warm.
- There's a minimum token count for a block to be cacheable — very short system prompts or tool lists won't benefit because the overhead isn't worth it.
- Writing to cache costs more than a normal input token, but reading from cache costs significantly less. The savings show up when the same prefix is reused multiple times, not on a single call.
Where to put cache breakpoints
The general rule: put static, reusable content first, mark it with a breakpoint, then put the variable, per-request content after it. Common candidates:
- System prompts — especially long ones with instructions, persona, formatting rules
- Tool definitions — a large JSON schema for tools rarely changes between requests
- Reference documents — a knowledge base excerpt, a contract, a codebase file you're asking questions about
- Few-shot examples — static example conversations used for steering output
Here's what a cached request looks like in practice:
curl https://api.anthropic.com/v1/messages \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"model": "claude-sonnet-4-5",
"max_tokens": 1024,
"system": [
{
"type": "text",
"text": "<your 4000-token system prompt and guidelines>",
"cache_control": {"type": "ephemeral"}
}
],
"messages": [
{"role": "user", "content": "Summarize ticket #4821 using the above guidelines."}
]
}'
The system block is cached; the user message changes on every call but doesn't invalidate the cache because it comes after the breakpoint.
For tool-heavy agents, apply the breakpoint to the tool definitions instead:
const response = await anthropic.messages.create({
model: "claude-sonnet-4-5",
max_tokens: 1024,
tools: [
{ name: "search_orders", input_schema: { /* ... */ } },
{ name: "refund_order", input_schema: { /* ... */ } },
],
system: [
{ type: "text", text: longSystemPrompt, cache_control: { type: "ephemeral" } }
],
messages: conversationHistory,
});
You can set multiple breakpoints in a single request (e.g., one after the system prompt, one after a static document, one after few-shot examples) as long as each block before a breakpoint stays identical across calls.
Verifying the cache is actually working
Don't assume caching is working just because you added cache_control — check the usage object returned with every response:
{
"usage": {
"input_tokens": 25,
"cache_creation_input_tokens": 1150,
"cache_read_input_tokens": 0,
"output_tokens": 340
}
}
On the first call, you'll see cache_creation_input_tokens populated — that's the write. On subsequent calls within the TTL window, you want cache_read_input_tokens populated instead, with cache_creation_input_tokens at or near zero. If you keep seeing cache creation on every call, something upstream is changing the cached prefix — check for timestamps, random IDs, or non-deterministic ordering in your system prompt or tool list.
Common implementation mistakes
- Putting dynamic content before the breakpoint. If your system prompt includes
"Today is {date}", it invalidates the cache every day — move dynamic fields after the cached block or into the user message. - Reordering tools or documents between requests. Even if the content is the same, a different order breaks the prefix match.
- Caching content that's too small. If a block doesn't meet the minimum token threshold, the breakpoint is a no-op.
- Expecting cross-session caching. The TTL is short; caching helps high-frequency workloads (chat turns, agent loops, batch processing) far more than infrequent one-off requests.
Where this fits if you're building on top of Claude
If you're exposing Claude through your own product or internal tool, prompt caching is something you implement once at the request layer and it benefits every downstream call. If you'd rather not manage API keys, billing, and request plumbing yourself, SubToAPI turns your existing Claude access into a straightforward HTTPS API — with application keys, streaming, and usage metadata per key — so your team can focus on prompt structure instead of infrastructure. The Messages docs cover request formatting, and streaming and tools work the same way you'd expect from a direct Claude integration. Start with the quickstart or check pricing if you're evaluating plans.
questions
Does prompt caching reduce output quality or change responses? No. Caching only affects how input tokens are processed internally; it doesn't change the model, the prompt content, or the generated output in any way.
How long does the cache stay alive between requests? It's typically around five minutes of inactivity, refreshed on each cache hit. For bursty or steady traffic, this keeps the cache warm; for sparse, infrequent requests, you'll pay the cache-write cost more often than you get cache-read savings.
Can I cache multiple separate blocks in one request? Yes — you can set several cache_control breakpoints in a single request (for example, after the system prompt and again after a static document), as long as everything up to each breakpoint stays identical across calls.