Zenaique

A teammate randomized system prompts per user and expects the prompt cache to still help, what's wrong?

Spot the error·Medium·4.0 · 0·~2 min·Asked atBrowserbaseCoreweave·Relevant atAnthropicOpenAI
Attempt it

Click any words you think contain an error. Click again to unmark.

Mark at least one word to submit.
TL;DR

Prompt caches match the exact leading tokens. A per-user salt at position zero means no two users share a cached prefix, and there is no semantic fallback, so the hit rate is near 0%, not 90%.

Memory aid
Sign in to see the mnemonic that makes this stick.
Easy to grasp

Imagine a library that gives you a discount when you ask for a book whose title starts exactly the way other people's titles start. The librarian checks the first letter, then the second, then the third, and so on, until the letters stop matching. If the very first letter is your initial, nobody else's title will ever match yours past letter one. That is what putting a per-user salt at the start of a prompt does to the cache. The librarian never even gets to read the long identical part that follows, because they gave up on letter one. The fix is to keep your initial out of the title and write it on a sticker at the end, where the matching has already finished.

Concept explanation~2 min read

Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.

Prompt caching is the largest single cost saving in production LLM serving. It can cut input-token bills by an order of magnitude on workloads with a stable system prompt. Because the saving is so large, this mistake is both common and expensive. A small, well-intentioned change at the top of the prompt silently removes the entire benefit, while the team believes it is still in effect.

The plan puts a six-character per-user salt at the head of every system prompt for analytics. It predicts a ninety percent cache hit rate. Its reasons are that the rest of the prompt is identical and that providers match prompts at a semantic level. Both halves of that prediction are wrong, and they fail for different reasons rooted in how the cache works. The same pattern recurs in many forms: timestamps in the system prompt, request ids in tool schemas, per-tenant prefixes in agent loops. All of them break the cache for the same structural reason.

The cache matches the leading token sequence exactly

Hosted providers do not publish their cache internals, but Anthropic and OpenAI both document exact-prefix matching. The open-source engines show two common structures. SGLang's RadixAttention stores cached prefixes in a radix tree indexed by exact token ids, with KV-cache pages attached to the tree nodes. vLLM keys each full KV block by a hash chained from its parent block's hash, so it also behaves like a prefix tree.

A lookup walks the tree from the root. At each step it asks whether the request's next token id matches one of the children of the current node. If yes, it descends. If no, the walk stops. The node where it stops marks the longest cached prefix this request can reuse. The provider charges the discounted rate for those tokens and the full rate for everything past the stop point.

In this scenario, the first token of every request is the user-specific salt. After a few hundred requests, the root of the tree has hundreds of children, one for each unique salt token. None of them are shared across users. A request from a different user descends one step, hits no further match, and returns immediately. Only the same user's next call can continue, and only while that user's entry is still cached. The two thousand identical tokens that follow the salt are never reached because the walk already stopped at depth one.

This is not a bug in any particular provider, and you cannot tune it away with longer keys or more aggressive matching. It is the inherent behavior of any prefix-keyed structure. A single different leading token does not cost one token of cache miss. It costs the entire downstream prefix that follows that token in the tree. A cache prefix acts as a single key from position zero up to the breakpoint, and any difference along that key invalidates all of it.

Why providers cannot safely do semantic caching
The fix: cacheable prefix versus per-request tail
How to detect this bug in production
Sign in to unlock the full deep dive.

Situations where this technique stops working.

Sign in to see when this approach fails.

2–4 min · Everything important, quickly.

Sign in to see the quick scan of the deep dive.

Real products, models, and research that use this idea.

  • Anthropic Claude prompt caching uses cache_control markers and matches the prefix byte-exactly up to the marker. Per-user content must live after the final marker to preserve hit rate.
  • OpenAI's automatic prompt caching needs the entire rendered prefix to match. On GPT-5.5, cached input costs 0.1x the standard input rate.
Sign in to see more production examples.

What an interviewer would ask next. Try answering before peeking at the approach.

QWhy can prompt caches not safely do fuzzy or semantic matching even if it were efficient?
A

The cache stores KV-cache state computed under specific exact tokens. Reusing it under a different leading token would feed downstream attention layers vectors that were never produced from this prompt. That breaks determinism and likely degrades output. Fuzzy matching would be incorrect, not just imprecise.

2 more follow-ups an interviewer would ask next. Sign in to reveal them.

Red flags & common mistakes

The phrases that signal junior thinking. Click to expand.

Most common mistake

Assuming prompt caching matches prompts by meaning or 'mostly equal' content. It does not. The lookup is a byte-exact prefix comparison from position zero, and a single different leading token invalidates the whole prefix.

Sign in to see all red flags and common mistakes.

60 second bullets to scan on the way to the call.

  • Why prefix caches match exact tokens from position zero, not semantic similarity

  • How a single leading-token difference invalidates the entire downstream prefix

Sign in to unlock the revision sheet.

Primary sources. Browse if you want the original framing.

Similar questions

Same topic, related formats. Practice these next.

4 curated
Next question
What is the KV cache in transformer inference?
Flashcard·Easy