Availit World How AI really works, in your language

Lesson 17 of 4619 minFacts checked October 20, 2026

Long context: reading a whole book at once

In 2023, most open models could read about 2,000 to 4,000 tokens at once: a few pages. In 2026, the top open models from DeepSeek, Kimi and Z.ai read a million, the length of several long novels or a big codebase. This lesson shows why that was hard, the tricks that made it possible, and why “it fits” is not the same as “it understood”.

In this lesson you will

  • Explain what the context window is, and what is in it
  • Name the three costs of long context: compute, memory and training
  • Explain how RoPE is stretched to longer texts (position interpolation, YaRN)
  • Compare the attention patterns that make a million tokens affordable
  • Explain why a model’s real, usable context is often shorter than its advertised one
On this page
  1. What the context window is
  2. Why long is hard
  3. Stretching positions
  4. Reading less: smarter attention
  5. It fits, but did it understand?
  6. Remember
  7. Check yourself

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.

How much is how much (English, roughly 0.75 words per token)
TokensAboutLike
4,0003,000 wordsA long article
32,00024,000 wordsA short book, or a few dozen code files
128,00096,000 wordsA novel
1,000,000750,000 wordsSeveral 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.

Context windows of open models
  • Llama 2 (2023) 4,096 tokens
  • Llama 3.1 (2024) 131,072 tokens
  • gpt-oss (2025) 131,072 tokens
  • Qwen3.8-27B (2026) 262,144 tokens
  • DeepSeek-V4 (2026) 1,048,576 tokens

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.
KV cache for one 128K-token conversation (BF16)
  • Llama 3.1 8B, if it had no GQA 64 GiB
  • Llama 3.1 8B (8 KV heads) 16 GiB
  • Qwen3.8-27B (16 of 64 layers keep a cache) 8 GiB
  • gpt-oss-20b (half the layers keep 128 tokens) 3 GiB

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.

  1. 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.
  2. 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.
  3. 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.
DeepSeek-V4-Pro: config.json (selected lines)json
"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

A model was trained up to 32,768 positions. Its config says YaRN with factor 4. How long can its texts be?

Show the answer

Answer: 131,072 tokens The factor multiplies the original length: 32,768 × 4 = 131,072, which is 128K.

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.

Try itWhich tokens does the newest token read?

Each pattern trades a little thoroughness for a lot of speed and memory.

This interactive part needs JavaScript.

Who uses what, in 2026
ModelLong-context design
gpt-ossLayers alternate: a sliding window of 128 tokens, then full attention
Gemma 4 31BFive of every six layers use a window of 1,024 tokens; every sixth is full
DeepSeek-V3.2, GLM-5DeepSeek Sparse Attention: a fast indexer picks the most useful earlier tokens (2,048 in V3.2)
DeepSeek-V4-ProCompressed sparse layers (4 tokens per summary, best 1,024 read) interleaved with heavily compressed layers (128 per summary), plus a 128-token window
Qwen3.848 Gated DeltaNet (linear) layers and 16 full-attention layers
Kimi LinearKimi Delta Attention (linear) mixed with full MLA layers; Kimi K3 builds on it
MiniMax-M1Lightning (linear) attention mixed with full attention

What does each trick do?

0 of 7 sorted
  • 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.
A one-million-token memory test (Long MRCR, scores reported by DeepSeek, April 2026)
  • Claude 92.9 score
  • DeepSeek-V4-Pro 83.5 score
  • Gemini 76.3 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.

Check yourself

6 questions. Tap the answer you think is right.

0 of 6 answered

What is in a model’s context window?

Show the answer

Answer: Everything it can see right now: system prompt, conversation, files, tool results and its own reply so far The window is the model’s working view. What it learned in training lives in the weights instead.

You double the length of a text. Roughly how does full attention’s work change?

Show the answer

Answer: It goes up about four times Every token is compared with every earlier token, so the work grows with the square of the length: 2² = 4.

What does position interpolation do?

Show the answer

Answer: Squeezes long texts’ positions into the range the model was trained on Instead of using angles the model never saw, it maps the new, longer range back into the trained one.

Why do sliding-window models keep the first few tokens?

Show the answer

Answer: Models park spare attention on them, and dropping them breaks the model These attention sinks act as a parking spot for softmax’s extra weight. Keeping 4 of them made window attention stable.

A model claims a 128K-token window. What do RULER and NoLiMa suggest?

Show the answer

Answer: Its usable context may be much shorter, especially for harder questions Many models pass simple needle tests but fall well behind on harder long-context tasks, often from 32K on.

How does DeepSeek-V4 make a million tokens affordable?

Show the answer

Answer: It compresses groups of tokens into summaries and reads only the most useful ones, plus a short recent window Its CSA layers merge every 4 tokens and read the best 1,024 summaries; its HCA layers merge 128 tokens into each summary; a 128-token window keeps local detail.

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
Previous lesson All lessons

Choose your language