10 — Architecture

Context Windows#

Long Context · RoPE ScalingMathematical 1 min read
◆ The PatternHow much text can a model see at once — and how to push the limits

The context window is the maximum number of tokens a model can process in one forward pass. GPT-3 (2020) had 2K tokens; by 2024 GPT-4 Turbo offered 128K and Gemini 1.5 1M or more. Expanding context is critical for document analysis, code understanding, and long conversations.

Attention cost: O(n²) time, O(n) KV-cache memory
Quadratic compute + linear memory = context length is the fundamental bottleneck.
RoPE scaling: θ' = θ · α where α = target_len / train_len
NTK-aware interpolation stretches RoPE frequencies to extrapolate beyond training length.

Approaches to longer context: (1) RoPE scaling (NTK, YaRN) — cheapest, (2) Sliding window attention (Mistral) — each layer sees a local window, (3) Sparse attention patterns — attend to subset, (4) Ring attention — distribute across GPUs, (5) Simply train on more context.

Doubling context length quadruples attention compute but only doubles KV-cache memory. Long-context models primarily fight the compute cost, not the memory cost.
Interactive — attention patterns at different context lengths

Python — sliding window attention#

Pattern bridge: The finite span of tokens the model can see at once. In statistics, sample size is the context window of inference — more data, better estimates.
Worked example

Going from a 4k to a 128k window means 32× as many tokens: the KV-cache grows 32×, and the attention scores 1,024×. Extending a 4k-trained model with RoPE interpolation scales positions by the same factor of 32. A long window is expensive, and the cost grows faster than the length.

Where it misleads
  • Fitting text in the window is not the same as using it: accuracy drops for information in the middle of long contexts (Liu et al. 2024).
  • Advertised window sizes are often tested with simple “needle” retrieval; reasoning across many parts of a long document is much harder.
  • Long prompts raise cost and latency on every call; retrieval of a few relevant passages is often better.
In code
short, long = 4_096, 131_072
tokens = long / short
attention_work = tokens ** 2                   # score matrix grows with n^2
kv_memory = tokens                             # the cache grows with n
rope_scale = long / short                      # position interpolation factor
Python, in your browser — edit the code and run it again

Assumes import numpy as np. Configurations named after real models use their published shapes; other numbers are made up or simulated, as the comments say.

The same idea with a library
def sliding_window_mask(seq_len, window_size):
    """Causal + sliding window: attend to last W tokens only"""
    causal = torch.triu(torch.ones(seq_len, seq_len), diagonal=1)
    window = torch.tril(torch.ones(seq_len, seq_len),
                        diagonal=-window_size)
    mask = causal + window
    return mask.masked_fill(mask >= 1, float('-inf'))

# Mistral uses window_size=4096: each token attends to
# the previous 4096 tokens only, regardless of context length
Sources
  • N. F. Liu et al., “Lost in the Middle: How Language Models Use Long Contexts”, Transactions of the ACL 12, 2024
  • B. Peng, J. Quesnelle, H. Fan & E. Shippole, “YaRN: Efficient Context Window Extension of Large Language Models”, ICLR, 2024
  • A. Q. Jiang et al., “Mistral 7B”, arXiv:2310.06825, 2023 — sliding-window attention

Last reviewed October 2026 · How this site checks its content · Report a mistake

Linked from 2
← Previous
KV-Cache
Open in the full reader, with the topic sidebar