Dashboard

What Is a Needle in a Haystack Test for AI Models?

What the needle in a haystack test measures, why a perfect score proves less than it seems, and how to run your own version in under 40 lines of Python.

Cecilia Iona
Cecilia Iona
Senior Editor, AI & Product
7 October 20261 min read

A needle in a haystack test hides one fact, the needle, at a chosen position inside a long block of unrelated text, the haystack, and asks the model to find it. It measures whether a model can retrieve a single planted fact from a long context. A perfect score proves less than it sounds like, because real work rarely looks like one odd sentence in a pile of filler.

The test became widely known in late 2023, when the developer Greg Kamradt ran it on long-context models and published the results as a grid. Since then, near-perfect needle scores have become a standard line in model announcements.

How the test works

The setup is simple, which is why it spread.

  1. Pick a long body of ordinary text to act as the haystack, such as a collection of essays.

  2. Pick a sentence that does not belong in it. That is the needle, for example a made-up access code.

  3. Insert the needle at a chosen depth, from the very start to the very end of the text.

  4. Ask a question that only the needle can answer, and check whether the reply contains it.

  5. Repeat across several context lengths and several depths, then plot pass or fail on a grid.

The grid is the point. A model might retrieve the needle perfectly at 8,000 tokens and miss it at 100,000, or find it at the start and the end but lose it in the middle. A single average score hides that pattern. If the unit of measure is new to you, the entry on what a token is in AI explains why length is counted that way.

What a good score does and does not tell you

A pass tells you that the model can locate and repeat one exact fact across the tested length. That is a real capability and a useful floor.

It does not tell you much else:

  • Whether the model can combine two facts from different places in the text.

  • Whether it can pick the right fact when several look similar, such as two contract clauses with different dates.

  • Whether it can summarize the whole document fairly rather than the loudest parts.

  • Whether it still follows your instructions when they sit at the start of a very long prompt.

The needle is conspicuous. A sentence about an access code in an essay about startups is out of place, so a model can find it by noticing what does not fit, which is easier than finding a relevant detail in a document where everything fits. Position also matters. A study titled Lost in the Middle found that models often do best when the relevant information sits at the beginning or end of the input and worse when it is in the middle of a long context.

Run your own version

You do not need a benchmark suite. This is a small grid test you can run against any model with an API. Use ordinary prose as the filler and avoid repeated text, since repetition makes the needle easier to spot.

python

FILLER = open("filler.txt").read().split("\n\n") # paragraphs of ordinary prose NEEDLE = "The access code for the supply room is 4417." QUESTION = "What is the access code for the supply room? Answer with the number only."

def build_haystack(target_words, depth): paragraphs, words = [], 0 while words < target_words: p = random.choice(FILLER) paragraphs.append(p) words += len(p.split()) paragraphs.insert(int(len(paragraphs) * depth), NEEDLE) return "\n\n".join(paragraphs)

def ask_model(prompt): raise NotImplementedError("call your model API here")

results = {} for target_words in (2000, 8000, 32000): for depth in (0.0, 0.25, 0.5, 0.75, 1.0): hits = 0 for _ in range(5): answer = ask_model(build_haystack(target_words, depth) + "\n\n" + QUESTION) hits += "4417" in answer results[(target_words, depth)] = hits / 5 ```

Words are only a rough stand-in for tokens, which is fine for spotting where recall falls off. Five repeats per cell smooth out randomness, because a model can pass once and fail the next time.

Make it harder

Once the basic version passes, change the test so it looks more like your real use.

  • Add a decoy. Insert a second sentence with the same shape, such as the access code for the server room, and ask for the supply room.

  • Use two needles that must be combined, for example a unit price in one place and a quantity in another, and ask for the total.

  • Swap the filler for your own documents, so the needle competes with text that looks like the real thing.

If a model that scores perfectly on the basic grid drops sharply on the decoy version, you have learned something no headline number would have told you. The same logic applies to any claim about long inputs, which is why it pays to read whether a bigger context window means better answers before paying for one. For turning a one-off check like this into something you can rerun after every model change, see what an AI eval is. The wider picture of how models handle input is in how AI models work, and the basic concept is in the entry on the context window.

FAQ

What is the needle in a haystack test?

It is a check that hides one planted fact inside a long document and asks the model to retrieve it. Results are usually shown as a grid of context length against needle position.

Does a perfect score mean a model handles long documents well?

No. It shows the model can retrieve one conspicuous fact. It says little about combining facts, ignoring similar distractors, or summarizing accurately over a long input.

What is a multi-needle test?

A multi-needle test plants several facts in the same haystack and asks for all of them, or for a result that depends on more than one. It is harder than the single-needle version and closer to real tasks.

How is this different from the context window size?

The context window is the maximum amount of text a model accepts. The needle test checks how well the model uses that text. A model can accept a very long input and still handle the middle of it poorly.

How did this land?

About the author

Cecilia Iona
Cecilia Iona

Senior Editor, AI & Product

Cecilia leads the Swarmz editorial desk. She has spent a decade turning complex AI and product topics into writing people actually finish, and she owns the blog's quality bar.

Share

Get the next post in your inbox

One email a month. Product updates, engineering posts, and the best of Built with Swarmz.

I agree to receive emails about AI building tips and Swarmz product news. Unsubscribe any time.