Pidoku

Long-Context Research

Module 24 Lesson 24.g 50 min Difficulty 4/5

Prerequisites 12.h, 14.f, 14.i, 24.b, 24.c


Before you start: words you need#

WordPlain meaning
Context windowThe maximum number of tokens the model accepts in one request (prompt plus output).
Advertised vs effective contextAdvertised: what the model card says it accepts. Effective: how long the input can be before the model’s answers clearly get worse on a given task.
RoPE and extension (YaRN, LongRoPE)RoPE encodes token positions with rotations; extension methods stretch it so a model trained on short texts can read longer ones after brief fine-tuning.
Needle in a haystackA test that hides one fact in a long text and asks for it. Easy; most models pass.
Multi-hop / aggregation tasksTests where the answer needs several facts combined or counted across the text. Much harder.
Context parallelism (CP, DCP)Splitting one request’s sequence across GPUs, for prefill (CP) or for the decode-time KV cache (DCP) (14.f).
RAG (retrieval-augmented generation)Fetching only the relevant passages and sending those, instead of the whole document.

1. What is it?#

Long-context work has three sub-problems:

  1. Can the model do it? Architecture and training: positions, attention over many tokens, training data with long-range dependencies.
  2. Can we serve it? KV memory, KV bandwidth, prefill compute.
  3. Does it actually work? Evaluation: effective context versus advertised context.
TopicStatus, November 2026
RoPE plus extension (YaRN, LongRoPE)ESTABLISHED
1M-token windows in open frontier models (DeepSeek-V4, MiniMax-M3, Kimi K3, Nemotron 3)ESTABLISHED as an advertised feature
Sparse, compressed and hybrid attention to make 1M affordable (24.b)ESTABLISHED in models; serving support maturing
Decode context parallelism, KV offload for 1M servingESTABLISHED in engines since 2026
Effective context matching advertised context on hard tasksNot achieved; the gap is still the practical limit
Routing between retrieval and full context per queryEMERGING

Facts checked on 12 November 2026.


2. Why does it matter?#

Agents and coding tools routinely send 100k+ tokens (1.e). Long context costs more per token in memory, in decode bandwidth and, without sparse attention, in quadratic prefill (12.h). Paying for a 1M window the model cannot really use is one of the easiest ways to waste money in 2026.


3. Simple analogy#

A person who can hold a 1,000-page binder on their desk has not necessarily read it. Problem 1 is whether they can read that much at all; problem 2 is whether the desk is big enough; problem 3 is whether, asked a question that needs pages 40, 410 and 870 together, they get it right.


4. Tiny example: 50 questions about one 100k-token report#

PlanTokens prefilledKV read while answering
Full document every time, no prefix cache5.0 M~134 TiB
Full document, prefix cached0.11 M~134 TiB
Retrieval of 4k tokens per question0.21 M~5.6 TiB

Prefix caching wins on prefill, even against retrieval. But every answer token still reads the whole 100k-token cache, so decode is about 24x more memory traffic than retrieval. Section 6 computes it.


5. Technical explanation#

Problem 1: can the model do it?#

Settled: RoPE plus extension works for 4–32x the training length with modest fine-tuning, YaRN generally beats plain interpolation, and short-context quality can drop if extension is careless.

What changed by 2026: frontier open models are now designed for long context rather than stretched after training. DeepSeek-V4 stores compressed tokens in most layers and reads only the top entries; MiniMax-M3 reads selected 128-token blocks; Kimi K3 and Qwen3.5 use hybrid linear attention (24.b). The cost per token at 1M is now an architecture decision.

Still open: attention dilution, the idea that with more tokens competing for attention, the model’s focus on the relevant one weakens regardless of position encoding. Gated attention (24.b) and sparse attention plausibly help; there is no settled answer.

Problem 2: can we serve it?#

Mostly solved up to 128k by FlashAttention, paged KV, chunked prefill, GQA or MLA, FP8 KV and prefix caching. For 1M, 2026 added:

  • Decode context parallelism (DCP): shard each request’s KV across GPUs by token position. vLLM’s August 2026 results on 8× B200 with Kimi K2.6 in NVFP4: plain tensor parallelism filled the KV cache at concurrency 64 and stayed near 1,863 tokens/s per GPU; with DCP, 6,091 tokens/s per GPU at concurrency 512.
  • Prefill context parallelism to split one huge prompt’s prefill across GPUs (14.f).
  • Sparse-aware offload: keep the indexer’s selection on GPU, the rest in CPU memory (Hybrid HiSparse ran GLM-5.3 at 1M on 8× H200).
  • Tiered caches for long documents queried repeatedly (Strata, OSDI 2026; 24.c).

1M-token serving went from “a cluster per request” (2024) to “possible on one 8-GPU node for sparse-attention models” (2026). It is still expensive.

Problem 3: does it work?#

BenchmarkWhat it shows
Needle in a haystack (2023)Minimum bar; saturated
Lost in the Middle (2023)Facts in the middle of a long input are used worst
RULER (2024)Retrieval, multi-hop tracing, aggregation: most models’ effective context far below advertised
LongBench v2 (2024)Realistic long tasks needing reasoning
NoLiMa (2025)When the question and the needle share no words, accuracy drops sharply with length
Context Rot (Chroma, 2025)Performance falls with input length even on simple tasks
OpenAI MRCRMulti-round coreference over long chats; used in 2026 KV-quantization studies

The finding that has held for three years: advertised context ≫ effective context for anything beyond literal lookup. There is still no broad independent evaluation showing a 2026 open model using 1M tokens reliably on multi-hop tasks. Model makers’ own long-context scores are a start, not proof.

The alternative: retrieval#

Most “long context” tasks are retrieval-shaped: the answer is in a small part of the input.

  • Long context wins for synthesis across a whole document, when retrieval quality is poor, and when the same document is asked about many times (prefix caching makes later prefills cheap).
  • Retrieval wins when the answer is localised, when documents change per query, and when decode bandwidth or cost matters.
  • Open question (EMERGING): predicting per query which regime it is in, and routing.

6. Under the hood: prefill and decode cost of three plans#

Go
// longctx.go — 50 questions about one 100k-token document, three ways.
package main

import "fmt"

func main() {
	const (
		queries   = 50
		docTokens = 100_000
		question  = 200   // tokens per question
		answer    = 300   // tokens generated per answer
		ragTokens = 4_000 // retrieved chunks per question
		kvKiB     = 96.0  // KV bytes per token (GQA-8, FP8) from 24.c
	)
	type plan struct {
		name             string
		prefilled, ctxAt int // tokens computed in total; context length during decode
	}
	plans := []plan{
		{"full document, no prefix cache", queries * (docTokens + question), docTokens + question},
		{"full document, prefix cached", docTokens + queries*question, docTokens + question},
		{"retrieval (4k tokens per question)", queries * (ragTokens + question), ragTokens + question},
	}
	fmt.Printf("%-36s %16s %26s\n", "plan", "tokens prefilled", "KV read while answering")
	for _, p := range plans {
		// every generated token reads the whole context's KV once
		readGiB := float64(queries*answer*p.ctxAt) * kvKiB / (1 << 20)
		fmt.Printf("%-36s %16d %22.0f GiB\n", p.name, p.prefilled, readGiB)
	}
}

Output:

plan                                 tokens prefilled    KV read while answering
full document, no prefix cache                5010000                 137604 GiB
full document, prefix cached                   110000                 137604 GiB
retrieval (4k tokens per question)             210000                   5768 GiB

How to read it:

  • Prefill counts tokens only. Without sparse attention, each long-prompt token also costs more attention compute, so the first plan is worse than 5M tokens suggests.
  • Decode reads the whole cache every token. Sparse attention would cut “KV read” for the full-context plans (it reads only the selected entries), which is exactly why 2026 models adopted it.
  • Prefix caching plus sparse attention makes long context competitive with retrieval for repeated questions over one document. For one-off questions over many documents, retrieval still wins.

7. Performance implications#

  • At 128k+ tokens, KV reads dominate decode; GQA, MLA, FP8 KV and sparse reads are the levers.
  • Prefill at 1M without sparse attention is very slow; with it, prefill becomes mostly linear.
  • Long requests occupy KV for a long time, cutting concurrency for everyone on the replica.
  • DCP and offload trade network and PCIe traffic for capacity; measure both.

8. Production implications#

  • Measure effective context for your task (retrieval, multi-hop, aggregation) before building a product on the advertised window.
  • Cap context by tier and price long context separately (12.h).
  • Separate long-context traffic onto its own replicas (20.f), so it does not starve short requests.
  • Use prefix caching for repeated documents; consider retrieval for one-off queries.
  • Validate KV quantization and sparse attention specifically at your longest lengths (24.c).

9. Common mistakes#

  1. Treating a passed needle-in-a-haystack test as proof of long-context ability.
  2. Building on 1M context without measuring multi-hop accuracy at 128k, 256k and 1M.
  3. Ignoring decode cost: a cached prefix still makes every output token read the whole cache.
  4. Proposing a new position-encoding extension when the limit is elsewhere.
  5. Mixing long and short requests on one replica without limits.

10. Hands-on exercise#

  1. Run longctx.go. Make the document 500k tokens and ask 5 questions instead of 50. Which plan is cheapest now?
  2. Run a RULER-style test (retrieval, multi-hop, aggregation) at 8k, 32k, 128k and the maximum you can afford on a model you serve. Plot accuracy per task type against length.
  3. Repeat with a NoLiMa-style question that shares no words with the needle. How much earlier does accuracy fall?
  4. Build a retrieval pipeline and a full-context pipeline for one real document set. Compare cost and answer quality per query.
  5. If your engine supports DCP, measure maximum concurrency at 128k with and without it.

11. Interview questions#

  1. What are the three sub-problems of long context, and which is least solved?
  2. What is effective context, and how do you measure it for a product?
  3. How did 2026 models make 1M-token windows affordable to serve?
  4. What does decode context parallelism shard, and what does it cost?
  5. When does retrieval beat long context, even with prefix caching?
  6. What would you tell a product team that wants to build on 1M context?

12. Further reading#

Checked on 12 November 2026.

↑↓ navigate↵ openesc close

100% drag to pan · scroll or pinch to zoom · double-click to zoom in