Contents4 of 15
Download the PDF (892 KB)Before You Blame the ModelPart I · The Room: what the model actually sees
Chapter 03Runtime
Your prompt is a contract with the cache
With my small open model, reading a 60,000-token prompt cold took about 58 seconds. Reading the same prompt warm took a fraction of a second.
Same model, same tokens, same machine. The only difference was whether the server had already seen the beginning of that prompt.
Every serious model server does this, from llama.cpp on your desk to the big APIs. It keeps the work it did on the start of your last request, and if your next request starts with the exact same bytes, it skips straight past them. The hosted providers turn that into a price. As of November 2026, Anthropic bills cache reads at a tenth of the normal input price on most models, with a surcharge the first time a prefix is written to the cache. You mark where caching should happen yourself. OpenAI caches automatically for prompts of 1,024 tokens or more, and its docs list discounts of up to 95% on its newest models. Minimum lengths, lifetimes and prices all change, so check the current docs before you plan around them.
The catch is in the word exact. The cache matches a prefix. It reuses everything up to the first byte that changed and recomputes everything after it. One changed byte near the top of your prompt and you pay full price for the whole thing.
#The prefix killers
When I went hunting for why my cache hit rate was low, I found it was being broken by things that looked harmless:
- A tool list that reshuffles. Most chat templates put tool definitions near the top. Reorder them, reword one description, or add a tool mid-run, and everything after them is recomputed.
- Timestamps and random ids near the top. "Current time: 14:02:31" in the system prompt makes every request unique from byte one.
- A one-time message on turn zero that disappears on turn one, so every later prompt has shifted.
- Volatile sections placed before stable ones. A live status block above the instructions means the instructions never cache.
- Server settings that fight each other. On my local server, one cache option meant to help made each new session evict another session's reusable prefix. Turning it off brought reuse back.
The easiest win in this book to implement came from the first item. I used to change the tool list each turn to show only the tools that made sense. I froze the list instead, byte for byte, and enforced "which tool is allowed right now" in code when a call comes in (Chapter 6 shows how). Across 30 runs, that cut uncached prompt tokens by 25% and wall time by 15%. Nothing else changed.
Meanwhile, several clever layouts I was sure would help, like moving everything volatile to the end or sorting sections from most stable to least, measured flat. A cache idea is cheap to test and expensive to believe. Test it first.
#The real choice every harness makes
Under all of this sits one design decision, and every agent makes it whether it means to or not.
- Append and cache. Keep adding to one growing conversation. The prefix never changes, so the cache is wonderful, until the window fills and you have to cut something.
- Rebuild and fit. Rebuild a compact prompt from scratch each turn. It fits forever, but anything you rewrite near the top gets paid for again.
Neither one is free. The mistake is not knowing which bill you chose. If you rebuild, keep the parts that don't change at the very front and make them byte-stable. If you append, plan for the day the window fills (Chapter 4).
def first_diff(a: str, b: str) -> int:
n = min(len(a), len(b))
return next((i for i in range(n) if a[i] != b[i]), n)
# the final prompt text after templating; servers match tokens, so this
# is a close approximation, and plenty to find what's breaking reuse
prev, cur = rendered(turn - 1), rendered(turn)
i = first_diff(prev, cur)
print(f"reusable: {i / max(len(cur), 1):.0%} first change: {cur[i:i + 80]!r}")