Sub topic 10
Prompt processing and token generation are different workloads
This chapter assumes you can estimate weight memory and KV cache size from the earlier chapters. It also assumes you know that memory bandwidth is the rate at which a processor can read data from memory. Here you will learn why a local model has two different speeds, which hardware limit applies to each, and how that changes what you buy and measure.
Two phases in every request
Every request to a local model runs in two phases.
- Prompt processing (also called prefill) reads your whole prompt, including the system prompt, chat history and any pasted documents. It computes the attention keys and values for every prompt token and stores them in the KV cache.
- Token generation (also called decode) produces the reply one token at a time. Each new token needs the full model, and it depends on the token before it.
The two phases use the same weights, but they put different loads on the hardware. Tools report them as two separate numbers. A "prompt eval" rate of 400 tokens per second and a "generation" rate of 20 tokens per second on the same machine is normal.
Why prefill depends on compute
During prefill, the model already has all the prompt tokens. It can process hundreds of them in one batch. Each weight is read from memory once and then used for every token in the batch.
That reuse is the key point. The work is mostly large matrix-by-matrix multiplication. Many arithmetic operations happen for each byte read. The limit becomes how fast the chip can do arithmetic, which is measured in FLOPS (floating-point operations per second).
A rough rule: a forward pass costs about 2 operations per parameter per token. For an 8B model, that is about 16 GFLOP per prompt token. At an effective 4 TFLOPS, the ceiling is about 250 tokens per second. A GPU with an effective 40 TFLOPS gives about 2,500 tokens per second. The numbers are illustrative. Real throughput depends on the kernels, the quantisation format and the batch size.
Why decode depends on memory bandwidth
During decode, each pass handles one new token (for a single user). The model must still read nearly every weight, but now it does only about one multiply-add per weight it reads. The chip spends most of its time waiting for data.
So the first-order limit is:
- tokens per second ≤ memory bandwidth ÷ bytes read per token
For a dense model, bytes read per token is about the model file size. This is the ceiling from the earlier code example, and the next chapter develops it fully. The consequence for this chapter is that the same GPU can be compute-bound in prefill and bandwidth-bound in decode.
The KV cache adds to the bytes read per token. Each decode step also reads the whole cache for attention. At short context the cache is small next to the weights. At long context it can become a noticeable share of the traffic, so generation slows as the conversation grows.
Comparing the two phases
| Property | Prompt processing (prefill) | Token generation (decode) |
|---|---|---|
| Tokens handled per pass | Many, in a batch | One |
| Work per byte of weights read | High | Very low |
| Usual limit | Compute (FLOPS) | Memory bandwidth |
| What you feel | Wait before the first token | Speed at which text appears |
| Grows with | Prompt length | Reply length, plus context size |
| Hardware that helps most | More GPU compute | Higher bandwidth, smaller file |
The wait before the first token is called time to first token. It is roughly prompt length divided by the prompt processing rate.
A worked example
Consider a machine with two measured rates for an 8B model at Q4KM: 300 tokens per second for prompt processing and 18 tokens per second for generation. You paste a 6,000-token document and ask for a 400-token summary.
def request_time(prompt_tokens, output_tokens, prefill_tps, decode_tps):
ttft = prompt_tokens / prefill_tps # wait before first token
generation = output_tokens / decode_tps # time to stream the reply
return ttft, generation, ttft + generation
ttft, gen, total = request_time(
prompt_tokens=6000,
output_tokens=400,
prefill_tps=300,
decode_tps=18,
)
print(f"time to first token {ttft:.1f} s")
print(f"generation {gen:.1f} s")
print(f"total {total:.1f} s")The output is 20.0 s for time to first token, 22.2 s for generation and 42.2 s in total. Both phases matter here. If you chat in short turns with a small prompt, decode dominates. If you feed long documents, prefill can dominate. Retrieval pipelines and coding assistants often send thousands of tokens of context for each short answer, so prefill speed matters more for them than the generation speed suggests.
Hardware consequences
The two limits explain several patterns that surprise newcomers.
- A high-bandwidth, modest-compute machine feels fast when chatting but slow on long prompts. Many unified-memory systems fit this description. Generation is smooth, but a 20,000-token prompt can take minutes.
- A GPU with strong compute but a model split across GPU and CPU can have fast prefill and poor decode. Layers left in system RAM are read at system RAM bandwidth. Decode speed is pulled toward that slower rate.
- A smaller quantisation speeds up decode almost in proportion to file size. It helps prefill much less, because prefill is not waiting on memory. Some quantised formats also need extra work to unpack weights, which can reduce prefill speed.
- Batching several users helps decode a lot. With several requests in one pass, each weight read serves several tokens. Server software uses this to raise total throughput. A single local user rarely gets this benefit.
Edge cases and common mistakes
- Reporting one speed. A claim of "30 tokens per second" without a prompt length and a phase is not useful. Ask which phase, which quantisation and what context size.
- Prompt caching hides prefill. Many runtimes reuse the KV cache when the start of a new prompt matches the previous one. In a chat, only the new turn is processed, so prefill looks cheap. The first request after loading the model, or an edit near the top of the prompt, forces a full prefill.
- Ignoring context growth. Decode speed measured with an empty context overstates speed late in a long session.
- Assuming the GPU's FLOPS figure applies. Advertised peak FLOPS are rarely reached. Treat them as an upper bound, as you do with bandwidth.
- Mixing up time to first token and generation speed. A system can start streaming quickly and then stream slowly, or the reverse. They need separate checks.
What to take into the next chapter
Generation speed is set mainly by bandwidth and file size. Prompt speed is set mainly by compute. When you evaluate hardware, decide first which phase your workload spends its time in. The next chapter turns the decode half of this picture into a number you can calculate.
Exercise
A machine measures 500 tokens per second for prompt processing and 25 tokens per second for generation. You send a 4,000-token prompt and receive a 250-token reply. How long is the wait before the first token, how long does the reply take to stream, and which phase takes more of the total time? Then say what you would change if most of your requests looked like this.
Show answer
Time to first token is 4,000 ÷ 500 = 8 s. Streaming the reply takes 250 ÷ 25 = 10 s. The total is 18 s, and generation takes slightly more of it. For requests like this the two phases are close, so both matter. A faster-compute GPU would cut the 8 s, and higher memory bandwidth or a smaller quantisation would cut the 10 s. If prompts grew to 20,000 tokens, prefill would take 40 s and would dominate, so compute would become the priority.
Exercise
A model is split so that some layers run on the GPU and the rest run on the CPU from system RAM. Why might prompt processing stay fast while generation becomes slow?
Show answer
Prefill reuses each weight across many tokens, so it is limited by compute, and the GPU's compute covers much of the work. Decode reads all weights for every token, so it is limited by bandwidth. The layers in system RAM are read at the lower system RAM bandwidth on every token, and that slower read pulls the overall generation speed down.