Eighth GPU Deep Dive post, and a return to a thread PagedAttention left half-pulled. That post covered block sharing between branches of the same request — parallel samples, beam search candidates — copying a block only when one branch actually diverges from the others. But most repeated prefixes in real traffic aren't siblings in one request. They're a system prompt re-sent on every call to an API, a few-shot template reused across thousands of unrelated requests, or turn eight of a chat conversation that still carries turns one through seven verbatim. Those requests don't arrive in the same batch, sometimes not even in the same minute. Sharing a block between them means caching it past the request that first computed it.
The waste, restated
The KV cache for a given span of tokens is a pure function of the model weights and the tokens that came before it — nothing about it depends on which request asked for it. A naive serving system still recomputes it from scratch every time, because it has no way to tell that the first 800 tokens of this request are byte-for-byte identical to the first 800 tokens of the last one. If the shared portion is a long system prompt or a growing conversation history, that recomputed prefill can dwarf the cost of generating the one new token the request actually wanted.
vLLM's answer: hash the blocks
vLLM's Automatic Prefix Caching builds directly on the block structure from the PagedAttention post. Every full block gets a hash key built from three things: "Parent hash value: The hash value of the parent hash block," "Block tokens: A tuple of tokens in this block," and "Extra hashes" for anything else that needs to make the block unique — LoRA adapter IDs, multimodal input hashes, or a per-tenant cache salt. Chaining each block's hash to its parent's means two requests only produce a matching hash for block N if every one of their first N blocks was identical — exactly the guarantee needed to safely reuse the cached KV tensors instead of recomputing them. On a new request, the scheduler hashes its prompt tokens and looks up each block's hash in a pool shared across every request currently being served; whatever matches gets reused, and only the unmatched suffix actually needs a forward pass.
Blocks that no longer belong to any running request don't get freed immediately — they sit in a free queue, evicted least-recently-used first, on the chance a future request shares that prefix too. vLLM orders that queue deliberately: a request's last block "must hash more tokens and is less likely to be reused by other requests," so it gets evicted before the earlier, more generic blocks nearer the shared root. The system prompt at the front of your context is far more valuable to keep around than the one request-specific sentence at the end of it.
SGLang's answer: don't fix the block size, use a tree
SGLang (Zheng et al., 2023/2024) generalizes the same idea with a different data structure. RadixAttention stores the KV cache for every token sequence it has ever processed in a radix tree — "a space-efficient alternative to a classical trie" — mapping token sequences directly to their KV cache tensors, with a page size of a single token rather than a fixed block. The paper is explicit that this isn't limited to one request's own branches: it "enables the automatic reuse of the KV cache across multiple generation calls," so "requests with the same prompt prefix can reuse the KV cache" regardless of when or from whom they arrived. Because the tree's structure already is the prefix-sharing structure — every request's path from the root is exactly its shared history — there's no separate hashing step, and matches aren't limited to whole-block boundaries the way a fixed hash-block scheme's are.
Eviction follows the tree shape: SGLang "evicts the least recently used leaf first," which lets common ancestors keep being shared until they, too, become leaves with nothing left depending on them. On top of this, SGLang adds cache-aware scheduling — sorting the batch so requests with the longest matching prefix run next to each other, maximizing how much of the cache survives between them. Across the paper's benchmarks (agent control, few-shot learning, JSON decoding, RAG, multi-turn chat), this pushes the achieved cache hit rate to "50% to 99%," with cache-aware scheduling reaching "96% of the optimal hit rate on average." The paper reports SGLang's full system — RadixAttention, intra-program parallelism, and faster constrained decoding together — delivers up to 6.4x higher throughput and 3.7x lower latency than the inference systems it compares against; it doesn't isolate how much of that is RadixAttention alone, so that combined number is best read as what the whole system achieves, not a RadixAttention-specific multiplier.
Same goal, two different data structures
Both systems are solving the identical problem — detect when a new request's prefix has already been computed, and skip recomputing it — with the tradeoff landing on different sides of a flexibility-versus- overhead line. vLLM's hashed, fixed-size blocks reuse the allocator machinery PagedAttention already needed, at the cost of only matching at block boundaries. SGLang's radix tree matches down to individual tokens and makes the branching structure of shared history explicit, at the cost of maintaining a tree instead of a flat hash map. Neither changes the computation being approximated — as vLLM's own docs put it, prefix caching "won't change model outputs" at all, since the cached tensors are exactly what a fresh forward pass over those same tokens would have produced. It's a scheduling and memory-reuse layer sitting entirely underneath the model.
Why this interacts with everything else on this blog
Cache-aware scheduling is still scheduling — the same job continuous batching does when it decides which requests share an iteration. SGLang's own paper flags the tension directly: "greedy cache-aware scheduling can achieve high throughput," but taken too far "it can lead to starvation" for requests whose prefixes don't match anything currently cached. And prefix caching only pays off at all because PagedAttention already made the KV cache addressable in small, independently-allocatable blocks instead of one contiguous reservation per request — without that, there'd be no granular unit to hash, match, or evict in the first place. Prefix caching doesn't replace either idea; it's what you get when you ask PagedAttention's block sharing to work across time, not just across branches of one request in flight.
This is also, by vLLM's own account, no longer a research curiosity: "prefix caching is almost a free lunch and won't change model outputs," so it "has been widely used by many public endpoints (e.g., OpenAI, Anthropic, etc.) and most open source LLM inference frameworks (e.g., SGLang)." The system prompt you resend on every API call is, on any of these backends, very likely not being recomputed at all.
References
- SGLang: Efficient Execution of Structured Language Model Programs — Zheng et al. (arXiv:2312.07104)
- Automatic Prefix Caching — vLLM Design Documentation
- Efficient Memory Management for Large Language Model Serving with PagedAttention (arXiv:2309.06180)
Prefix tree diagram above is original artwork made for this post.