
The Memory Bill Behind Every Token
Why a longer conversation consumes serving memory even after the prompt is processed

Why a longer conversation consumes serving memory even after the prompt is processed
AI-assisted edition · Educational review score 95%
Why a longer conversation consumes serving memory even after the prompt is processed
Created by Bob · AI-assisted and reviewed before publicationA causal language model emits a response one token at a time. To choose the next token, each transformer layer lets the newest position attend to earlier positions. Those earlier tokens have already been transformed into key and value vectors. Recomputing those vectors from scratch at every step would repeat work for an unchanged prefix.
Serving systems separate prompt processing, often called prefill, from iterative decoding. Prefill processes the supplied context; decode appends one token, then another. The response may look like one fluid sentence, but underneath it is a loop whose state grows after every choice.

A key-value cache stores the earlier attention keys and values for every layer so decoding can reuse them. The newest token still compares its query with cached keys and combines the corresponding values, but the server avoids regenerating the old key-value pairs. This saves computation while consuming accelerator memory.
Cache size grows with sequence length, layer count, and the number and size of stored key-value heads. That's why a request with a long prompt or long answer can occupy much more serving memory than a short request, even when both use the same model weights. The cache is request state, not new knowledge learned by the model.

A server must fit model weights, temporary computation, and the caches of many active requests into finite memory. Cache allocations vary because prompts and answers have different lengths; unused gaps and over-reservation can waste capacity. PagedAttention applies an operating-system-like idea: split cache storage into blocks and map a sequence's logical blocks onto available physical blocks.
Blocks can be allocated as a request grows instead of reserving one large contiguous region. The design doesn't make memory free, and different runtimes use different strategies. It changes the packing problem. Production capacity planning needs a distribution of prompt and output lengths, not only a model parameter count.

These references were used to check the important factual claims in this edition.