Skip to content
StackDecode
LLM Library
Running and Using LLMsAnimated

Prefill and Decode

The two phases of answering a prompt.

The idea

Prefill processes the whole prompt in parallel and builds the KV cache, which sets time to first token. Decode generates one token at a time and is limited by memory bandwidth, which sets speed.

Watch it work

Press play, pause, or step through. Some animations have settings you can change.

PREFILL: the whole prompt is processed in parallelp1p2p3p4p5p6

Prefill reads the whole prompt at once. It is compute-bound, and sets time-to-first-token.

generated:
0
1/7

Prefill and decode have different bottlenecks, which is why serving systems treat them differently.

Step by step

  1. 1Prefill the prompt in one pass
  2. 2Store keys and values
  3. 3Decode one token per step
  4. 4Stream tokens as they appear

Where it is used

All LLM serving.

Watch out for

Long prompts make the first token slow; long answers make the total slow.