Chapter 20Down the Rabbit Hole: PagedAttention and the KV Cache That Was Mostly Empty
The number that pulled me in was from the vLLM paper. In FasterTransformer and Orca, only 20.4% to 38.2% of the GPU memory set aside for the KV cache held actual token state. PagedAttention got that to 96.3%, with 2 to 4x the throughput at the same latency. vLLM, TensorRT-LLM, Hugging Face TGI and SGLang all use some version of it.
The core idea is from 1960s operating systems. What I found is that it doesn't map onto GPUs as cleanly as the one-liner suggests.
Why the KV cache runs out#
Each new token attends to every previous one, so servers cache keys and values: one row per token per layer.
bytes/token = 2 (K and V) x layers x KV heads x head dim x bytes per elementFor OPT-13B, the paper's example, that's 800 KB per token in FP16, so a 2048-token request needs about 1.6 GB. Grouped-query attention helps: Llama-3-8B, with 8 KV heads, is 128 KiB per token.
Decoding is memory-bandwidth-bound. Throughput comes from batching sequences that share the weight read, so it's roughly proportional to how many sequences fit, which KV memory decides.
Three kinds of waste#
Before vLLM each request's cache was one contiguous tensor, reserved for the maximum length because output length is unknown at admission.
- Reservation. Slots for token 500 are held from step 1.
- Internal fragmentation. Reserve 2048, stop early: the rest was never used.
- External fragmentation. Holes between variable-sized chunks too small for the next request.
What convinced me paging beats a better allocator is the paper's "Orca (Oracle)" baseline, which knows every output length and reserves exactly. vLLM still beats it by 1.7 to 2.7x on ShareGPT, because external fragmentation remains.
Pages, again#
A request is a process, a KV block (16 tokens by default) is a page, the block table is the page table, and the free pool is the frame allocator.

Figure 6 from Kwon et al., "Efficient Memory Management for Large Language Model Serving with PagedAttention" (SOSP 2023), arXiv:2309.06180, licensed CC BY 4.0. Cropped and recompressed.
block table: [7, 1] phys 7: [ T T T T ] full phys 1: [ T T T . ] one free slot waste: 1 slot
The allocator hands out any two free blocks.
Allocation is one unit at a time, on demand, and every free block satisfies every request, so external fragmentation can't happen. Internal waste is capped at B − 1 slots per sequence.
The attention kernel has to change#
Keys and values are now scattered, and the softmax normalizer is global, so you can't softmax each block and average. The fix is FlashAttention's tiling trick: each block reports its local max, sum of exponentials and weighted sum of values, and partials merge exactly.
The block-wise kernel (Python-ish)
m, l, acc = -inf, 0.0, zeros(d)
for j, phys in enumerate(block_table): # e.g. [7, 1, 3]
K = kv_pool.K[layer, phys] # contiguous inside the block
V = kv_pool.V[layer, phys]
n = min(B, seq_len - j * B) # last block is partial
s = K[:n] @ q / sqrt(d)
m_new = max(m, s.max())
scale = exp(m - m_new) # rescale old partials
p = exp(s - m_new)
l = l * scale + p.sum()
acc = acc * scale + p @ V[:n]
m = m_new
out = acc / lRescaling by exp(m_old - m_new) puts blocks on equal footing, so it's the same math as contiguous attention up to float rounding. I had this wrong going in: PagedAttention doesn't save memory like quantization does. It stores exactly the same KV and stops wasting the space around it.
Loads inside a block are contiguous; only the jump between blocks is indirect, which is why block size can't be 1. The paper's kernel was still 20 to 26% slower than FasterTransformer's in isolation, repaid by the bigger batch. And there's no MMU: the kernel loads block_table[seq][j] and computes addresses itself. No TLB, no page faults.
The second win: sharing#
With a table of indirections, two tables can point at one physical block. vLLM reference-counts blocks and copies only on write, like fork(). Four parallel samples of one prompt share its blocks, and only the partially filled last block is copied, by writers who see a refcount above one. For a 30-token prompt and block size 16, that's three copies; the full first block is never copied.
For beam search the paper measured memory savings of 37.6 to 55.2% on Alpaca and 44.3 to 66.3% on ShareGPT.
Prefix caching extends this across requests, on by default in vLLM V1: finished requests' full blocks keep a hash in an LRU queue. The hash chains through the parent's hash, since KV depends on every earlier token. A matching prefix is reused and its prefill skipped.
What paging didn't fix#
Contiguous allocation never ran out mid-request because it reserved the worst case. On-demand allocation can: if the pool is full and a sequence needs one more block, someone goes. vLLM preempts the most recently arrived group. The paper swapped blocks to CPU; V1 defaults to recompute, re-running prefill on prompt plus generated tokens. It's all or nothing, because every decode step reads every block: no "cold page" to evict.
My simulator: one pool of 32,768 token slots, 3,000 synthetic requests with lognormal lengths, FCFS, one token per step.
| Allocator | KV util | Concurrent | Ext frag | Preempts |
|---|---|---|---|---|
| contiguous / max | 32.0% | 16.0 | 0.1% | - |
| contiguous / oracle | 50.3% | 24.7 | 78.9% | - |
| paged / block=16 | 96.9% | 49.8 | 0.0% | 818 |
| paged / block=128 | 89.7% | 45.2 | 0.0% | 463 |
| paged / block=512 | 72.0% | 34.6 | 0.0% | 82 |
The first row lands in the paper's 20 to 38% band; paged at 16 lands near its 96.3% with 3x the concurrency. But 818 preemptions in about a thousand steps: sequences cross block boundaries together, the pool empties, the newest are evicted, recomputed, readmitted and evicted again. Engines soften this with watermarks and running-sequence caps. I found no published study measuring the thrash directly.
Sharing has a dark side
A prefix-cache hit is measurably faster to first token. "The Early Bird Catches the Leak" recovers other users' prompts token by token from that timing. "Auditing Prompt Caching in Language Model APIs" found global cross-user cache sharing at 7 API providers, including OpenAI. vLLM supports a per-tenant salt in the block hash, which closes the channel by giving up cross-tenant reuse.
The question I'm left with#
PagedAttention looks like a trade: contiguous allocation turned unknown output length into wasted memory, paging turns it into preemption risk.
Can admission control hold KV utilization near 100% without thrashing when lengths are unknown? Learned length predictors are noisy and conservative watermarks bring the waste back. A 2026 paper, "Robust KV Cache Management under Output Token Length Uncertainty", works on this using the Azure LLM 2024 trace.
The other one: do software block tables survive at all? vAttention (ASPLOS 2025) keeps the KV cache virtually contiguous and maps physical pages on demand via CUDA virtual memory, so the kernel doesn't change. It reports up to 1.23x the throughput of paged FlashAttention and FlashInfer kernels, but must work around 2 MB driver pages and gets no free fine-grained sharing. With cheap fine-grained hardware paging, the block table might look like a workaround for a missing MMU.
Sources#
- Kwon et al. Efficient Memory Management for Large Language Model Serving with PagedAttention (SOSP 2023)
- Yu et al. Orca (OSDI 2022)
- Prabhu et al. vAttention (ASPLOS 2025)
- Zheng et al. SGLang
- Song et al. The Early Bird Catches the Leak (IEEE TIFS)
- Gu et al. Auditing Prompt Caching in Language Model APIs (ICML 2025)
- Robust KV Cache Management under Output Token Length Uncertainty
- Ainslie et al. GQA
- vLLM. Prefix caching design and optimization docs
- TensorRT-LLM. KV cache reuse
- Hugging Face TGI. PagedAttention