How it works · 32 of 49 Advanced

How does LLM serving work: batching, KV cache, prefill and decode?

Batching, KV cache, prefill vs decode

LLM serving: Serving a model to many users is a memory problem before it is a compute problem. Most of a GPU's memory holds the model's weights; the rest holds each request's KV cache, the keys and values of every token so far, which grows by one token per step. Reading the prompt (prefill) happens in one parallel pass; writing the answer (decode) is one token per step, slow per request, so servers batch: one step moves every running request forward one token, and finished requests leave between steps. How many fit in a batch is decided by how the KV memory is handed out: reserve the maximum length per request and most of it sits empty; hand it out in small pages on demand and two to four times as many requests share the same GPU.

Loading the animation…

Say it like this Serving a model is mostly a memory problem: each request's KV cache grows a token at a time beside the weights, the server batches every request one token per step, and handing that memory out in small pages instead of reserving the maximum fits two to four times as many users on the same GPU.

Under the hood — for builders
The source, in its words
Kwon, Li and others ("Efficient Memory Management for Large Language Model Serving with PagedAttention", SOSP 2023), a 13B model on an A100 40GB: "Approximately 65% of the memory is allocated for the model weights"; "Close to 30% of the memory is used to store the dynamic states of the requests".
Terms
KV cache: each token's attention keys and values, kept so they aren't recomputed. Prefill: reading the prompt in one parallel pass. Decode: one new token per step. Batch: requests run in the same step.
The source's sum
OPT-13B: 2 (key and value) × 5,120 × 40 layers × 2 bytes = "800 KB" a token; 2,048 tokens "can be as much as 1.6 GB". So a 12 GB slice holds about 7 requests at full length.
Reserved vs used
Older servers "pre-allocate a contiguous chunk of memory with the request's maximum length". PagedAttention hands out small blocks on demand: "blocks as pages, tokens as bytes, and requests as processes".
Run here: a busy server
A 15,000-token KV budget (12 GB at 800 KB a token), prompts 20–600 and answers 20–1,000 tokens, more arrivals than it can serve. Fixed reserves 2,048; paged uses 16-token blocks.
Prefill vs decode
Paper: the prompt phase "can be parallelized"; generation "becomes memory-bound". Long prompts slow the first token; long answers slow everything after.
Batching
Iteration-level: "After each iteration, completed requests are removed from the batch, and new ones are added", so no one waits for a whole batch.
In our papers
nvidia-dynamo-fast-ai-serving-recovery: a ready replacement engine took over in 5.8 s against a 148 s restart, and "uses memory that could otherwise support requests".
Measure it
Time to first token (prefill) · time per output token (decode) · tokens per second across users · KV memory used · queue length.
Watch out
Long contexts take whole rows of bays; a bigger batch raises throughput but each user's step slows. The scene draws 256-token blocks; ours is a toy.
Sources
Kwon, Li, Zhuang, Sheng, Zheng, Yu, Gonzalez, Zhang and Stoica, SOSP 2023, arXiv:2309.06180, read in session · paper nvidia-dynamo-fast-ai-serving-recovery, checked 19 Nov · serving simulation, Python, 19 Nov 2026.

What happens in the LLM serving animation, step by step?

  1. One 40 GB GPU serving a 13-billion-parameter model: 65% holds the weights, about 30% the requests' working memory, the KV cache.
  2. Each request is a car. The prompt is read in one pass (prefill); the answer then grows one token per step (decode), and every token stays parked: 800 KB a token for this model.
  3. Decode is slow per car, so the server moves the whole batch one token per step; a finished car leaves after any step and a waiting one joins.
  4. Your turn: drag the users below. Bays reserved for the longest possible answer: 8 fit, most of each bay sits empty, the rest wait.
  5. Paged: memory handed out in small blocks as each car grows. vLLM held 2.2× to 4.3× more requests at once and served 2–4× the throughput.
  6. In our papers: NVIDIA Dynamo keeps a spare engine ready to recover in 5.8 s, not 148 s, using memory that could hold requests. Next: what an agent remembers.

What everyday example explains LLM serving?

A car park that paints every bay for the longest lorry, vs one that lets cars park nose to tail and paints a new line only when a car needs it.

Why should a team care about LLM serving?

"why does our self-hosted model get slow with ten users?" is a memory question; the serving engine (vLLM, SGLang, TensorRT-LLM) matters as much as the GPU, and every feature that holds memory (a long context, a spare engine) costs batch places.