← All writing

LLM engineering· January 30, 2026 · 10 min

Prompt Caching Is the Most Underrated Latency Win of 2026

One flag, four breakpoints, p50 down 60%. If your prompt is over 2k tokens and you're not caching, you're lighting money on fire.

A Flag That Cut p50 by 60%

The agent had a 6.2k-token system prompt — tool definitions, persona, output schema, a few in-context examples, and the running conversation. P50 latency on first response was 2.4s. P95 was 4.1s. Users complained that the assistant felt slow.

We added four cache_control markers in the right places. P50 dropped to 900ms. P95 to 1.7s. Cost on the cached portion fell ~90%. The diff was about 12 lines.

That was the entire optimization. No model swap. No streaming hacks. No prompt surgery. Just telling Anthropic which parts of the prompt do not change between calls.

If you are running an LLM app in production in 2026 and your cache hit rate is not a graph on your dashboard, you are leaving real performance on the floor.

How It Actually Works

Anthropic's prompt caching stores the model's internal KV state at specific breakpoints in your prompt. When the next request hits with the same prefix, the model skips recomputation and replays from the cached state. The cache lives for 5 minutes (or 1 hour if you pay for the extended TTL).

The economics:

Token typeCost vs base inputLatency vs base input
Cache write~1.25xsimilar
Cache hit (5m TTL)~0.1x (90% off)~0.15x on cached prefix
Cache hit (1h TTL)~0.1x (90% off)~0.15x on cached prefix

You pay a small premium the first time you write to the cache. After that, the cached tokens are nearly free and dramatically faster. As long as you reuse a prefix more than ~2-3 times within 5 minutes, you come out ahead on both cost and latency.

The Four-Breakpoint Pattern

Anthropic lets you set up to four cache breakpoints per request. The pattern that works in production is to mark them in order of stability — most stable first.

[ system instructions          ]  ← breakpoint 1 (rarely changes)
[ tool definitions             ]  ← breakpoint 2 (changes per deploy)
[ few-shot examples            ]  ← breakpoint 3 (changes per experiment)
[ retrieved context / history  ]  ← breakpoint 4 (changes per turn)
[ user message                 ]  (uncached, always fresh)

The key rule: a cache breakpoint covers everything before and including its marker. Any change above a breakpoint invalidates that breakpoint and everything below it. So order matters, ruthlessly.

Put the most volatile content last. If you accidentally put a timestamp at the top of your system prompt — and I have seen this in real codebases — every request invalidates the entire cache and your hit rate goes to zero.

A Concrete Example

Here is the shape in the Anthropic Python SDK. The same pattern works in TypeScript with @anthropic-ai/sdk.

from anthropic import Anthropic

client = Anthropic()

response = client.messages.create(
    model="claude-sonnet-4-7",
    max_tokens=1024,
    system=[
        {
            "type": "text",
            "text": STABLE_SYSTEM_PROMPT,  # persona, rules, output schema
            "cache_control": {"type": "ephemeral"},
        },
        {
            "type": "text",
            "text": render_few_shot_examples(tenant_id),  # examples
            "cache_control": {"type": "ephemeral"},
        },
    ],
    tools=tools_with_cache_breakpoint(tools),  # tool defs, breakpoint at end
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": conversation_history_through_turn_n_minus_1,
                    "cache_control": {"type": "ephemeral"},
                },
                {"type": "text", "text": current_user_message},
            ],
        }
    ],
)

# Read cache metrics off the response
usage = response.usage
print({
    "input": usage.input_tokens,
    "cache_creation": usage.cache_creation_input_tokens,
    "cache_read": usage.cache_read_input_tokens,
    "output": usage.output_tokens,
})

Two non-obvious things in this snippet. First, the breakpoint inside messages covers the conversation history through turn n-1, so each new user turn only adds an uncached delta — the previous turns stay cached across the conversation. Second, every response gives you cache read/write counts in usage. Log these on every request.

Cache Hit Rate Is a First-Class Metric

If you cannot answer "what is our cache hit rate by route over the last 24h" from your dashboard, you do not actually know whether caching is working.

I log three numbers per request:

cache_read_tokens / (cache_read_tokens + input_tokens)  → hit rate
cache_creation_tokens                                    → write volume
cache_read_tokens * 0.0 + input_tokens * 1.0 + ...       → effective cost

Then I aggregate by route, tenant, and prompt version. A healthy long-prompt agent runs at 80-95% cache hit rate on the cached portion. If yours drops below 50%, something is busting the cache — usually a timestamp, a UUID, or a tenant config string that snuck above a breakpoint.

The most embarrassing bug I have shipped on this: I included datetime.now().isoformat() in the system prompt as a "current time" reference. Every single request invalidated the cache. We caught it because the cache hit rate graph went flat at 0%. Cost was 4x what we projected for a week before anyone noticed.

The Cache-Busting Gotchas

These are the patterns that quietly destroy your hit rate. In rough order of how often I see them:

  1. Anything dynamic above a breakpoint. Timestamps, request IDs, "the current date is...", a randomized greeting. One byte changes the hash, the cache is gone.
  2. Tool definitions reordered. If your tool registry has a dict somewhere and you iterate it with non-deterministic order, the serialized tool list flips between calls.
  3. Whitespace changes from string formatting. Trailing newlines, indentation diffs from prompt templating, smart quotes vs straight quotes. Hash these in dev and assert they are stable.
  4. JSON key ordering. If you serialize any embedded JSON in the prompt with sort_keys=False and the dict order varies, you bust the cache. Always sort keys.
  5. Model swaps mid-flight. Cache state is per-model. If you A/B route between Sonnet and Haiku, each model has its own cache.

The defensive move: log a hash of the cached prefix on every request. If the hash changes when you do not expect it to, you have a bug. I keep this as a sampled debug log in prod.

When NOT to Cache

Caching is not free, and it is not always a win.

  • Short prompts (under 1024 tokens). Anthropic enforces a minimum cacheable size. Below that, caching does nothing.
  • Highly variable system prompts. If your system prompt is templated per user with substantial unique content, hit rate will be low and the 1.25x write premium will hurt.
  • Low-traffic endpoints. Cache TTL is 5 minutes (or 1 hour at extra cost). If your endpoint sees one request every 10 minutes, you are paying the write premium every time and never benefiting.
  • One-shot batch jobs. A nightly batch that runs N times then sleeps for 23 hours does not benefit at all.

The rule of thumb: cache pays off when you reuse a prefix more than ~2-3 times within the TTL window. Anything less, you are net negative.

The Multi-Tenant Pattern

Multi-tenant apps need to be careful about cache scope. The cache is keyed on the exact prefix bytes. If two tenants share the same system prompt and tool definitions, they share cache state. This is usually what you want — your system prompt is the same for all tenants, so cache infra is amortized across them.

What you do not want is leaking tenant-specific data into the cached prefix. If tenant A's account data is in the cached portion, and tenant B's request happens to share the prefix, you have a problem.

The pattern that works:

[ shared system prompt (same for all tenants)   ]  ← breakpoint 1, shared
[ shared tool definitions                       ]  ← breakpoint 2, shared
[ tenant_id marker + tenant-specific instructions ]  ← breakpoint 3, per-tenant
[ tenant's recent context                       ]  ← breakpoint 4, per-tenant
[ user message                                  ]  (uncached)

The tenant_id marker — literally something like <tenant:42> as the first token after the shared content — makes the cache key diverge for different tenants while keeping the shared prefix cacheable across all of them. Anthropic's caching is content-addressed, so two tenants with identical content below breakpoint 2 will share that cache. Below breakpoint 3, they will not.

In practice this gives you the best of both worlds: amortized cache writes on the shared portion, isolated cache state on the per-tenant portion.

What We Tried That Did Not Work

Early on, I tried to be clever and cache the user's recent messages by aggressively re-ordering messages so that immutable history landed above a breakpoint and the new message landed below. It worked in synthetic tests. In production it caused two problems.

First, the model occasionally responded as if it had not seen the most recent message, because the reordering made the conversational flow weird. Second, on any conversation edit (user retries a previous turn), I had to invalidate the breakpoint and re-cache from scratch — a common operation in our UI, so the hit rate cratered to 30%.

We backed it out and went with the boring pattern: history above the breakpoint, new user message below, no reordering. Hit rate stabilized at 87%. The model behaved correctly. Boring won.

A Realistic Production Profile

For one long-running agent in prod, after a few weeks of tuning:

MetricBefore cachingAfter caching
p50 latency2.4s0.9s
p95 latency4.1s1.7s
Input cost per call$0.018$0.004
Cache hit raten/a88%
Cache write raten/a7% of calls

The cost number lies a little because we now use a slightly more capable model. On the original model the cost drop was closer to 75%.

The Takeaway

Prompt caching is not a clever optimization. It is the default for any serious long-prompt LLM app in 2026. It is a flag, not a refactor.

Log your cache hit rate. Put the volatile content at the bottom. Hash your cached prefix in dev. Use the four breakpoints in stability order.

If your prompt is over 2k tokens and you are not using caching, you are lighting money on fire.