In this article
  1. Before you start
  2. Why interviewers ask this
  3. Context length from first principles
  4. The Staff-level answer
  5. The rubric
  6. Three variants of this question
  7. Common mistakes
  8. Follow-up questions
  9. Sources

Before you start

This page sits inside the transformer architecture guide, and the neural-network basics are in how neural networks work.

Why interviewers ask this

A candidate who says "attention is quadratic" and stops has named one limit out of four. The question is reported by candidates in large language model (LLM) and system-design rounds, and it separates people who know the formula from people who have run a model at length.

A weak answer picks a single cause, usually the quadratic compute, and treats longer context as a pure speed problem. A strong answer separates memory, compute, position handling and quality, and says which one binds first for the situation described. A Staff-level answer also says that the order depends on the setup: training and serving differ, batch size matters, and the answer changes once a fast attention kernel or a smaller cache is in use. The honest framing is "typically", because no single published ordering covers every model and hardware choice.

Context length from first principles

Score matrix
A score matrix is the table of attention scores that one attention head computes in one layer, with one row and one column per token, so n tokens give n times n entries before the softmax turns each row into weights.
Effective context length
The effective context length of a model is the longest input over which it still does a given task at acceptable quality, which can be shorter than the longest input it accepts.

In plain words

Picture a student reading a long report. For every page, the student compares it with all earlier pages. That is the quadratic part. The student also keeps one note card per page so that earlier pages need not be reread. That is the cache. The analogy breaks in one place: a model does this comparison in every layer and in every head, in parallel.

In one precise sentence, longer context raises the memory of attention scores and of cached keys and values, raises the compute per generated token, and pushes the model into position ranges and use patterns where it was trained little or not at all.

Step 1: Naive attention stores an n-by-n score matrix, and FlashAttention removes only that

Naive attention stores a score matrix of n squared entries per head and layer. Here n is the number of tokens. The self-attention cost per layer grows with n squared, as in the complexity table of Vaswani et al. (2017).

Take a Llama-2-7B-style configuration (illustrative): 32 layers, 32 heads, head size 128, 16-bit numbers. At 4,096 tokens one such matrix is 32 MiB. At 131,072 tokens it is 32 GiB, and at 1,048,576 tokens it is 2,048 GiB.1 That is the first thing that fails if attention is written naively.

FlashAttention removes the stored score matrix but not the quadratic compute. Dao et al. (2022) present an exact attention method that avoids writing the full matrix to GPU memory, so this wall moves away. The arithmetic does not shrink. The FlashAttention page explains the method, and the page on making attention cheaper lists the alternatives.

Step 2: The KV cache grows linearly and sets how many requests fit

During generation the model does not recompute the keys and values of earlier tokens. A KV cache is a per-layer memory of each processed token's attention keys and values, kept so every new step can reuse them instead of redoing that work. It grows linearly with context length and with batch size.

Its size is 2 × layers × KV heads × head size × tokens × bytes per number × batch. For the illustrative configuration this is 524,288 bytes per token: 2 GiB for one sequence of 4,096 tokens, 64 GiB (about 69 GB) at 131,072 tokens and 512 GiB at 1,048,576 tokens.1

Take an 80 GB GPU holding about 14 GB of 16-bit weights for a model of about 7 billion parameters (illustrative round figures), which leaves about 66 GB. That fits 30 sequences of 4,096 tokens, 3 of 32,768 tokens and none of 131,072 tokens.1 The cache stays resident for the whole request, so a longer context first cuts the batch, and so the throughput per GPU.

KV-cache memory of one sequence grows linearly with contextBar chart of the KV cache of one sequence for an illustrative 7B-style model in 16-bit numbers: 2 GiB at 4,096 tokens, 16 GiB at 32,768, 64 GiB at 131,072 and 512 GiB at 1,048,576.KV cache of one sequence (GiB)02004006004,09632,768131,0721,048,576Context length (tokens)21664512Diagram: Interview Clozo, CC BY 4.0 ·interview.clozo.ai/blog/what-breaks-first-when-you-push-context-length-way-up.html
The cache grows linearly; at 131,072 tokens one sequence's 64 GiB (about 69 GB) exceeds the roughly 66 GB left after the weights.CC BY 4.0 — source: What breaks first when you push context length way up?
Text description of the diagram
  • The KV cache of one sequence is 2 GiB at 4,096 tokens and 512 GiB at 1,048,576 tokens.
  • The cache is 64 GiB at 131,072 tokens.

The KV cache of one sequence is 2 GiB at 4,096 tokens and 512 GiB at 1,048,576 tokens.1

Step 3: The cost of every generated token climbs

Each new token must read the whole cache, plus the weights. At an illustrative memory bandwidth of 2 terabytes per second, reading a 64 GiB cache takes about 34 milliseconds per generated token, against about 1 millisecond for a 2 GiB cache.1 The 14 GB of weights add a fixed read of about 7 milliseconds, so the part that grows is the cache read, and latency per token rises in a straight line with context, even when everything fits.

Attention compute has two forms. Per generated token, attention reads and multiplies against the whole cache, about 4 × n × d floating-point operations (FLOPs) per layer, with d the model width, 4,096 here (illustrative), so it grows linearly with n.

Over a whole input (prefill or training), the scores plus the weighted sum cost about 4 × n² × d FLOPs per layer: about 275 billion at 4,096 tokens and about 281 trillion at 131,072 tokens, 1,024 times more for 32 times the length.1 As a share of per-token FLOPs, from 4nd attention against about 24d² for the projections and the feed-forward layer (illustrative), attention is about 14% at 4,096 tokens, 57% at 32,768, 84% at 131,072 and 98% at 1,048,576.1

For the illustrative 7B-style model, one sequence's cache outgrows the memory left after the weights on an 80 GB GPU between 32,768 and 131,072 tokens.
Context lengthKV cache, one sequenceScore matrix, one head, fp16Cache read per tokenSequences in the 66 GB left
4,0962 GiB32 MiB1.07 milliseconds30
32,76816 GiB2 GiB8.59 milliseconds3
131,07264 GiB32 GiB34.36 milliseconds0
1,048,576512 GiB2,048 GiB274.88 milliseconds0

Step 4: Positions and quality fail without a crash

A model that accepts a long input has not necessarily learned to use it. Models handle position with schemes such as rotary positional embedding (RoPE), and positions far beyond the trained range are unfamiliar; extension methods rescale or interpolate positions to cover them, as in Chen et al. (2023). The RoPE page shows why.

Even inside the trained range, use is uneven. Liu et al. (2023) note that relatively little is known about how well models use longer context. On multi-document question answering and key-value retrieval, they find that accuracy is often highest when the relevant passage is at the start or end of the input and drops significantly when it sits in the middle.

This failure is silent: the model still answers, and nothing errors. So the effective context length can be shorter than the advertised window, and only a test on your own task shows it.

Context length hits four separate limits, not oneRow flow of four limits as context grows: the attention score matrix, the KV cache, the cost of each generated token, and position handling and quality. The KV cache is highlighted as the one that fills GPU memory first.Score matrixn by n per headKV cachegrows with nand batchPer-tokencostcache reads,attentionPosition andqualitybeyond trainedrangeDiagram: Interview Clozo, CC BY 4.0 ·interview.clozo.ai/blog/what-breaks-first-when-you-push-context-length-way-up.html

Context length meets four different limits, and the KV cache is the one that fills the GPU first in a typical serving setup.CC BY 4.0 — source: What breaks first when you push context length way up?
Step-by-step description
  1. Naive attention stores n squared scores.
  2. The cache grows linearly and cuts the batch.
  3. Each token reads the whole cache.
  4. Long input may not be used well.
  • Naive attention stores a score matrix of n squared entries per head and layer.
  • The KV cache grows linearly with context length and with batch size.
  • FlashAttention removes the stored score matrix but not the quadratic compute.

Knowing it is half the job. Saying it is the other half.

Interview Clozo is an AI interview copilot for real interview rounds: it hears the question and puts a strong answer on your screen, and you say it in your own words. See the features or how it works.

Start Free Trial →

The Staff-level answer

Interviewer asks

What breaks first when you push context length way up?

A strong answer

Usually memory, not compute. Naive attention stores an n-by-n score matrix per head, which explodes first, but FlashAttention removes that. What remains is the KV cache: it grows linearly with context and batch size, so long contexts cut how many requests fit on a GPU. Then per-token latency rises because every step reads the whole cache, and the cost of processing a long prompt grows quadratically. Last, quality: positions beyond the trained range and weak use of the middle of the input mean the effective context is shorter than the advertised one. I would measure it on the real task.

What makes it Staff-level: it orders the failures, names which fix removes which wall, and ends with a measurement rather than a slogan.

The rubric

A strong answer separates four limits, and a Staff answer adds which fix removes which one.
DimensionWeak answerStrong answerStaff answer
CorrectnessSays attention is quadratic and stopsNames score matrix, cache, compute and positionsAlso says which wall FlashAttention removes and which it leaves
DepthQuotes no sizesGives the cache formulaPlugs a real model configuration into it
Trade-offsTreats long context as freeSays memory cuts the batchNames fewer KV heads, cache quantisation or paging as fixes, and their quality cost
CommunicationLists termsOrders the limitsSays "typically" and what would change the order
Follow-up readinessStuck on "why not FlashAttention"Explains that FlashAttention is exact and saves memory but not FLOPsProposes measuring the effective length on the task

Three variants of this question

What if the interviewer says FlashAttention already solved this?

It solved the score-matrix memory wall only. The compute stays quadratic and the KV cache is unchanged, so serving a long context still costs memory and time per token.

What if the model fits everything in memory but answers badly?

Then quality is the limit. Positions may lie outside the trained range, or the model may use the middle of the input poorly, as Liu et al. (2023) found for passages in the middle. Test the task at several lengths and positions.

What if you only have one GPU and a one-million-token document?

The illustrative cache for one sequence is 512 GiB, far above one card.1 Split the document, retrieve only the relevant parts, or use a smaller cache design. At Staff level, add that sequence or ring parallelism spreads one sequence across several GPUs, and that training also needs activation memory on top of the cache.

Common mistakes

  • "FlashAttention makes attention linear." (wrong) It makes the stored memory for scores linear and leaves the compute quadratic. (right) "It is still exact and quadratic in compute; only the score memory goes away."
  • "Compute is the only limit on context." (wrong) The cache and the positions bind too. (right) "Memory, per-token cost and position handling all limit it, typically in that order."
  • "A big window means the model uses all of it." (wrong) Use can be uneven across the input. (right) "The effective context length is what I measure on my task."
  • "Count only the weights when sizing a server." (wrong) The cache can exceed the weights. (right) "Weights plus cache per request times batch."

Follow-up questions

How does grouped-query attention change the answer?

Grouped-query attention (grouped-query attention (GQA), Ainslie et al., 2023) is a design in which several query heads share one set of keys and values, so fewer KV heads are stored. With 8 instead of 32 KV heads in the illustrative configuration, the cache at 131,072 tokens falls from 64 GiB to 16 GiB.1 The multi-head attention page shows the baseline.

Does a bigger context window mean the model uses all of it?

No. The window is what the model accepts; the effective context length is what it uses well, and it depends on the task.

How do you extend a model beyond the length it was trained on?

Methods rescale or interpolate positions so the model sees familiar ranges, usually with some further training, as in position interpolation. Check quality at the new length, because acceptance is not use.

Is the quadratic compute or the cache the real bottleneck?

For serving, usually the cache first, because it limits the batch. For long single prefills, the quadratic compute dominates. More LLM practice is in the LLM interview questions.

Key takeaways
  • Context length meets four limits: score matrix, KV cache, per-token cost, and positions and quality.
  • FlashAttention removes the stored score matrix but not the quadratic compute.
  • The KV cache grows linearly with context and batch size, so it typically cuts the batch first.
  • Each generated token reads the whole cache, so latency rises with context.
  • The effective context length can be shorter than the advertised window.

Sources

  1. Lost in the Middle: How Language Models Use Long Contexts (arXiv 2307.03172) — arXiv (Liu et al.), 6 Jul 2023
  2. Attention Is All You Need (arXiv 1706.03762) — arXiv (Vaswani et al.), 12 Jun 2017
  3. FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness (arXiv 2205.14135) — arXiv (Dao et al.), 27 May 2022
  4. Extending Context Window of Large Language Models via Positional Interpolation (arXiv 2306.15595) — arXiv (Chen et al.), 27 Jun 2023
  5. GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints (arXiv 2305.13245) — arXiv (Ainslie et al.), 22 May 2023