Sub topic 10
Prompt processing and token generation are different workloads
This chapter assumes you know what a language model is, can run commands in a terminal, and have read the earlier chapters on weight memory, the KV cache, and memory bandwidth. It covers why one model shows two very different speeds.
Two phases of every request
Every request to a local model runs in two phases.
- Prompt processing (also called prefill) reads your whole prompt and builds the KV cache for it. The model sees all prompt tokens at once.
- Token generation (also called decode) produces the reply. It makes one token at a time, and each new token needs the one before it.
Tools report these as two numbers. In llama.cpp they appear as pp (prompt processing) and tg (token generation), both in tokens per second.
Why prefill depends on compute
During prefill, the model processes hundreds or thousands of tokens in one batch. It reads each weight from memory once, then uses it for every token in the batch. The memory traffic is spread across many tokens, so the arithmetic becomes the limit. A GPU with many compute units does very well here. This is why prompt processing is often ten to a hundred times faster than generation, measured in tokens per second.
Why decode depends on memory bandwidth
During decode, the batch is a single token. The model still has to read about all of its weights to produce that token, but it does only a small amount of arithmetic with them. The compute units spend most of their time waiting for data. The speed limit is memory bandwidth, which is why the earlier ceiling formula (bandwidth divided by model size) predicts generation speed and not prompt speed.
| Row | Prefill | Decode |
|---|---|---|
| Tokens per pass | Many | One |
| Main limit | Compute | Memory bandwidth |
| Typical speed | Hundreds to thousands of tokens/s | Single digits to tens of tokens/s |
| Grows with | Prompt length | Reply length |
What this means in practice
A long prompt, such as a pasted document, costs prefill time before the first word appears. A long reply costs decode time. Two machines can rank differently on each. A GPU with strong compute but modest bandwidth may load a long prompt quickly and still generate at a middling speed. A machine with high bandwidth and weak compute may do the reverse.
You can estimate total response time from the two speeds:
def response_seconds(prompt_tokens, reply_tokens, pp_tok_s, tg_tok_s):
prefill = prompt_tokens / pp_tok_s
decode = reply_tokens / tg_tok_s
return prefill + decode
# A 6000-token document and a 300-token summary
print(response_seconds(6000, 300, pp_tok_s=400, tg_tok_s=20))This prints 30.0: 15 seconds of prefill and 15 seconds of decode. Here a short question about a long document is dominated by prefill, so decode speed alone would mislead you.
Common errors
- Quoting one speed for a machine. Always ask whether a number is prompt processing or generation.
- Applying the bandwidth ceiling to prefill. It bounds decode only.
- Ignoring prompt length when judging responsiveness in chat tools that resend a long history.
Exercise
A model prefills at 500 tokens/s and generates at 10 tokens/s. You send a 2000-token prompt and get a 200-token reply. How long does the response take, and which phase takes longer?
Show answer
Prefill takes 2000 / 500 = 4 seconds. Decode takes 200 / 10 = 20 seconds. The total is 24 seconds, and decode takes longer.
Exercise
Why does adding a faster GPU compute unit help prompt processing more than token generation?
Show answer
Prefill reuses each weight across many tokens, so arithmetic is the limit. Decode handles one token per pass and waits on memory reads, so it is limited by memory bandwidth, which extra compute does not raise.