ModelRefs / Needle in a Haystack — AI Glossary
Needle in a Haystack — AI Glossary
A long-context benchmark inserting a specific fact ('needle') at varying positions in a long document and measuring retrieval accuracy. Also called NIAH.
Overview
NIAH tests whether a model can retrieve a single injected fact from a distracting document corpus at any context position. Frontier models (Gemini 1.5 Pro, Claude 3 Opus) approach near-perfect NIAH scores at 128K context. Failures reveal positional biases in attention patterns. Extended to multi-needle and multi-hop variants.
Reference details
| Topic | prompting |
|---|---|
| Also known as | NIAH |
| Last reviewed | 2026-06-24 |
Related terms
How it works
- A distinctive fact — the needle — is written into a long filler document at a controlled position, and the model is asked a question only that fact answers.
- Two variables are swept: total context length and the needle's relative depth in it. The result is a grid, usually rendered as a heatmap, with one cell per length-and-depth pair.
- The filler matters as much as the needle. Repetitive or topically unrelated filler makes the needle stand out lexically, so the test measures salience rather than retrieval; filler drawn from the same domain is a much harder and more honest test.
- Scoring is retrieval of the fact, typically by string or judged match. It is near-binary per trial, which is what makes the error bars large and frequently unreported.
- Multi-needle variants insert several facts and require all of them, which turns a retrieval test into a conjunction and drops scores sharply for the same reason per-prompt constraint adherence drops below per-constraint.
- Multi-hop variants require combining two inserted facts, which separates retrieving from the context from reasoning over what was retrieved — the distinction a single-needle test cannot make.
Example: Why a perfect heatmap is consistent with a 2% failure rate
A grid of 10 context lengths by 10 depths is 100 cells, and the usual practice is one trial per cell — so a green heatmap is 100 successes out of 100 trials. Suppose the model's true per-trial retrieval rate is 98%. The probability that all 100 trials succeed is 0.98^100 = 0.133, so roughly one run in eight of a genuinely 2%-fallible model produces a flawless grid. The grid cannot distinguish 98% from 100%, and at production scale that gap is the whole question: at a million long-context requests, 2% is 20,000 retrieval failures. Getting resolution requires trials per cell, and the arithmetic is unforgiving — the standard error of a proportion is sqrt(p(1-p)/n), so even 10 trials per cell at p = 0.98 gives sqrt(0.98 x 0.02 / 10) = 0.044, about ±4.4 points per cell, and 1,000 trials in total. This is why a published heatmap is better read as a map of where failures cluster than as a measurement of how often they happen.
Commonly confused with
Needle-in-a-haystack measures retrieval from context, not long-context reasoning, and conflating the two is the standard overclaim. Finding one verbatim fact in 128,000 tokens is a lookup; summarising the document, tracking an entity across it, or reconciling two statements made 100,000 tokens apart are different capabilities that a near-perfect needle score does not predict. The second confusion is with RAG evaluation: the needle is placed in the context by the harness, so retrieval from a corpus — the part that actually fails in production — is not being tested at all.
When to use it
Reach for it when:
- Sanity-checking that a model's advertised context length is usable at all, where a failure at depth is disqualifying.
- Locating positional weakness: a heatmap shows whether failures cluster in the middle, which is actionable for prompt layout.
- Comparing context-extension techniques, where the question is specifically whether attention still reaches distant positions.
- As a regression test on a long-context pipeline, where a previously green region going red is a real signal.
Reach for something else when:
- As evidence of long-context comprehension. It tests lookup, and comprehension benchmarks exist separately.
- With one trial per cell, presented as a measurement. Report the trials per cell or report it as a map, not a rate.
- With filler unrelated to the needle, which makes the target lexically obvious and inflates the result.
- As a substitute for evaluating retrieval. The needle is pre-placed; the hard part of RAG is finding it in the corpus.
Continue your research
Use these connected ModelRefs sections to compare alternatives, inspect implementation paths, and review the evidence and governance boundaries relevant to Needle in a Haystack — AI Glossary.
Frequently asked questions
What is Needle in a Haystack?
A long-context benchmark inserting a specific fact ('needle') at varying positions in a long document and measuring retrieval accuracy.
Is Needle in a Haystack the same as NIAH?
Yes — NIAH are common aliases for Needle in a Haystack.
What concepts are related to Needle in a Haystack?
Closely related concepts include lost in the middle, long context, retrieval pipeline.