Field manual · 11 min
How one GPU serves many chats at once: a PagedAttention teardown
A part-by-part teardown of PagedAttention, the memory trick behind vLLM.

When you send a message to an AI model in the cloud, you're not alone on that machine. The same GPU is usually working on a pile of other people's requests at the same time. How many it can handle at once decides how fast your answer shows up and how much the provider pays per answer.
For a while, the thing that capped that number wasn't the math. It was bookkeeping. Servers were reserving memory they never used.
This post takes apart the fix, a technique called PagedAttention, from the 2023 paper that introduced vLLM (a popular open-source program for serving models). I'll go one part at a time: the problem, the pieces, how they move, the clever bits, and what it costs. At the end there's a short script you can run to see the core idea with your own eyes.
Part 1: The thing being stored
What it is: the KV cache, the working memory a model keeps for every token in a conversation. A token is a chunk of text, roughly a word or part of one.
What's literally happening: each layer of the model turns every token into a few lists of numbers. Two of them, the key and the value, get looked at again every time the model writes a new token. Instead of recomputing them, the server stores them. That store is the KV cache, and it grows by one entry per token, for every layer.
The paper puts a number on it. For a 13 billion parameter model called OPT-13B:
2 (key and value) x 5,120 (numbers per token per layer) x 40 (layers) x 2 (bytes per number) = 819,200 bytes, about 800 KB per token.
That model handles up to 2,048 tokens, so one long request can need about 1.6 GB of cache. On a 40 GB NVIDIA A100 serving that model, the paper says about 65% of memory holds the model itself and close to 30% holds the cache. The rest is scratch space.
So the cache is the part that changes size, and it's the part that decides how many requests fit at once.
Part 2: The problem
What's literally happening: most machine learning software wants each block of data stored in one continuous stretch of memory. So older servers did the simple thing. When a request came in, they reserved one continuous chunk big enough for the longest possible answer, say 2,048 tokens, before generating a single word.
The trouble is that nobody knows how long an answer will be until it's done. Many answers end up far shorter than the max. The paper names three kinds of waste that come out of this:
- Reserved: slots held for tokens that haven't been written yet. They might get used eventually, but they block other requests the whole time.
- Internal fragmentation: slots reserved for tokens that never get written, because the answer ended early.
- External fragmentation: gaps between those chunks that are too small to fit anyone else's request.
When the authors profiled existing systems, only 20.4% to 38.2% of the cache memory held actual token data. The rest was reserved, stranded or never used.
Think of it like: a restaurant that holds a 12-seat table for every party that walks in, just in case their friends show up. Most parties are two people.
Part 3: The pieces
PagedAttention borrows an idea operating systems have used since the 1960s: virtual memory with paging. There are three parts.
1. Blocks. The cache for each request is cut into fixed-size blocks. Each block holds the keys and values for a set number of tokens. The paper settled on 16 tokens per block as its default.
2. Logical vs. physical. Each request sees its blocks as a neat, numbered list: block 0, block 1, block 2. Those are logical blocks. Where they actually sit in GPU memory (the physical blocks) can be anywhere, in any order.
3. The block table. A small lookup table for each request that says "logical block 0 lives at physical block 7, logical block 1 lives at physical block 1," plus how many slots in each are filled.
The paper spells out the mapping to operating systems directly: blocks are pages, tokens are bytes, requests are processes.
The attention step, the part of the model that looks back at earlier tokens, was rewritten so it can read keys and values from blocks scattered around memory instead of one continuous chunk. That rewrite is the "attention" in PagedAttention.
Part 4: How it moves
Here's the paper's own walkthrough, with a block size of 4 to keep it small.
- A request arrives with a 7-token prompt. The server doesn't reserve room for a 2,048-token answer. It grabs just enough blocks for the prompt: 2 blocks. Logical block 0 maps to physical block 7, logical block 1 to physical block 1. Block 0 is full (4 tokens). Block 1 has 3 tokens and 1 empty slot.
- The model writes its first new token. It goes in that one empty slot. The block table's "filled" count goes up.
- The model writes its second new token. Block 1 is full now, so the server grabs a fresh physical block (block 3), adds it to the table as logical block 2, and keeps going.
New blocks only get handed out when the last one is full. So the most any request can waste is the unused part of its final block. With 16-token blocks, that's at most 15 slots per request, instead of everything between the real length and the max.
When a request finishes, its blocks go back in the pool for the next request.
Think of it like: a parking garage that assigns spots one at a time as cars arrive, instead of blocking off a whole row because a tour bus might show up.
Part 5: The clever bits
Once memory is split into blocks with a lookup table in front, two requests can point at the same physical block. That opens up sharing.
Parallel sampling. Ask for several answers to one prompt and every answer shares the same prompt. With PagedAttention, the prompt's blocks are stored once and every answer's table points to them. Each physical block keeps a reference count, the number of tables pointing at it.
Copy-on-write. When one answer needs to write into a shared block, the server checks the reference count. If more than one answer is using that block, it copies the block first, gives the copy to the writer and lowers the count. It's the same trick an operating system uses when a program forks. Only one block ever gets copied, not the whole history.
The paper measured how much memory this saves (on OPT-13B):
- Parallel sampling: 6.1% to 9.8% on one dataset (Alpaca), 16.2% to 30.5% on another (ShareGPT).
- Beam search, a method that keeps several candidate answers alive and prunes them as it goes: 37.6% to 55.2% on Alpaca, 44.3% to 66.3% on ShareGPT.
Shared prefixes. Lots of requests start with the same long system prompt: the instructions and examples a company puts in front of every user's message. A server can compute that prefix's cache once, keep its blocks, and map every matching request to them. The paper compares this to a shared library that many programs load. In their translation test, sharing a 5-example prefix gave 3.58x the throughput of their strongest baseline.
This is the same idea behind the prompt caching discounts API providers offer today. Anthropic's docs, for example, say a cache hit requires "100 percent identical prompt segments" up to the cache point. The reason is in this mechanism: a token's keys and values depend on every token before it, so a stored block is only reusable when everything in front of it matches exactly.
Part 6: What happens when memory runs out
Even with blocks, a busy server can run out. PagedAttention's answer is preemption: pause some requests to make room.
- It evicts all of a request's blocks or none of them, since a request needs its whole history to continue.
- The paused request's blocks either get swapped out to regular CPU memory and brought back later, or simply deleted and recomputed when the request resumes.
Recomputing sounds wasteful, but it's faster than it looks. When the request comes back, the prompt and everything it already wrote can be fed through the model in one pass, the same fast way a prompt is processed. The paper found recomputation was the better choice with small blocks, and swapping with large blocks. In the middle range (16 to 64 tokens per block), the two came out about even end to end.
Part 7: What it costs
Nothing's free. Reading keys and values from scattered blocks means extra work: looking up the table, extra branches, handling different lengths. The paper measured the attention step running 20% to 26% slower than a highly tuned version that used continuous memory.
But attention is only one part of the model's work, and fitting far more requests in memory at once more than makes up for it. End to end, vLLM served 2 to 4 times the throughput of the leading systems at the time (FasterTransformer and Orca) at the same latency. Against FasterTransformer on the ShareGPT dataset, it handled up to 22 times the request rate.
Block size is a real trade-off too. Too small and the GPU can't read enough data in parallel. Too big and you're back to wasting the end of each block, and fewer blocks match exactly enough to be shared. On short requests, blocks larger than 32 tokens hurt performance noticeably.
Part 8: Why this matters if you never run a server
You might never touch vLLM. But this mechanism explains a few things you'll run into anyway:
- Why long context costs more than it looks like it should. Every extra token is cache that has to live somewhere and be read again on every new token.
- Why prompt caching wants an identical prefix. One changed character early in a prompt changes every key and value after it.
- Why agent builders now care about the order of things in a prompt. An October 2026 paper from a Tsinghua University team on "cache engineering" for agents points out that with standard caching, "the token prefix fully determines the cached states," which is exactly why editing early history forces everything after it to be recomputed.
Test it
You don't need a GPU to see the core idea. This script makes up 100 requests with realistic-ish lengths and counts how much memory each approach reserves:
import math, random
random.seed(0)
MAX_LEN = 2048 # what the old way reserves per request
BLOCK = 16 # tokens per block (the PagedAttention paper's default)
# 100 made-up requests: prompt + output lengths, usually far below the max
reqs = [(random.randint(50, 600), random.randint(20, 400)) for _ in range(100)]
used = sum(p + o for p, o in reqs)
reserved_old = len(reqs) * MAX_LEN
reserved_paged = sum(math.ceil((p + o) / BLOCK) * BLOCK for p, o in reqs)
print(f"tokens actually stored: {used:,}")
print(f"old way, slots reserved: {reserved_old:,} ({used / reserved_old:.1%} used)")
print(f"paged, slots reserved: {reserved_paged:,} ({used / reserved_paged:.1%} used)")
print(f"most waste per request when paged: {BLOCK - 1} slots")
When I ran it with Python 3, I got:
tokens actually stored: 52,624
old way, slots reserved: 204,800 (25.7% used)
paged, slots reserved: 53,328 (98.7% used)
most waste per request when paged: 15 slots
That 25.7% happens to land inside the paper's measured 20.4% to 38.2% range, but don't read too much into that. These are made-up lengths, and this script only models reserving for the max, not the fragmentation between chunks. The point is the shape: reserve-for-the-max wastes most of the memory, and blocks waste almost none.
Then play with it:
- Set
BLOCK = 128and watch the paged number drop. That's the block-size trade-off from Part 7. - Change the output range to
random.randint(1500, 2000). When answers really are long, the old way stops looking so bad. PagedAttention matters most when lengths are unpredictable.
If you want to go further, vLLM is open source and its docs cover block size and prefix caching settings for your version.
The whole machine, in order
- A request arrives. The server grabs only enough blocks for the prompt.
- The block table maps the request's tidy logical blocks to scattered physical ones.
- Each new token fills the last block. A new block is handed out only when it's full.
- Requests with the same prompt or prefix point at the same blocks. Copy-on-write splits them only when one needs to write.
- When memory runs out, whole requests get paused, then swapped back or recomputed.
- When a request finishes, its blocks go back in the pool.
None of it is exotic. It's a 60-year-old operating system idea, applied to a new kind of memory. That's my favorite kind of fix.
Sources