What is the needle-in-a-haystack test?
The needle-in-a-haystack test measures a language model's ability to retrieve information from long context by planting a single fact at different positions in long unrelated text and asking the model to find it; results are usually shown as a heatmap by context length and position.
Lost in the middle is the finding that models use information mid-context less reliably; this test is a simple way to expose such position effects. Passing it does not mean a model understands long context; it measures only superficial retrieval.
- Topic
- Evaluation and quality
- Also known as
- NIAH
How the needle-in-a-haystack test works
- One sentence holding the target fact is planted in long filler text.
- The sentence's position moves from start to end, while context length rises from short values to the model's limit.
- For each combination the model's answer about the fact is scored right or wrong.
- Results appear as a length by position heatmap; multi-needle and multi-hop variants make harder tests.
In more detail
Origins of the test and the saturation of single-needle NIAH
The modern LLM needle-in-a-haystack evaluation pattern was popularised in late 2023 by Greg Kamradt to stress-test 128K, 200K context windows. Since then, single-needle recall has largely saturated in frontier and open-weight models, prompting a shift toward multi-needle reasoning, variable tracking and semantically indirect retrieval suites.
In-domain needles without lexical overlap
Many early NIAH setups plant an out-of-distribution fact, such as a pizza topping rule inside Paul Graham essays, which creates a sharp lexical contrast that models spot easily. When the needle blends stylistically and topically into homogeneous enterprise documents or financial reports, retrieval accuracy drops noticeably.
KV cache memory and time to first token at long context
Even if an on-premise model retains high retrieval accuracy at 128K tokens, long contexts dramatically expand the KV cache in GPU memory and increase Time-To-First-Token (TTFT) latency. Evaluating long-context usability therefore requires plotting accuracy alongside VRAM footprint and latency across sequence lengths.
Commonly confused terms
| Term | How it differs |
|---|---|
| RULER and BABILong | Needle-in-a-Haystack (NIAH) tests isolated single-fact retrieval from filler text, whereas RULER expands long-context evaluation to multi-needle, variable tracking, aggregation and multi-hop QA where models degrade much faster. |
| RAG versus long-context prompting | Retrieval-Augmented Generation (RAG) indexes documents externally and injects only top relevant chunks at inference time, whereas long-context prompting stuffs whole documents into the context window; NIAH helps decide the safe boundary between the two. |
Why the needle-in-a-haystack test matters for on-premises AI
Organisations feeding long documents to a model in one go need to know how much of the advertised context it really uses. A 2024 benchmark measuring real context size showed that despite near-perfect scores on this test, models dropped sharply on harder tasks as context grows, with only half of those claiming 32K tokens or more performing satisfactorily at that length. Another study shows performance shifts markedly with the position of relevant information.
A reinsurer runs single and multi-needle tests built from its own documents before feeding long treaty texts straight to a model, using RAG for documents beyond the length where performance drops.
Common questions
Does passing the test mean a model understands long context?
No. It measures only finding one fact. Performance can drop markedly on tasks such as multi-hop tracing and aggregation.
How should organisations use the test?
With text and facts from their own document types at varied lengths and positions, adding multi-needle and multi-hop variants to the single-needle test.
Related terms
Sources
- Hsieh et al., RULER: What's the Real Context Size of Your Long-Context Language Models?, 2024
- Liu et al., Lost in the Middle: How Language Models Use Long Contexts, 2023
In the glossary: Glossary, M to Q