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 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
- 1Prefill the prompt in one pass
- 2Store keys and values
- 3Decode one token per step
- 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.