---
title: "Paged attention"
description: "Count useful and reserved cache slots, follow a block table, and see how paging supports growth and safe prefix sharing."
canonical_url: "https://fanout.sh/ai-engineering/lessons/paged-attention"
md_url: "https://fanout.sh/ai-engineering/lessons/paged-attention.md"
last_updated: "2026-11-11"
access: "public"
---

# Paged attention

Count useful and reserved cache slots, follow a block table, and see how paging supports growth and safe prefix sharing.

Three conversations are sharing a model server. One needs five cached tokens, another needs two, and the third needs nine. Their lengths will keep changing. How much memory should the server reserve for each?

Paged attention lets a sequence's key-value cache occupy separate fixed-size blocks. We will allocate a tiny cache by hand and see where the memory savings actually come from.

## Reserving the future

Start with an intentionally wasteful policy: give every request space for sixteen tokens, whether it currently needs that much or not.

Our three requests hold 5 + 2 + 9 = 16 useful cached tokens, but reserve 3 × 16 = 48 token slots. Two thirds of the reserved slots are empty. Other requests cannot use those slots while they belong to the existing requests.

Allocating exactly the current length is awkward too if every sequence must remain in one continuous region. When a region needs to grow, its neighboring memory may already be occupied. Free memory can exist in separated gaps without forming one large enough region.

These are related allocation problems: reserving too much space, leaving unused room inside reservations, and being unable to combine separate free regions.

## Logical order, physical freedom

Divide cache memory into blocks that each hold four tokens' keys and values. A request gets another block only when it needs it.

A five-token request needs two blocks. Its first four tokens might live in physical block 6 and its fifth in physical block 2.

A block table preserves their logical order: sequence block 0 maps to physical block 6, and sequence block 1 maps to physical block 2.

The attention kernel uses that table to find the keys and values. The tokens do not need to sit next to one another in physical memory to participate in the same attention calculation.

This is a design inspired by virtual memory. The “pages” here are software-managed KV blocks. It does not mean that an operating system automatically moves each attention access between a disk and the GPU.

Predict: our request grows from five to eight cached tokens. Does it need a third four-token block?

No. Its two blocks already contain eight slots. The next allocation happens when token nine needs cache space.

## Counting what remains unused

With four-token blocks, lengths 5, 2, and 9 require 2, 1, and 3 blocks. Six blocks hold 24 token slots. Sixteen are useful and eight are currently empty.

That is much better than the 48-slot reservation, but it is not zero waste. Each request's final block can be partly empty. We also need block tables and other serving state.

Smaller blocks reduce this last-block slack. They also increase the number of block entries and can change kernel efficiency. Choosing a block size is a system tradeoff, not a contest to choose the smallest possible number.

## Sharing an unchanged prefix

Two sequences may begin with identical token prefixes. If the cache is compatible and the serving system supports sharing, their block tables can point to the same physical blocks for that common prefix.

Suppose two requests share eight cached tokens. With our block size, both can point to the same two full prefix blocks. After they diverge, each needs its own subsequent cache entries.

If a shared block must be modified, the system needs to preserve the other sequence's view, often using copy-on-write: copy before making a change that should affect only one owner. Reference counts track whether another sequence still needs a block.

An identical word appearing later in two different histories is not necessarily shareable. Its cached representation depends on what came before it.

## What improves for a user

Less wasted cache memory can let the server keep more requests active. Throughput measures how much work the service completes over time. Latency measures how long an individual request waits or runs.

Higher throughput does not automatically mean a particular user sees tokens sooner. Larger batches, scheduling, prompt lengths, and queueing all affect latency. Paging mainly addresses cache management; it does not remove the arithmetic of attention.

For a useful experiment, keep the model, precision, prompt/output lengths, and request arrival pattern fixed. Compare completed requests per second, first-token delay, time between generated tokens, and peak memory. Increase concurrency in stages rather than reporting one favorable number.

For the requests holding 5, 2, and 9 tokens, paging reduced the allocation from 48 slots to 24 while keeping the same 16 useful tokens. Check for the same kind of unused reservation in your server before attributing a memory limit to the cache's useful contents.

## Lesson notes

### Key ideas

- KV fragmentation problem in naive serving
- Block tables: virtual-memory thinking for caches
- Prefix sharing across requests
- Throughput vs latency in batched serving

## Further reading

- Efficient Memory Management for LLM Serving (vLLM/PagedAttention)
- vLLM blog

---
This representation contains public Fanout content only. Protected Pro lessons, account data, billing, checkout, and pricing are not included.

Browse the public content map: https://fanout.sh/sitemap.md
