Inference Trade-offs Explained

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 tokensHit rateTTFT p50 offTTFT p50 onThroughput gain
00.0% 52.6 ms 52.6 ms 1.00x
25633.7% 78.0 ms 49.3 ms 1.34x
1,02466.8% 147.1 ms 46.1 ms 2.21x
4,09688.6% 438.2 ms 47.4 ms 4.41x
8,19293.6% 865.0 ms 52.5 ms 5.98x
Recorded in results.md section 21: four shared system prompts of the length shown, Mistral-7B on one A100. Latency at half the cache-off throughput; throughput saturated.

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

Workloadgoodput$/M tokJ/tokTTFT p99TPOT p99ITL 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.

Q1In the animation, what share of all prompt tokens are found in the prefix cache?
Reveal the answer

Answer: 75% (counted right within 10%)

75%, and the median first token falls from 131.5 ms to 21.4 ms.

Compute a shared prefix once →

Commit to a number, then reveal.

Q2Which workload gains least from prefix caching in the sweep?
Reveal the answer

Answer: Offline batch, which shares nothing

Offline batch moves by +0%; the coding agent gains +868%.

Which workloads it helps →

Pick one, then reveal.

Q3Why is a cached prompt token worth so much?
Reveal the answer

Answer: Prefill is compute-bound: a cached token is a token of work not done at all

Every prompt token costs its share of the model's FLOPs, so the gain grows with the shared fraction of each prompt. The cost is memory: on chat peak KV use rises +588%.

Why it saves so much →

Pick one, then reveal.

Papers and sources