PROMPT CACHE PREFIX
One changed byte at the top, and the whole cache misses
Wrenpost's support assistant caches a 7,000-token prefix: tools, rules and the support handbook, with a breakpoint after the handbook. To greet customers by name, someone adds 'Customer: Akin' between the rules and the handbook. Five requests arrive a minute apart from Akin, Ebele, Feyi, Akin, Ebele. What happens to the handbook's cache reads?
A prompt cache stores the processed state of a prompt’s prefix (its keys and values, the KV cache from KV cache batching) and reuses it when the next request starts with the same bytes. On the Claude API the prefix runs in render order, tools then system then messages, up to a block you mark with cache_control, and Anthropic’s prompt caching page says a hit needs “100 percent identical prompt segments” up to that block. There is no partial credit for a block that is almost the same. The first changed byte ends the match, and everything after it is processed and paid for again.
So the order of a prompt is a cost decision. Stable parts go first and volatile parts go last, with the breakpoint on the boundary between them.
system = [
{"type": "text", "text": f"Time: {now:%H:%M:%S}"}, # changes every second
{"type": "text", "text": RULES},
{"type": "text", "text": HANDBOOK, "cache_control": {"type": "ephemeral"}},
]
tools = [TOOL_DEFS[n] for n in enabled_tools] # enabled_tools is a set
messages = [{"role": "user", "content": question}]tools = [TOOL_DEFS[n] for n in sorted(enabled_tools)] # fixed order
system = [
{"type": "text", "text": RULES},
{"type": "text", "text": HANDBOOK, "cache_control": {"type": "ephemeral"}},
]
messages = [{"role": "user", "content":
f"Customer: {name}\nTime: {now:%H:%M:%S}\n\n{question}"}] # volatile last
# then check usage.cache_read_input_tokens > 0 from the second request on Watch the two invariants and the row marked as the first byte that differs from the previous request. In TIMESTAMP ON TOP that row is the second block, every request writes 7,010 tokens at 1.25x, and the run costs 25% more than no caching at all. NAME IN SYSTEM and TOOLS FROM A SET still save money, but only the requests that happen to match an earlier one hit: the repeat customers, or the requests that land on the same worker. In FIX: STABLE PREFIX the first differing byte is the customer name, after the breakpoint, and requests 2 to 5 read all 7,000 tokens.
Build the prompt template in render order. The breakpoint is fixed on the handbook block; tools are already sorted. Place all six blocks so that caching costs more than sending no cache_control at all.
GOAL: all six blocks placed and the five requests cost more than with no caching
A stable prefix is necessary but not sufficient. The entry has a lifetime.
The prefix is now stable. Find the shortest gap between requests at which it still reads nothing from the cache.
GOAL: shortest gap at which requests 2–5 all read 0 tokens
Five requests from different customers go through a cache that keys each entry on the exact text from the start of the prompt up to a block with cache: true. Rewrite buildPrompt(req) so that requests 2 to 5 each read at least the whole handbook from cache. Return an array of blocks { text, cache } in render order. Rules the grader checks: every tool name, the rules, the handbook, the customer, the time and the question must each appear in some block; at most 4 blocks may have cache: true (cache_control is accepted too). req.tools arrives in a different order on each request.
What the sim simplifies: tools, system and messages are one flat list of blocks, token counts are made up, output tokens are left out of the cost, and an entry exists only at a breakpoint. The real cache also looks back up to 20 block positions from each breakpoint for an earlier entry, so a hit can land a little before the breakpoint you marked. The multipliers are the Claude API’s as of 11 November 2026: a 5-minute write costs 1.25x base input, a 1-hour write 2x, and a read 0.1x on most models and less on some. Prefixes under the model’s minimum length (512 tokens on the newest models; 1,024 on some current ones and up to 4,096 on others, such as Haiku 4.5 and Opus 4.6) are not cached at all, and no error tells you. On most Claude models cache reads also do not count toward the input-token rate limit, so a prefix that hits raises throughput as well as cutting cost (LLM rate limits). The caching, batching and latency chapter has the full rules, the prices per model and a lab that measures all of this on a real prompt.
Any LLM feature with a long shared system prompt, tool list or document: support assistants, agents with many tools, document Q&A, few-shot classifiers. Symptoms: cache reads near zero in the usage fields, a bill that did not drop after caching was turned on, cost per request that rises with a deploy that added a date, a user setting or a new tool, and time to first token that never improved. For the full practice see the caching chapter.
“We turned on prompt caching and the bill did not move. What do you check?” Say that the cache is an exact prefix match, then read the usage fields: zero reads with writes on every call means something before the breakpoint changes per request. Name the usual suspects (timestamp, user details, unstable tool or JSON order), the fix (stable first, volatile last, breakpoint between), and the other causes of misses: TTL expiry, minimum length, and traffic split across models.
USED IN
Chapters that build on this lesson.