• English
  • Long context

    The context window is the number of tokens a model can take into account at once: the prompt, any documents, the conversation so far and its own output. It grew from 2,048 tokens in GPT-3 to 128,000 in many open models today, and some commercial models accept a million or more. Long context lets a model read a whole codebase or a book in one go. It also brings three problems: cost, accuracy, and the position encoding itself.

    Problem 1: cost

    Two things grow with the context length nn:

    • Memory. The KV cache stores keys and values for every token: it grows linearly, ∝n\propto n.
    • Compute. Every token's query is compared with every earlier key. The number of pairs grows quadratically, ∝n2\propto n^2.

    All the other matrix multiplications (projections and MLPs) cost the same per token, so they grow linearly. Prefill compute is roughly

    FLOPs≈2Pn⏟weights+2Ld n2⏟attention\text{FLOPs} \approx \underbrace{2 P n}_{\text{weights}} + \underbrace{2 L d\, n^2}_{\text{attention}}

    for PP parameters, LL layers and model width dd (the length of each token's vector). The first term is one multiply and one add per parameter per token. The second counts attention itself: about n2/2n^2/2 pairs of tokens (each token looks only at earlier ones), and in every layer each pair costs a dot product of length dd for its score and another dd multiply-adds to mix in its value, about 4d4d operations.

    For short prompts the first term wins. Past the length where the two are equal, n=P/(Ld)n = P / (L d), attention dominates and the cost curve bends upward. For Llama 3 8B that is 8×109/(32×4096)≈61,0008 \times 10^9 / (32 \times 4096) \approx 61{,}000 tokens.

    Figure 12.1 · What a longer context costs
    100 MB1 GB10 GB100 GB1 TB10 TB1k4k16k64k256k1Mmodel weights (16-bit)one 80 GB GPU
    17 GBKV cache for one sequence1.1× the weightsmodel weights (16-bit)

    Shapes follow Llama 3 8B and 70B; “no GQA” gives every query head its own keys and values.

    Both axes are logarithmic, so a straight line means a power law: memory has slope 1, attention compute slope 2. For the 8B shape, the KV cache of a single 128k-token sequence is already as large as the model's weights, and attention overtakes everything else at around 60k tokens. Compare with the model that skips grouped-query attention.

    Decode feels the length too, through memory rather than arithmetic. Every decode step reads the whole cache (chapter 10 explains why reading, not computing, sets decode speed). At 128k tokens the cache of an 8B model is as big as its weights, so each new token takes roughly twice as long as with a short prompt.

    Grouped-query attention, KV quantization and the other tricks from Caching attack the memory line. FlashAttention (Fast inference) makes the quadratic compute fast and memory-light, but still quadratic. Changing the exponent itself needs a different attention pattern; more on that below.

    Problem 2: does the model actually use it?

    A model that accepts a million tokens does not necessarily use them well. Two experiments made this visible.

    Lost in the middle (Liu et al., 2023) gave models 20 retrieved documents, only one of which held the answer, and moved that document around. Accuracy was highest when it came first or last and dropped sharply in the middle, at worst close to answering with no documents at all. Needle in a haystack hides one out-of-place sentence at a chosen depth in a long filler text and asks for it back, sweeping both depth and length.

    Figure 12.2 · Where long-context recall breaks
    40%60%80%100%answering with no documentsfirstlastposition of the document with the answer (1 of 20)

    Illustrative numbers that mimic published shapes, not a benchmark of any specific model.

    Illustrative curves that reproduce the published shapes. Older models show a U-shape and fail in the middle of long inputs; newer ones are much flatter. Switch to the needle test: each cell is one document length and needle depth.

    Newer models pass the simple needle test almost perfectly, so harder benchmarks (RULER, multi-needle tests, and multi-hop tests whose questions need several facts chained together) are used instead. They still find that the effective context, the length at which a model keeps its short-context quality, is often well below the advertised one, and that quality slowly degrades as more irrelevant text is added. People call this context rot.

    Why the U-shape? The causes are still debated; three are usually given. Training data rarely needs information from the middle of very long texts; attention patterns favor the start (the "attention sink" we saw in Caching) and the most recent tokens; and with causal attention, early tokens are processed without knowing what the question will be.

    Problem 3: positions the model has never seen

    RoPE encodes position by rotating pairs of numbers in each query and key, every pair at its own speed. Fast pairs complete many full turns within the training length; slow pairs complete less than one. A model trained on 4,096 tokens has never seen the slow pairs turned any further than their 4k angle. Feed it position 30,000 and those pairs land on angles it has never seen, and attention breaks.

    The fixes all squeeze positions back into the trained range:

    • Position interpolation (PI) divides every position by s=s = target length / training length. All angles fit, but neighbours are now ss times closer, which blurs the fast pairs.
    • NTK-aware scaling (named after a piece of theory about how networks learn, the neural tangent kernel) increases the RoPE base instead (the 10,000 that sets the rotation speeds, chapter 4), so fast pairs barely change and slow pairs stretch the most.
    • YaRN treats each pair separately: pairs that already turned many times are left alone, slow ones are interpolated, with a ramp in between, plus a small correction to attention temperature, a factor that makes attention's softmax a little sharper, much like sampling temperature. Many open long-context models use YaRN or a close relative.
    Figure 12.3 · Stretching RoPE to a longer context
    angles seen in training (4k) angles used at the target length never seen
    pair 0wavelength 6full circle seenstep ×1
    pair 16wavelength 63full circle seenstep ×1
    pair 32wavelength 628full circle seenstep ×1
    pair 40wavelength 2kfull circle seenstep ×1
    pair 44wavelength 3kfull circle seenstep ×1
    pair 48wavelength 6knever seenstep ×1
    pair 52wavelength 11knever seenstep ×1
    pair 56wavelength 19knever seenstep ×1
    pair 60wavelength 35knever seenstep ×1
    pair 63wavelength 53knever seenstep ×1

    5 of 10 sample pairs leave the trained range

    Positions past 4k rotate the slow pairs into angles training never produced. Attention scores there are unpredictable, and quality falls apart.

    Ten of a head's 64 rotating pairs, trained on 4k tokens. Inner ring: angles seen in training. Outer ring: angles needed at the target length, orange where never seen. Fast pairs (top) have seen the full circle; the slow ones are the problem. Compare the methods, and watch the step: how much the angle changes between neighbouring tokens.

    Scaling alone is not enough for top quality. In practice models are pretrained at a moderate length (4k to 8k), then continue training on long documents while the scaled context grows in stages, up to 128k or beyond. That last phase is a small fraction of the training compute but teaches the model to actually use distant tokens.

    Cheaper attention patterns

    To change the quadratic exponent, a token must stop looking at every earlier token. Sparse attention keeps a structured subset. Chapter 5 showed which scores these patterns skip; the question here is reach. If a token cannot see far back directly, can information from far back still get to it?

    Figure 12.4 · Sparse attention patterns
    O(n·w)58/136pairs scored · 43% of full attention
    attended directly reachable through earlier layers out of reach

    After these layers, token 15 can draw on 4 of 16 tokens

    Tap a row to pick the query token.

    Each token sees only the last w = 4. Linear cost; information still travels w − 1 tokens per layer, so deep stacks reach far (Mistral 7B, many hybrid models).

    Each row is a query, each column a key (16 tokens). Highlighted row: what the selected token attends to directly, and, with more layers, what it can reach indirectly through other tokens. Even a narrow sliding window reaches far when layers are stacked.
    • Sliding window attention (used by Mistral 7B, and interleaved with full-attention layers in models like Gemma) costs O(n⋅w)O(n \cdot w), work that grows with the length times the window rather than the length squared, and keeps the KV cache at ww tokens per layer.
    • Global or sink tokens add a few positions everyone can see (Longformer, BigBird, StreamingLLM).
    • Learned sparse attention lets the model pick which blocks of earlier tokens to read for each query, as in DeepSeek's sparse attention and Native Sparse Attention.
    • Some architectures drop attention's growing cache altogether and keep a fixed-size state instead: see Beyond the classic transformer.

    Very long contexts also need more than one GPU per sequence. Ring attention and context parallelism split the sequence across devices; each device holds a slice of the queries while blocks of keys and values are passed around the ring, overlapping the transfer with computation.

    Long context in practice

    • Long context or retrieval? Stuffing everything into the prompt is simple and lets the model connect distant facts, but it is slow and expensive and suffers from context rot. Retrieval (RAG) sends only the relevant pieces. Many systems combine both: retrieve generously, then let a long-context model read.
    • Put the question last, and important material near the start or end. Lost-in-the-middle effects are smaller in new models, but rarely zero.
    • Cache the long, stable part. A big document followed by many questions is the ideal case for prompt caching.
    • Agents compact their history. Long-running agents summarize or drop old tool outputs, keep notes in files, and start fresh contexts for sub-tasks, instead of letting the context fill with noise.

    Summary

    • The KV cache grows linearly with context, attention compute quadratically; beyond some tens of thousands of tokens attention dominates.
    • Accepting a long context is not the same as using it: recall dips in the middle and degrades with length.
    • RoPE needs scaling (PI, NTK-aware, YaRN) plus continued training on long documents to work past its training length.
    • Sparse patterns, sliding windows and state-based architectures attack the quadratic cost; ring attention spreads one sequence over many GPUs.

    Long context is one way a model gets expensive; sheer size is the other. The next chapter shows how to make big models cheaper to run and to adapt: mixture of experts, quantization and LoRA.