When llm.asm claimed to beat llama.cpp by up to 36%, there was a catch. It was faster at writing an answer but about seven times slower at reading the prompt. Both are measured in tokens per second, and they're different jobs. This explainer covers the two phases of running a language model, the memory structure that connects them, and how to read benchmark numbers.

2
Phases of every LLM response: prefill and decode
NVIDIA
~2 GB
KV cache for Llama 2 7B at 4,096 tokens (FP16)
NVIDIA
2–4x
Throughput gain vLLM reports from PagedAttention
vLLM paper

What are prefill and decode?

A language model produces text one token at a time. A token is a word piece: NVIDIA's inference guide puts it at roughly "four English characters."

Prefill comes first. The model processes your entire prompt in one pass. NVIDIA describes this as "a matrix-matrix operation that's highly parallelized," which "effectively saturates GPU utilization." Every prompt token can be handled at the same time.

Decode comes next. The model produces one new token, adds it to the sequence, and repeats. Each step only processes one token, which NVIDIA calls "like a matrix-vector operation that underutilizes the GPU compute ability." Its verdict: "this is a memory-bound operation." For every token, the model's weights still have to be read from memory, so memory speed sets the pace.

What does the KV cache store?

Attention lets each new token look back at every earlier token. To do that it needs each earlier token's "key" and "value" vectors, at every layer. Recomputing those each step would mean, as a Hugging Face blog post puts it, that "each new prediction requires a full forward pass through all transformer layers."

So they're saved. Hugging Face's cache documentation says a KV cache "stores these calculations so they can be reused without recomputing them." Prefill fills the cache with the prompt. Each decode step adds one more token's entries.

How big does the KV cache get?

NVIDIA gives the formula: bytes per token = 2 × layers × (heads × head size) × bytes per value. The 2 is for keys and values. Its worked example, Llama 2 7B at 16-bit precision with 4,096 tokens, comes to "~2 GB" for a single user.

Line chart: KV cache memory for Llama 2 7B at 16-bit grows linearly from 0.25 GiB at 512 tokens to 2 GiB at 4,096 and 16 GiB at 32,768; the same model with 8 key/value heads instead of 32 would need a quarter of that

The cache grows "linearly with batch size and sequence length," as NVIDIA notes. Long chats, long documents and many simultaneous users all multiply it. That's why Hugging Face warns it can "become a bottleneck for long-context generation."

How does grouped-query attention help?

Standard attention gives every head its own keys and values. Grouped-query attention (GQA), from a 2023 paper, lets groups of query heads share one set: "more than one, less than number of query heads." The authors report quality "close to multi-head attention" with speed comparable to multi-query attention, where all heads share one set.

The cache shrinks in proportion. Qwen3-0.6B, the model llm.asm runs, uses 16 query heads but only 8 key/value heads, according to the comments in llm.asm's transformer code. That halves its cache. In our chart, a Llama 2 7B variant with 8 key/value heads instead of 32 would need a quarter of the memory. That's our own calculation, for illustration.

For servers handling many users, managing this memory is its own problem. The vLLM paper found cache memory "significantly wasted by fragmentation." Its PagedAttention system, which stores the cache in fixed-size pages, improved throughput 2–4x over earlier systems at the same latency.

How should you read "tokens per second"?

llama.cpp's benchmark tool, llama-bench, separates the two phases:

  • pp, "Prompt processing": "processing a prompt in batches." The default is 512 tokens, hence "pp512."
  • tg, "Text generation": "generating a sequence of tokens." The default is 128 tokens, hence "tg128."

Its README shows how far apart they are. In one GPU example, pp512 runs at 2,368.80 tokens/sec and tg128 at 131.42. Results also "do not include the times for tokenization and for sampling."

Two practical rules follow:

  • How long you wait before text appears depends mostly on prefill: long prompts take longer to start.
  • How fast the text streams is the decode rate, the "tg" number.

llm.asm shows why the label matters. Its README reports it beating llama.cpp at decode by 18% to 36% on a CPU. But because it reads prompts one token at a time instead of in batches, its prefill speed is 26.7 to 160.8 tokens/sec against llama.cpp's 56 to 1,113. For a chatbot with short prompts, llm.asm feels faster. For summarising a long document, llama.cpp wins easily.

One more caution: NVIDIA notes token counts "may not be equivalent if they use different tokenizers." Tokens per second across different model families isn't a like-for-like comparison.

FAQ

What is the difference between prefill and decode?

Prefill processes the whole prompt at once and is limited by compute. Decode generates the response one token at a time and is limited by how fast weights and the KV cache can be read from memory.

What is the KV cache?

Stored attention keys and values for every earlier token, at every layer, so the model doesn't recompute them for each new token. It grows as the conversation gets longer.

Why do long conversations use more memory?

The KV cache adds a fixed number of bytes for every token. For Llama 2 7B at 16-bit, that adds up to about 2 GB at 4,096 tokens.

What do pp512 and tg128 mean?

llama-bench's default tests: prompt processing of 512 tokens and text generation of 128 tokens, each reported in tokens per second.

What is grouped-query attention?

An attention design where several query heads share one set of keys and values. It cuts KV cache size with little loss in quality.

Reader poll

What feels slow when you run LLMs?

Vote to see results.

  • #llm-inference
  • #kv-cache
  • #benchmarks
  • #llama-cpp
  • #explainer