Prefill vs decode
Prefill reads the whole prompt in one parallel, compute-bound pass and sets TTFT; decode then emits one token per step, bound by memory bandwidth, and sets TPOT. Most serving optimizations target one phase.
Analogy: A kitchen preps every ingredient at once with all burners on (prefill, limited by burners), then serves a tasting menu one course at a time, walking to the pantry before each course (decode, limited by the walk).
How it works
- Batch-1 decode does ~1 FLOP per byte of weights read; an H100 needs ~300 to stay busy.
- Batching many sequences into each decode step recovers that idle compute.
- That is why APIs price output ~4–8× above input: each output token is its own sequential step.
- Mixing phases hurts: one long prefill entering the batch stalls every stream's decode.
- Fix it with chunked prefill (prompt chunks ride along with decode steps) or separate prefill and decode GPU pools.
Check yourself
Answer to mark it learned; your answers aren't scored.
A few-GPU vLLM deployment decodes ~60 chat streams at ~25 ms per token. Whenever a user pastes a 32k-token contract, every stream freezes and p99 inter-token latency jumps past 1 s. Best fix?
Go deeper
Full explanation
Every generation request runs in two phases with opposite bottlenecks.
Prefill runs all prompt tokens through one parallel forward pass. That's matrix–matrix work, thousands of FLOPs per byte of weights read, so it's compute-bound. It writes the prompt's KV cache and yields the first token, so it sets TTFT. A 32k-token prompt on an 8B model takes roughly a second of H100 time.
Decode emits one token per step per sequence. Each step streams every weight, plus the sequence's KV cache, from HBM to do very little math: ~1 FLOP per byte at batch 1, while an H100 needs ~300 to keep its tensor cores busy. So decode is memory-bandwidth-bound and sets TPOT. Batching many sequences into each step recovers the idle compute.
That's why APIs price output ~4–8× above input: each output token is a sequential step holding a batch slot and KV memory, while thousands of input tokens share one efficient pass.
Mixing phases causes interference: a long prefill entering the batch stalls everyone's decode. Two fixes:
- Chunked prefill: split long prompts into chunks (e.g., 512–2,048 tokens) that ride along with decode steps (Sarathi-Serve; on by default in vLLM V1).
- Disaggregated serving: separate GPU pools for prefill and decode, shipping the KV cache between them (DistServe, Splitwise, Mooncake; NVIDIA Dynamo, llm-d), so each scales for its own SLO.
Code Roofline check: why prefill and decode differ
# Which resource limits a forward pass: compute or memory bandwidth?
PEAK_FLOPS = 989e12 # H100 SXM, dense BF16
HBM_BW = 3.35e12 # H100 SXM HBM3, bytes/s
RIDGE = PEAK_FLOPS / HBM_BW # ~295 FLOPs per byte needed to saturate compute
def forward_pass(params_b: float, tokens: int, bytes_per_param: int = 2) -> str:
flops = 2 * params_b * 1e9 * tokens # ~2 FLOPs per param per token
bytes_read = params_b * 1e9 * bytes_per_param # weights read once per pass
intensity = flops / bytes_read
ms = max(flops / PEAK_FLOPS, bytes_read / HBM_BW) * 1e3 # slower roof wins
bound = "compute" if intensity > RIDGE else "bandwidth"
return f"{tokens:>5} tokens: {intensity:>5.0f} FLOP/B, >= {ms:.1f} ms, {bound}-bound"
# 8B model in BF16; ignores attention and KV reads (they make decode worse)
print(forward_pass(8, 2000)) # prefill, 2k prompt: 2000 FLOP/B, >= 32.4 ms, compute
print(forward_pass(8, 1)) # decode, batch 1: 1 FLOP/B, >= 4.8 ms, bandwidth
print(forward_pass(8, 64)) # decode, batch 64: 64 FLOP/B, >= 4.8 ms, bandwidth
# Batch 64 costs the same ~4.8 ms as batch 1 but yields 64 tokens:
# batching is the whole economics of decode. Prefill is already compute-bound.Real example
A support app runs Llama-3-8B on vLLM, decoding ~60 chat streams at ~25 ms per token. A user pastes a 32k-token contract. Its prefill needs ~1 s of H100 compute; done in one step, it freezes all 60 streams and p99 inter-token latency jumps from ~40 ms to over 1 s. With chunked prefill at 2,048 tokens per step, the contract spreads over 16 steps that also carry decode tokens: streams slow slightly instead of stalling, and the contract's TTFT grows only modestly. At hundreds of GPUs with many such prompts, a separate prefill pool starts to pay.
Trade-offs +3 · −3
+ You gain
- Tells you which knob fixes which metric: TTFT → prompt length, prefix caching, compute; TPOT → bandwidth, quantization, speculative decoding.
- Explains the price sheet: cheap input, expensive output, and cheapest cached input whose prefill is skipped.
- Separating the phases (chunked or disaggregated) removes interference and lets each use its own batch size and parallelism.
− You pay
- On a shared pool, long prefills stall everyone's decode unless prefill is chunked.
- Disaggregation must move the KV cache between GPUs: an extra hop that needs NVLink or RDMA-class networking.
- Two pools to size and autoscale, and the right prefill:decode ratio shifts with the prompt/output mix.
Use it when
Reason in these terms for any latency, cost or capacity question. Keep chunked prefill on by default; consider disaggregation at large scale with long prompts and strict, separate TTFT and TPOT SLOs.
Avoid it when
Don't disaggregate a few-GPU deployment or short-prompt chat: the KV transfer and second pool cost more than the interference they remove. Chunked prefill on one pool is enough.
Numbers to know 4
- Forward pass ≈ 2 FLOPs per parameter per token: an 8B model prefilling 2k tokens ≈ 32 TFLOPs.
- Batch-1 decode ≈ 1 FLOP per byte of FP16 weights; an H100 needs ~300 (989 TFLOPS ÷ 3.35 TB/s) to be compute-bound.
- Output lists at ~4–8× input (2025 list prices): GPT-4o $2.50 vs $10, Claude Sonnet $3 vs $15 per million tokens.
- DistServe: up to 7.4× more requests or 12.6× tighter SLOs than co-located serving, keeping >90% of requests within SLO.
Common pitfalls 4
- Blaming the model for slow TTFT when the cause is a 20k-token prompt; prefill scales with prompt length. Trim it or cache the prefix.
- Trying to fix TPOT with more FLOPs: decode wants bandwidth, smaller weights (quantization) or fewer steps (speculative decoding).
- Estimating cost from output tokens alone: in RAG and agents, input usually dominates the bill despite its lower unit price.
- Picking a prefill chunk size blindly: too large brings the stalls back; too small adds overhead and raises TTFT for long prompts.
Interview questions 2
Answer out loud first, then reveal.
Why are output tokens priced higher than input tokens?Show answer
Because they cost more GPU time. Input goes through one parallel prefill pass that keeps the tensor cores busy, so thousands of prompt tokens share each read of the weights. Every output token is a sequential decode step that rereads the weights and KV cache from HBM, and the sequence holds a batch slot and KV memory for its whole generation. Providers pass that on at ~4–8×. The design consequence: cache stable prompt prefixes, and treat output length as a cost and latency budget.
When would you disaggregate prefill and decode?Show answer
When the two SLOs fight and scale justifies it. On a shared pool a long prefill stalls decode, so I either over-provision or miss TPOT. First I'd enable chunked prefill, which fixes most of it on one pool. I'd disaggregate when prompts are long (RAG, agents, code), TTFT and TPOT targets are both tight, and we run enough GPUs to keep two pools busy, with NVLink or RDMA for the KV transfer. The price is a second pool to autoscale and a KV-aware router, e.g., NVIDIA Dynamo or llm-d.