What This Page Does, and What It Does Not Redo
Extending a model's context window always happens in two stages: first you change the way the model locates positions in the text, then you continue its pre-training on genuinely long documents. That second stage is a special case of continued pre-training, or CPT, which has a lesson of its own: Continual pre-training and domain adaptation. That lesson covers CPT as a tool for domain specialization (medicine, law, finance): data mixing, settings, preventing catastrophic forgetting.
Here we do not redo that work. We recap CPT in a few lines, then devote the page to its most technical application: long-context extension.
Recap: CPT in Three Sentences
A generalist large language model (LLM) has been trained on enormous quantities of web text. CPT means taking that already-trained model and continuing exactly the same training, with the same objective (guess the next word, known as next-token prediction) but on a different corpus.
Two practical consequences. First, what moves is the model's learned weights: CPT is a real training run, with gradients and updates, not a file operation like model merging. Second, since the weights are already good, you proceed far more cautiously than during the initial training: a reduced learning rate, and a deliberate mixture of general data with the specialized data so the model does not forget what it knew. The exact settings are in the dedicated lesson and in Learning rate scheduling.
The analogy: rather than building a hospital from scratch, you renovate an existing building. The foundations (language comprehension) and the structure (grammar, syntax, reasoning) are already solid. You add specialized equipment on top.

CPT or Fine-Tuning? The Distinction in One Line
Supervised fine-tuning (SFT) shows the model question / answer pairs: it learns how to answer. CPT gives it whole documents to absorb: it learns what to know. The two are often chained, CPT for knowledge, then SFT for behaviour. See Instruction tuning for the second stage.
Long Context Extension: going beyond the training window
The problem: a fixed window
A model is trained with a fixed context length, the number of tokens it can read in one go. A token is a fragment of a word; in English, reckon roughly one token per three quarters of a word. The first LLaMA models worked on 4,096 tokens, about ten pages. Beyond that limit, performance does not degrade gently: it collapses. The model produces incoherent answers, forgets the beginning of the text, or generates gibberish.
Why such an abrupt fall? Because of how the model locates positions. The most widespread mechanism is called RoPE (Rotary Position Embedding), covered in detail in RoPE in detail. Its principle: to signal that a token sits at position 300 rather than position 12, you rotate its representation by an angle proportional to its position, like the hands of a clock, each turning at its own speed.
A decisive distinction for what follows. RoPE does not touch the model's learned weights. It is an operation applied to the activations, i.e. the vectors recomputed for every text: concretely, the query (Q) and key (K) vectors are rotated just before attention is computed.
One enormous consequence: since positions are handled by a fixed formula rather than by learned parameters, you can change that formula after training, without relearning anything. That is the whole trick behind the three techniques below.
So a model trained on 8,000 tokens has never seen an angle corresponding to position 8,001. It's like asking the time on a dial whose hands have started spinning at a speed never seen before: they are moving, but their position no longer means anything.
Yet users want to process entire documents, codebases, long conversations. How do you get there?
Technique 1: Position Interpolation (PI)
The most direct idea: compress all positions to fit within the range the model knows. The method is called PI, for Position Interpolation.
Reading the symbols: is the context length the model was trained on, the length you want to reach, and the ratio of the two is a compression factor below 1. In one sentence: shrink the scale of positions so that the farthest position in the new text lands exactly on the farthest position the model knows.
Concretely, to go from 4,096 to 16,384: the ratio is , so you divide every position by 4. Position 16,000 becomes 4,000, and the model stays in its comfort zone.
Extending Context Window of Large Language Models via Positional Interpolation (Chen et al., 2023)What the abstract claims, and what is therefore checkable: extension up to 32,768 tokens, on LLaMA models from 7 to 65 billion parameters, with fewer than 1,000 steps of retraining. The authors also justify interpolating rather than extrapolating: beyond the trained length, attention scores can take catastrophically high values that "completely ruin" the self-attention mechanism.
Advantage: extreme simplicity, a multiplicative factor on positions, nothing else.
Drawback: you lose local resolution. Compressing positions by a factor of 4 turns the gap between two neighbouring tokens from 1 into 0.25. The model therefore distinguishes two consecutive words less well, which degrades fine-grained understanding. It's like taking a ruler graduated in millimetres and squeezing it to cover four times the distance: the large measurements still read, the small ones no longer do.

Technique 2: NTK-aware Scaling
The flaw in PI that this technique fixes, in one sentence: PI compresses every clock hand by the same factor, including the ones that told two neighbouring words apart; NTK-aware scaling compresses only the slow hands and leaves the fast ones alone.
The key observation: in RoPE, not all rotation speeds play the same role.
- High frequencies (the fast hands) encode local relationships: which token is just before me, just after me.
- Low frequencies (the slow hands) encode overall position: where am I in the document.
PI compresses both categories identically, which is a waste. So-called NTK-aware scaling (the acronym comes from Neural Tangent Kernel, a theoretical tool that inspired the method's authors; the name stuck even though the final idea can be explained without it) modifies RoPE's base rather than the positions. The base is the number that sets the spacing of the rotation speeds: the larger it is, the slower the slow hands.
Reading the symbols: is the original value (often 10,000), (alpha) the extension factor you want (say 4 to go from 4,096 to 16,384 tokens) and the number of dimensions in each token's representation. The exponent is slightly above 1 and tends to 1 as grows: it is a technical correction ensuring the slowest hand is stretched by exactly the intended factor . In one sentence: you slow every hand down, but so unevenly that the fastest ones barely change.
The effect: low frequencies are stretched to cover the widened window, while high frequencies stay nearly intact, so the ability to tell two neighbouring words apart is preserved.
Technique 3: YaRN
The flaw in NTK-aware scaling that YaRN fixes, in one sentence: NTK-aware scaling treats every frequency by one continuous formula, without asking whether that frequency needed modifying at all, and it says nothing about the fact that a longer text mechanically dilutes attention; YaRN treats frequencies in groups and corrects the dilution.
YaRN is a playful acronym from its authors: Yet another RoPE extensioN method.
Its ingredients:
- Interpolation by parts (NTK-by-parts): frequencies are split into three groups. The highest are not touched at all, the middle ones are partly interpolated, the lowest are fully interpolated. A stepped version of the previous technique, instead of one continuous formula.
- Attention temperature correction: the longer the text, the more attention spreads over many positions, so the less each position receives. YaRN compensates by dividing the attention scores by a factor (called the temperature) that grows with the extension factor. The authors determine that factor empirically rather than from a theoretical formula; do not carry away a numeric value without checking the one recommended for the model family at hand.
What the abstract claims, and what is therefore checkable: YaRN matches earlier methods with 10x fewer tokens and 2.5x fewer training steps. The authors also report that YaRN manages to work beyond even the length of the texts used for retraining.
- PI: compresses every position by the same factor. Simplest, but local sharpness is lost.
- NTK-aware: modifies RoPE's base so mainly the slow hands are slowed. Preserves local sharpness.
- YaRN: frequencies handled in groups, plus a correction for attention dilution. It is the most retraining-efficient method according to its abstract, and the one most often seen in recent open models.
The Essential Step: Retraining on Long Text
Modifying positions via PI, NTK-aware or YaRN is not enough. You have made distant positions legible, but the model has never learned to use them: nothing has shown it that it can reach for information sitting far back.
So after modifying RoPE you need a CPT (continued pre-training, exactly the mechanism recapped at the top of this page) on genuinely long documents: books, thirty-page scientific papers, entire codebases, conversation transcripts. This is where, and only where, learned weights are modified.
That phase is short by pre-training standards: the PI abstract speaks of fewer than a thousand steps to reach 32,768 tokens.
Progressive Extension: Climbing in Stages
A direct thirty-fold extension is possible but risky: the jump is large and the model may converge poorly. In practice you often proceed in stages, for example:
At each stage you apply the corresponding RoPE modification, then retrain on texts of that length. The model adapts gradually.
Standard attention compares every token with every other one. For tokens that is comparisons: this is called quadratic complexity, written , the notation says how the cost grows as grows, ignoring constants.
The arithmetic can be done in your head, and it is brutal. Going from 4,096 to 131,072 tokens multiplies the length by 32, hence the number of comparisons by . A thousand times the work and the memory for the same sentence of output.
That is why context extension always goes hand in hand with techniques that cut the bill: Flash Attention, which computes attention block by block without ever storing the big comparison matrix, or caching the keys and values already computed (KV Cache).
Where Practice Stands
Rather than a model-by-model correspondence table, which ages within months and gets miscopied, remember the general shape of these announcements, plus two claims verifiable in the abstracts cited on this page:
- PI: extension up to 32,768 tokens on LLaMA models from 7 to 65 billion parameters, with fewer than a thousand retraining steps.
- YaRN: the same result as earlier methods for ten times fewer retraining tokens.
Where are we today? The DeepSeek-V4 abstract announces a one-million-token window, and more tellingly than the length itself, a cost figure: at that length the model needs only 27 % of the per-token inference operations and 10 % of the key-value cache of its predecessor. That is the real research front, not advertising a big window, but making it affordable.
DeepSeek-V4: Towards Highly Efficient Million-Token Context IntelligenceWhat remains is to check that a model can actually use its window. The test that became standard is called needle in a haystack: hide a specific sentence at a given depth inside a very long document, then ask the model to retrieve it.

1. When you apply PI or NTK-aware scaling to extend the context window, what exactly do you modify in the model?
2. After modifying RoPE (via PI, NTK-aware scaling or YaRN), why does the model still need retraining on long texts?
3. Which flaw of Position Interpolation (PI) does NTK-aware scaling fix?
4. You extend the window from 4,096 to 131,072 tokens, 32 times more. The cost of standard attention gets multiplied by roughly how much, and why?
5. A model advertises a context window of 128,000 tokens. What does that number guarantee?