PagedAttention: Managing AI Memory Efficiently
See how borrowing an old operating-system trick lets a GPU fit far more conversations into the same memory.
Here’s the KV cache’s practical headache: when a request arrives, nobody knows how long its answer will be. Twenty tokens? Four thousand? Early serving systems played it safe and reserved a big, continuous block of memory for the maximum possible length, the moment each request started.
That wasted memory in three ways at once:
- Reserved but never used. A 50-token answer sitting in room for 2,000.
- Gaps. When requests finish, they leave holes of odd sizes. A new request needs one unbroken stretch, so a hole that’s too small is useless, even if the free memory adds up to plenty.
- Copies. Ten requests sharing the same long system prompt each stored their own copy of it.
Researchers found that existing systems were wasting most of their KV-cache memory this way. In 2023, the team behind vLLM borrowed a fix that operating systems have used for decades: paging.
With PagedAttention, the cache is cut into small fixed-size pages, each holding, say, 16 tokens. A request gets a new page only when it actually needs one, and the page can be anywhere in memory. A little page table per request remembers where its pages live, and the attention computation follows that table. Waste shrinks to the unfilled end of each request’s last page.
Two bonuses fall out for free. Requests with an identical beginning, like a shared system prompt, can point at the very same pages (“prefix caching”), so that prompt is stored and prefilled once. And because more conversations fit, the batch can grow, which, as you know, means more throughput from the same GPU.
Key takeaways
- Reserving the maximum length up front wastes most KV-cache memory: unused room, gaps and copies.
- PagedAttention stores the cache in small pages handed out on demand, anywhere in memory.
- A page table per request tracks its pages; waste shrinks to part of one page.
- Shared prefixes can share pages, and more requests fit, so throughput rises.
Check your understanding
Go deeper (more technical)
PagedAttention was introduced with vLLM (Kwon and colleagues, SOSP 2023). The paper reported that earlier systems used only about 20–38% of their KV-cache memory for actual tokens, while vLLM wasted under 4%, which translated into 2–4× higher throughput at similar latency.
Pages also enable copy-on-write: when several answers are sampled from one prompt, they share the prompt’s pages until they diverge. SGLang’s RadixAttention organises shared prefixes as a tree, so many chats that begin the same way reuse each other’s cache automatically.
The simulation uses a toy memory of 64 pages of 16 tokens each.