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.
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.
Python — sliding window attention#
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.
- 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.
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
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- 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