Attention is O(n²) in sequence length. Where does that actually bite in practice - prefill vs decode?
Type it the way you would explain it to an interviewer. A few sentences is enough; you can't break anything.
Model answer: the part to lead with
The n×n score matrix means attention FLOPs scale quadratically with sequence length, but where that hurts differs by phase, and strong candidates separate them.
Full worked answer with diagram
The n×n score matrix means attention FLOPs scale quadratically with sequence length, but where that hurts differs by phase, and strong candidates separate them.
Prefill (processing the prompt): genuinely quadratic compute. All n tokens attend to all prior tokens in parallel - total attention FLOPs ~O(n²·d) per layer. Doubling prompt length roughly quadruples attention prefill work (the MLP part scales linearly, so the blended exponent depends on length - at short contexts MLPs dominate; at 100k+ attention dominates). This is why time-to-first-token grows superlinearly for long prompts, and why serving stacks use chunked prefill to avoid stalling decode traffic behind a 100k-token prompt.
Decode (generating token-by-token, with KV cache): each step is O(n·d) - the new token attends over n cached entries. Quadratic total across a full generation, but the per-step cost is linear in context. The real constraint here isn't FLOPs at all - it's memory bandwidth and capacity: every step must stream the entire KV cache from HBM, and the cache itself grows linearly with n (that ~320 KB/token for a 70B-class GQA model → ~40 GB at 128k). Long context at decode time is a memory problem.
Consequences that follow: (1) long-context pricing - some providers price long-context requests higher because prefill compute and cache memory both balloon; (2) architectural responses - GQA/MQA/MLA shrink the cache, sliding-window attention (Mistral-style) bounds it, hybrid SSM-attention models attack both; (3) FlashAttention fixes attention's memory footprint (no materialised n² matrix) and IO but not the FLOP count - prefill is still quadratic; (4) product implications - "just stuff everything in context" has a real quadratic cost floor, which keeps RAG and context curation economically relevant even as windows grow.
Worth sketching. Splitting the two phases before you talk shows the interviewer you know where the bottleneck actually sits.
flowchart TD
R["request"] --> PF["prefill: n prompt tokens at once"]
PF --> PC["attention FLOPs grow as n^2<br/>compute-bound, this is TTFT"]
R --> DC["decode: one token per step"]
DC --> DM["O(n) per step, but the real cost is<br/>streaming a cache that grows with n"]
DM --> DB["memory-bandwidth-bound, this is ITL"]
PC --> F1["answers: chunked prefill, FlashAttention"]
DB --> F2["answers: GQA, MLA, sliding window,<br/>KV quantization"]
What a good answer covers
- Prefill (processing the prompt): genuinely quadratic compute
- All n tokens attend to all prior tokens in parallel - total attention FLOPs ~O(n²·d) per layer
- Doubling prompt length roughly quadruples attention prefill work (the MLP part scales linearly, so the…
- This is why time-to-first-token grows superlinearly for long prompts, and why serving stacks use chunked…
- Decode (generating token-by-token, with KV cache)
Key terms: Prefill · Decode · memory bandwidth and capacity
In the glossary: Prefill · Chunked prefill · Decode · MQA · MLA · FlashAttention
Interviewer follow-ups to expect
Why is decode memory-bandwidth-bound - walk me through the arithmetic intensity?
What does sliding-window attention give up?
Why doesn't FlashAttention change the asymptotic FLOP complexity?
Worked answer from AI-Engineer-Interview-Questions by Om (MIT), reformatted for LLM Prep · answer text last updated 6 November 2026 · scoring and review schedule by LLM Prep