What the context window is
The context window is everything a model can see at one moment: the system prompt, the whole conversation, any files or web pages you gave it, the results of its tool calls, and the reply it has written so far. It is measured in tokens. Anything outside the window does not exist for the model.
| Tokens | About | Like |
|---|---|---|
| 4,000 | 3,000 words | A long article |
| 32,000 | 24,000 words | A short book, or a few dozen code files |
| 128,000 | 96,000 words | A novel |
| 1,000,000 | 750,000 words | Several long novels, or a medium-sized codebase |
Languages that need more tokens per word (see the tokens lesson) fit less text into the same window.
From each model’s config.json. Kimi K3 and GLM-5.3 also reach about a million, and Qwen3.8 can be stretched to 1M with the method in this lesson.
Why long is hard
Three separate costs grow as the text gets longer.
Compute
- Full attention compares each token with every token before it.
- Double the length and the attention work goes up four times.
- At a million tokens, attention, not the feed-forward blocks, becomes most of the work.
Memory
- Every token leaves keys and values in the KV cache, in every layer.
- Llama 3.1 8B keeps 128 KiB per token: 16 GiB at 128K, as much as the model itself.
- That memory must also be read again for every new token.
Training
- A model only handles lengths, and positions, it saw in training.
- Long texts are rare, and expensive to train on.
- So models train short first and stretch later: DeepSeek-V4 went 4K → 16K → 64K → 1M.
Worked out from each config.json: 2 × layers × KV heads × head size × 2 bytes per token. The next sections explain the tricks behind the shorter bars.
Stretching positions
Recall RoPE from the transformer lesson: each query and key is rotated by an angle that grows with its position. A model trained on 8,000 tokens has never seen the angles for position 100,000. Simply feeding it longer texts (extrapolation) gives huge, meaningless attention scores, and the output falls apart.
- Position interpolation (Meta, 2023): instead of going beyond the trained range, squeeze the positions into it. To double the length, treat position 16,000 like 8,000, 8,000 like 4,000, and so on. Within 1,000 steps of fine-tuning, LLaMA models reached 32K.
- NTK-aware scaling (2023, first posted on Reddit’s LocalLLaMA forum by one of YaRN’s future authors): RoPE turns its pairs of numbers at many different speeds. Squeezing all of them equally blurs the fast ones, which tell nearby tokens apart. So stretch the slow pairs a lot and the fast pairs hardly at all.
- YaRN (2023): a refined version of the same idea, plus a small adjustment to attention’s temperature. It needs about 10 times fewer tokens and 2.5 times fewer training steps than earlier methods, and is now the usual way to extend open models: DeepSeek-V4 uses it, and Qwen’s model card tells you how to turn it on.
"max_position_embeddings": 1048576,
"rope_scaling": {
"type": "yarn",
"factor": 16,
"original_max_position_embeddings": 65536,
"beta_fast": 32,
"beta_slow": 1
},
"sliding_window": 128,
"index_topk": 1024
- The model was trained with positions up to 65,536 (64K), then YaRN stretched them 16 times: 65,536 × 16 = 1,048,576.
- beta_fast and beta_slow set where YaRN’s “stretch slow pairs, keep fast pairs” rule switches over.
- sliding_window and index_topk belong to the attention tricks in the next section.
Quick check
Reading less: smarter attention
Stretching positions lets a model accept a million tokens. Making it affordable needs a different idea: let each new token read only part of the past. Try the patterns real models use.
Each pattern trades a little thoroughness for a lot of speed and memory.
This interactive part needs JavaScript.
| Model | Long-context design |
|---|---|
| gpt-oss | Layers alternate: a sliding window of 128 tokens, then full attention |
| Gemma 4 31B | Five of every six layers use a window of 1,024 tokens; every sixth is full |
| DeepSeek-V3.2, GLM-5 | DeepSeek Sparse Attention: a fast indexer picks the most useful earlier tokens (2,048 in V3.2) |
| DeepSeek-V4-Pro | Compressed sparse layers (4 tokens per summary, best 1,024 read) interleaved with heavily compressed layers (128 per summary), plus a 128-token window |
| Qwen3.8 | 48 Gated DeltaNet (linear) layers and 16 full-attention layers |
| Kimi Linear | Kimi Delta Attention (linear) mixed with full MLA layers; Kimi K3 builds on it |
| MiniMax-M1 | Lightning (linear) attention mixed with full attention |
What does each trick do?
-
Position interpolation
Answer: Lets the model handle positions it never saw
-
YaRN
Answer: Lets the model handle positions it never saw
-
A sliding window
Answer: Makes each token read less
-
Keeping attention sinks with a window
Answer: Makes each token read less
-
A sparse indexer picking the top 2,048 tokens
Answer: Makes each token read less
-
Training at longer lengths in stages
Answer: Lets the model handle positions it never saw
-
Linear attention’s fixed-size memory
Answer: Makes each token read less
It fits, but did it understand?
The classic test is the needle in a haystack: hide one sentence, such as “the secret number is 7481”, somewhere in a long text, and ask for it. Most modern models pass it almost perfectly. But it is too easy: the question repeats the needle’s words, so the model only has to find a match.
- Lost in the middle (2023): models found facts best at the start and end of a long text, and worst in the middle.
- RULER (NVIDIA, 2024) added harder versions: several needles, following chains, counting. Of the models that claimed 32K tokens or more, only half still did well at 32K.
- NoLiMa (2025) wrote needles that share no words with the question, so the model must actually understand. At 32K, 11 of 13 models fell below half of their short-text score.
The test hides several similar requests in a long conversation and asks for one particular answer. Scores are from DeepSeek’s own paper. The best models now do well even at 1M, but no model is perfect.
Remember
- The context window is everything the model can see at once, measured in tokens. Top open models now read about a million.
- Long context has three costs: attention compute (grows with the square), KV cache memory (grows with every token) and training on long texts.
- Position interpolation and YaRN stretch RoPE so that a model trained on 64K can read 1M (factor 16 in DeepSeek-V4).
- Each new token can read less: sliding windows, attention sinks, sparse top-k selection, compressed summaries or linear attention’s fixed memory.
- A window that fits is not one that is used well: RULER and NoLiMa show real, usable context is often much shorter than advertised.
Words to know
- Context window
- The maximum number of tokens a model can see at once: prompt, conversation, files and its own reply.
- Extrapolation
- Using a model on lengths or positions beyond those it was trained on. Usually fails without help.
- Position interpolation
- Squeezing longer texts’ positions into the range the model was trained on.
- YaRN
- The standard method for stretching RoPE: stretch slow-turning pairs a lot and fast ones little.
- Sliding window attention
- Each token reads only a fixed number of the most recent tokens.
- Attention sink
- The first tokens, where models park spare attention. Keeping them makes sliding windows work.
- Sparse attention
- Each token reads only a selected few earlier tokens, picked by a fast scorer.
- Linear attention
- Attention that folds the past into a fixed-size memory, so the cost grows only in step with length.
- Needle in a haystack
- A test that hides one fact in a long text and asks the model to find it.
- Context rot
- The drop in recall of any single fact as the context gets longer.
Go to the source
This lesson is built on these papers and documents. Paper links open arxiv.org, a separate site.
-
Extending Context Window of Large Language Models via Positional Interpolation
Short and clear. Figure 1 shows normal use, extrapolation and interpolation side by side.
-
YaRN: Efficient Context Window Extension of Large Language Models
The method behind most 1M-token open models. Section 3 builds it up step by step from position interpolation.
-
Efficient Streaming Language Models with Attention Sinks
The attention-sink discovery. The attention maps in Figure 2 are striking.
-
Lost in the Middle: How Language Models Use Long Contexts
The U-shaped curve that every long-context prompt guide is based on.
-
RULER: What’s the Real Context Size of Your Long-Context Language Models?
How to test long context honestly, and why simple needle tests mislead.
-
DeepSeek-V3.2: Pushing the Frontier of Open Large Language Models
Section 2.1 explains DeepSeek Sparse Attention and its lightning indexer.
-
DeepSeek-V4: Towards Highly Efficient Million-Token Context Intelligence
Section 2.3 has the compressed and heavily compressed attention, with clear diagrams in Figures 3 and 4.
-
Kimi Linear: An Expressive, Efficient Attention Architecture
Moonshot’s linear-attention hybrid: 75 % less KV cache and up to 6 times faster decoding at 1M tokens.
Check yourself
6 questions. Tap the answer you think is right.
0 of 6 answered
Lesson complete
Answer every question above to complete this lesson.
Your progress is saved on this device only.
Next lessonMixture of experts: huge models that run like small ones