Walk through what actually happens during the prefill phase of an LLM forward pass.
Same topic, related formats. Practice these next.
Same topic, related formats. Practice these next.
Prefill is the one-shot parallel forward pass that processes the whole prompt at once, builds the KV cache, and produces the first output token.
Picture someone settling down to read a long book and then answer a question about it. They first read the entire book end to end, taking notes on each chapter so they can flip back without re-reading. That note-taking pass is prefill. Once the notes are ready, answering follow-up questions is fast because they consult the notes instead of re-reading the book. For an LLM, the notes are the KV cache, the book is the prompt, and the first sentence of the answer is the first thing they say after finishing the read. Prefill is one big concentrated reading session; decoding the answer afterward is many small consultations of the notes.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
3 min: parallel pass over the prompt + KV cache population + first token emission + arithmetic intensity contrast with decode + role in TTFT + chunked prefill and prompt caching as the two main optimizations.
| Aspect | Prefill | Decode |
|---|---|---|
| Compute pattern | Parallel over prompt length T | Sequential, one token per step |
| Arithmetic intensity | High (hundreds of FLOPs/byte) | Low (~1 FLOP/byte) |
| Bottleneck | Compute-bound (tensor cores) | Bandwidth-bound (HBM) |
| KV cache role | Writes the entire cache at once | Appends one K/V pair per step |
| Latency contribution | Dominates TTFT | Dominates total generation time |
Real products, models, and research that use this idea.
What an interviewer would ask next. Try answering before peeking at the approach.
Red flags and common mistakes that signal junior thinking. Click to expand.
Treating prefill and decode as 'the same forward pass'. They have different shapes, different bottlenecks (compute-bound vs bandwidth-bound), and different optimization techniques.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.