Home / Glossary / Prefill and Decode

On this pageIn more detailWhy it mattersRelated termsRead more

Prefill and Decode

Prefill and decode are the two phases of text generation in a large language model. Prefill processes the input prompt. Decode produces the output, one token at a time.

In more detail

Prefill. The model reads the whole prompt in one pass. All input tokens are processed in parallel, which makes heavy use of the accelerator’s arithmetic capacity. This phase builds the KV cache and yields the first output token. The time it takes is experienced by the user as the wait before a response begins.

Decode. The model then generates tokens one after another. Each step takes the previous token, reads the cache and produces the next. The amount of computation per step is small, and the step has to read a large amount of cached data. This phase is limited mainly by memory speed, and it determines how quickly text appears once the response has started.

Why it matters

The two phases stress hardware in different ways. Prefill is compute-bound. Decode is memory-bound. A system tuned for one is not ideal for the other.

That has led to new approaches in serving models. Some systems run the two phases on separate pools of machines, each configured for its phase, and transfer the cache between them. This is called disaggregated serving. It improves efficiency and makes fast movement of cache data across the network important.

The split also explains two common measures of AI inference performance: time to first token, governed by prefill, and tokens per second, governed by decode.

Related terms

Read more