Needle in a haystack test (NIAH)
A needle in a haystack test hides one fact inside a long document and asks a language model to find it, to check whether the model can use its whole context window.
The test is simple. Take a long stretch of unrelated text, the haystack, and insert a single out-of-place sentence, the needle, at a chosen depth. Then ask the model a question that only the needle answers. Repeat this across a grid of document lengths and positions, and the results show where in its context window a model reliably finds information and where it starts to miss it.
The best-known version was run by Greg Kamradt in November 2023. His haystack was a collection of Paul Graham's essays and his needle a sentence claiming the best thing to do in San Francisco is eat a sandwich and sit in Dolores Park on a sunny day. He ran it on OpenAI's GPT-4 with a 128,000-token context and on Anthropic's Claude 2.1. His open-source code now also supports several needles at once and chains of linked values.
The test is sensitive to small details. Anthropic reported on December 6, 2023 that Claude 2.1 often declined to answer because the needle sentence looked out of place in the essays. Adding a short phrase to the start of the model's answer, pointing it to the most relevant sentence, raised its score on the original evaluation from 27% to 98%. The result measured the prompt as much as the memory.
Developers soon reported near-perfect scores. Google's Gemini 1.5 technical report, first published in March 2024, described retrieval above 99% at up to at least 10 million tokens. That is why researchers moved on. The RULER benchmark, published in April 2024, called basic needle retrieval a superficial form of long-context understanding and added multiple needles, multi-hop tracing and aggregation. Of 17 models tested, all claiming context windows of 32,000 tokens or more, only half held up well at 32,000.
In practice, a perfect needle score shows that a model can find a single quoted fact in a long input. It does not show that the model can reason over everything in that input, connect facts spread far apart, or summarise it faithfully. Those need harder tests.
It is a long-context test in which one fact is hidden at a set position inside a long document, and the model is asked a question only that fact answers. Running it at many document lengths and positions shows how reliably the model retrieves information from different parts of its context window. The widely used version was built by Greg Kamradt, who in November 2023 ran it on GPT-4 with a 128,000-token context and on Claude 2.1. His code is open source on GitHub. Similar retrieval tests have since been built into long-context benchmarks such as RULER. Finding one unusual sentence is much easier than using a long document well. The RULER benchmark showed that models scoring almost perfectly on basic needle retrieval degraded substantially on harder long-context tasks such as tracing chains of facts and aggregating information as inputs grew longer.Common questions
What is the needle in a haystack test for LLMs?
Who created the needle in a haystack test?
Why is the needle in a haystack test not enough?
Search for "needle in a haystack LLM", "long-context evaluation" or "RULER benchmark". Reliable sources include the original test code on GitHub, long-context benchmark papers on arXiv, and model developers' technical reports, which usually describe their own needle results. Treat headline claims of perfect long-context recall with care unless harder multi-fact tests are reported too.Finding more
Working out what this means for your business?
Aileron Shift builds the systems behind this site: AI newsrooms that research, write and publish on their own, marketing automation and WordPress hosting for companies that need to move faster than their competitors.