Module 3 · Long-context ingestion
200k to 1M+ token windows: possibilities and limits
6 min read
Current Claude models (Opus 5.5, Sonnet 5.5, Haiku 5.5) accept one million tokens of context, several thousand pages. Small open models often accept 32,000 to 128,000. Such a large window changes how you work: you can provide whole documents rather than excerpts. But "the model accepts a million tokens" does not mean "the model uses each of them equally well". This lesson measures what happens to accuracy, latency and cost as the context grows.
What a token represents
Everything is counted in tokens, and the ratio of characters to tokens depends on the text and the tokenizer:
| Content (Qwen2.5 tokenizer) | Characters per token |
|---|---|
| Quill project: Python code, Markdown and YAML in French | 3.3 |
| "12 450,00 €" (lesson 1.1) | 1.1 |
Numbers, punctuation and languages other than English cost more tokens. And each model family has its own tokenizer: Sonnet 5 and Haiku 5.5, for instance, count about 30% more tokens than their predecessors for the same text. For a reliable budget, count with the target model's tokenizer — for Claude, the token-counting endpoint:
def count_tokens(text: str, model: str = config.CLAUDE_MODEL) -> int:
"""Le compte exact pour le modèle visé : chaque famille de modèles a son tokenizer."""
return client().messages.count_tokens(model=model, messages=[{"role": "user", "content": text}]).input_tokens
| Order of magnitude | Tokens (approx.) |
|---|---|
| An email | 300 – 1,000 |
| A 30-page contract | 15,000 – 25,000 |
| A full annual report | 150,000 – 400,000 |
| An average code repository, without dependencies | 200,000 to several million |
The needle-in-a-haystack test
To measure what the model retrieves from a long context, we hide a single fact (the needle) in a long document (the haystack), then ask the question:
@dataclass
class Needle:
city: str
code: str
@property
def sentence(self) -> str:
return f"Note interne : le code d'accès de l'entrepôt de {self.city} est {self.code}."
@property
def question(self) -> str:
return f"Quel est le code d'accès de l'entrepôt de {self.city} ? Réponds uniquement par le code."
The haystack is not filler text: it is made of purchase orders, invoices and contract clauses, in the same register as the needle. The test is thus closer to a real document than with unrelated text, where the needle stands out.
def build_document(needle: Needle, n_tokens: int, position: float, tokenizer, seed: int = 0) -> str:
"""Un document d'environ `n_tokens` jetons, avec l'aiguille à `position` (0 = début, 1 = fin)."""
paragraphs, out, total = haystack_paragraphs(seed), [], 0
for p in paragraphs:
total += len(tokenizer(p, add_special_tokens=False)["input_ids"]) + 2
if total > n_tokens:
break
out.append(p)
out.insert(round(position * len(out)), needle.sentence)
return "\n\n".join(out)
We vary the document length (1,000 to 8,000 tokens) and the needle position (start, quarter, middle, three quarters, end), with 8 needles per cell.
| Length | Start | 25% | Middle | 75% | End |
|---|---|---|---|---|---|
| 1,000 tokens | 1.00 | 1.00 | 1.00 | 1.00 | 1.00 |
| 2,000 tokens | 1.00 | 1.00 | 1.00 | 1.00 | 1.00 |
| 4,000 tokens | 1.00 | 1.00 | 1.00 | 1.00 | 1.00 |
| 8,000 tokens | 1.00 | 1.00 | 1.00 | 1.00 | 1.00 |
Every needle is retrieved, everywhere, even by a 1.5-billion-parameter model. This is not a weakness of the experiment, it is its lesson: the simple needle test is saturated for recent models, trained precisely to pass it. It proves the model can retrieve a distinctive sentence ("the Lyon warehouse access code is…"); it says nothing about its ability to use a long document.
Two practical remarks:
- lengths stop at 8,000 tokens: beyond that, on a 6 GB card, the KV cache and attention spill into system memory, and a single question at 16,000 tokens takes several minutes. Memory and latency often limit before accuracy does;
- for a test that discriminates, the task must be realistic: a needle phrased like the haystack (one amount among hundreds of amounts), several facts to combine, a question that requires reasoning about what is retrieved. That is the purpose of the practice exercises.
The limits of a large window
| Limit | What happens | Remedy |
|---|---|---|
| Accuracy | Retrieving an isolated fact is easy for large models; connecting several scattered facts is much less so | Quote first, answer next (lesson 3.3); split complex questions |
| Latency | Reading the prompt (the "prefill") takes time that grows with its length | Prompt caching (lesson 3.4) |
| Cost | Each request pays for the whole context in input tokens | Prompt caching; send only what is useful |
| Noise | Off-topic but related passages distract attention | Filter before sending (lesson 3.3) |
| Output | The input window is large, the output stays limited (128,000 tokens for Claude) | Produce long deliverables in pieces |
The needle test is the minimum: a model that fails to retrieve an isolated fact will fail harder tasks. But a model that passes it perfectly can still fail when the question requires cross-referencing several passages ("are the clauses of this contract consistent with each other?"). Test on your documents and your questions.
Key takeaways
- Current Claude models accept one million tokens; small open models, 32,000 to 128,000.
- Count with the target model's tokenizer: numbers and French cost more tokens, and tokenizers differ.
- The needle test measures the ability to retrieve an isolated fact: it is a minimum, not a guarantee — Qwen2.5-1.5B passes it at 100% up to 8,000 tokens.
- Accuracy, latency, cost and noise degrade with length: send only what is useful, and cache.
Practice
- Run
examples/04_long_context.pywithQwen/Qwen2.5-0.5B-Instruct: how does the grid change with a model three times smaller? - Replace the needle with two facts to combine ("the Lyon code, followed by the Nantes code") and measure the drop.
- Count the tokens of one of your documents with the Qwen tokenizer and with
claude.count_tokens: how large is the gap?