Chapter 03 · Prefix caching
Prefix caching
Keeping the KV of shared prompt prefixes so the next request with the same prefix skips their prefill. Below, the mechanism as the simulator ran it, step by step; then what it measured, why it behaves that way, and the papers.
Check yourself ↓ three questions and what to remember, at the end
Loading the prefix-cache animation…
Its row of the matrix
Loading the matrix…
Compute a shared prefix once
Many prompts start the same way: a system prompt, a tool list, the earlier turns of a conversation. Their keys and values are the same every time, so an engine can keep them and let the next request with that prefix start its prefill after it (SGLang (RadixAttention): a radix tree of segments, least recently used evicted first). In the animation 75% of all prompt tokens are found in the cache, and the median time to first token falls from 131.5 ms to 21.4 ms.
A cached segment is memory that holds no running request's private tokens. While a request uses it, it is pinned; afterwards it stays only until something needs the room, so the hit rate depends on how soon the same prefix comes back (its reuse distance) and how much KV memory is spare.
Why it saves so much
Prefill is compute-bound: every prompt token costs its share of the model's FLOPs (the roofline's right-hand side), so a cached token is a token of work not done, not merely a faster one. The gain grows with the shared fraction of each prompt:
| System prompt tokens | Hit rate | TTFT p50 off | TTFT p50 on | Throughput gain |
|---|---|---|---|---|
| 0 | 0.0% | 52.6 ms | 52.6 ms | 1.00x |
| 256 | 33.7% | 78.0 ms | 49.3 ms | 1.34x |
| 1,024 | 66.8% | 147.1 ms | 46.1 ms | 2.21x |
| 4,096 | 88.6% | 438.2 ms | 47.4 ms | 4.41x |
| 8,192 | 93.6% | 865.0 ms | 52.5 ms | 5.98x |
It also depends on memory. With little spare KV, a longer think time between two turns of a session lets other sessions evict its segments: the hit rate falls from 68.8% at one second to 23.1% at five minutes (results.md section 21, OPT-13B on one A100-40GB).
Which workloads it helps
In the sweep it is the largest single lever wherever prompts share prefixes: goodput per GPU rises +868% on the coding agent (a 6,144-token shared prefix and eight turns), +656% on real-time voice (six turns) and +102% on chat; only +9% on long-context RAG, where the shared system prompt is a small part of each long prompt, and +0% on offline batch, which shares nothing. The cost is memory: peak KV use rises +588% on chat, because the cache keeps segments nobody is using right now. That is a trade worth making only while the memory is spare (chapter 2).
Against the paper
SGLang reports a large speed-up for multi-turn chat with short outputs and almost none with long ones; the simulator measures 2.48x and 1.27x (Ordering reproduces; the long-output gain is larger here).
What the sweep measured, lever by lever
Prefix caching (paged KV)
Change from the baseline on H100, at capacity for goodput, cost and energy, at the reference load for latency. Run it live
| Workload | goodput | $/M tok | J/tok | TTFT p99 | TPOT p99 | ITL p99 |
|---|---|---|---|---|---|---|
| Chat | +102% E | -50% E$ | -50% E | -57% | -27% | -63% |
| Coding agent | +868% E | -90% E$ | -86% E | -74% | -42% | -1% |
| Offline batch | +0% | +0% $ | -0% | -6% | +0% | -82% |
| Long-context RAG | +9% | -8% $ | -9% | -5% | -9% | +1% |
| Real-time voice | +656% E | -87% E$ | -82% E | -77% | -29% | -0% |
Caveats on these numbers
- EHuge percentages come from a baseline at its SLO edge. Capacity is the highest load at which 90% of requests meet both SLOs. Where the baseline sits just past a latency cliff (voice and the coding agent especially), a lever that pulls latency back under the SLO multiplies capacity, so gains of hundreds or thousands of percent are real in the model but say more about the cliff than about the lever. Marked wherever capacity at least doubles; read the absolute numbers too.
- $Prices per GPU-hour are illustrative. Cost per million tokens uses round illustrative prices (H100 $3.00, H200 $3.50, B200 $5.00 per GPU-hour), not quotes. Cost scales linearly with them, so the ranking of levers on one device does not depend on them; comparisons across devices do.
✓ better than the baseline, ✗ worse, by more than ±2%. Every lever on every metric and device: the matrix; on any two metrics: the explorer.
Check yourself
Commit to an answer before you reveal it: a wrong guess you can explain teaches more than a right one you skimmed. Every number in the answers comes from the same tested model as the chapter’s.
Reveal the answer
Answer: 75% (counted right within 10%)
Commit to a number, then reveal.
Reveal the answer
Answer: Offline batch, which shares nothing
Pick one, then reveal.
Reveal the answer
Answer: Prefill is compute-bound: a cached token is a token of work not done at all
Pick one, then reveal.
Papers and sources
- Zheng, Yin, Xie et al., SGLang: Efficient Execution of Structured Language Model Programs (arXiv:2312.07104): a radix tree of cached prefixes with LRU eviction
- Kwon, Li, Zhuang et al., Efficient Memory Management for Large Language Model Serving with PagedAttention (arXiv:2309.06180): KV blocks allocated on demand, preemption by recompute or swap