Walk through what actually happens during the prefill phase of an LLM forward pass.
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.
Detailed answer & concept explanation~8 min readEverything 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. 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.
- Anthropic and OpenAI both expose prompt caching, which skips the prefill compute for repeated prefixes; this would be impossible if prefill state were not a clean, reusable object.
- FlashAttention v2 and v3 (used in vLLM, SGLang, TensorRT-LLM) optimize the attention computation that dominates long-prompt prefill.
- Sarathi-Serve and DistServe split prefill into chunks and interleave them with decode from other requests to keep the GPU busy.
- Claude Sonnet 4.6 with a 200k context can prefill the full window in roughly 5-15 seconds on H100-class hardware before the first output token appears.
What an interviewer would ask next. Try answering before peeking at the approach.
QWhy is prefill compute-bound while decode is bandwidth-bound on the same hardware?
QHow does chunked prefill help overall throughput?
Don't say thisRed flags and common mistakes that signal junior thinking. Click to expand.
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.
Same topic, related formats. Practice these next.