Skip to content

Needles and Near Misses

Long-context tests usually hide one fact in filler and ask for it back. I did that with four local models, then added the thing real documents have: other facts that look almost the same.

Case study5 min read

Solid: the fact on its own, found at every length. Dashed: the same fact among three near misses.
Role
Solo project: question, design, code, analysis and write-up.
When
October 2026
Stack
Python, Ollama, Qwen3, Llama 3.1, Gemma 3, WikiText, matplotlib, pytest

The problem

Retrieval-augmented systems and agents put long documents in front of a model and trust it to find the one line that matters. Single-needle tests say that works. Real documents are full of lines that look like the one that matters.

What I built

40 items, each an invented fact planted in Wikipedia text at five depths and four lengths (1k to 16k tokens), alone, among three near-miss facts, left out entirely, and split into two facts far apart. Three local models on the full grid and a 30B thinking model on part of it: 6,140 calls, with a noise floor from repeating the identical prompt.

What happened

The fact alone was found 98 to 100% of the time at every length. Near misses cut recall by 18 to 43 points at 4k and more at 16k, wrong answers named a near miss rather than inventing one, and two facts far apart were often not combined.

  • 98–100%

    recall of one fact on its own, at every length up to 16k

  • 38–69%

    recall at 16k with three near-miss facts nearby

  • 98%

    of missing facts Gemma 3 filled in with a near miss

  • 0 of 200

    answers changed for the 8B models by sending the same prompt twice

01

The question

A model given a long document has to find the part that answers the question and ignore the rest. The usual test, a needle in a haystack, plants one sentence in unrelated text. Models now pass it at lengths far beyond anything here, which says they can find a sentence that stands out. It doesn't say whether they can find the right one when several look alike.

So I measured both, on models small enough to run on a laptop, with every prediction committed to git before the full run.

02

The setup

Each item is an invented fact the model cannot already know, such as who designed the Briskhaven Lighthouse, planted in Wikipedia text from WikiText-2. A closed-book control confirmed it: with no document, no model answered any question correctly. Each fact then goes in at five depths (start, 25%, middle, 75%, end) and four lengths (1k, 4k, 8k and 16k tokens), with the longer documents containing the shorter ones so the comparisons are paired. Then:

  • Near misses: three similar facts nearby, each naming a different person. One has the same name with another type of thing (Briskhaven Viaduct), and two have a type match with the name's ending or beginning changed (Briskstone, Vellhaven).
  • Fact absent: the key fact left out, with and without the near misses, where the right reply is “not stated”.
  • Two hops: “where was the designer of X born?”, which needs the designer fact and the person's birthplace, placed next to each other or at 10% and 90% of the document, with two other people's birthplaces as decoys.
  • A repeat of the identical prompt as a noise floor, on the cells whose answers vary.

03

Length alone costs nothing

With the fact on its own, qwen3 8B, Llama 3.1 8B and Gemma 3 12B answered 98 to 100% of items at every length and every depth. Going from 1k to 16k tokens changed recall by +0.0 points for all three. By the single-needle measure, these models read 16k tokens perfectly.

04

Near misses are what break it

With three look-alike facts in the document, recall at 4k fell by 18.5 points for qwen3 (to 80%), 42.5 for Llama (to 57%) and 32.5 for Gemma (to 68%). Now length did matter: at 16k recall was 69%, 38% and 52%.

Position started to matter too, but differently for each model. Gemma lost the middle: at 8k it found the fact 95% of the time at the start and 25% in the middle, and 12% in the middle at 16k. qwen3 did worst at 75% depth at every length. Llama favoured the start over the end by 27.5 points (pooled over 1k to 8k). With the fact alone, no depth was worse than another, so position effects only show up once there is something to confuse.

Position effects only show up once there is something to confuse.
Recall among near misses, by where the fact sits and how long the document is.

05

Wrong answers come from the near misses

The models almost never made a name up. A wrong answer named a near miss's person, and for the two 8B models usually one placed before the real fact: 87% and 82% of the time, against 63 to 65% if they had picked one at random. Gemma was close to random.

Leaving the fact out showed the same thing more starkly. With nothing similar in the document, every model said “not stated” on 98 to 100% of items. With the near misses there, Gemma named one of their people on 98% of items, qwen3 on 57% and Llama on 50%. A near miss is enough for a confident wrong answer.

What each reply named at 4k. Red: a near miss's person. Grey: “not stated”.

06

Two facts far apart are often not combined

Next to each other, the two facts of a two-hop question were combined 92 to 100% of the time. At 10% and 90% of a 16k document, Llama got 30% and 10% (one figure for each order), Gemma 35% and 20%, and qwen3 80% and 60%. Each model answered the second fact on its own 75 to 100% of the time, so the loss is in connecting them, not in finding either one.

I predicted the order would matter, with the birthplace before the person being harder. It didn't, consistently: that was the one prediction of thirteen that failed.

07

A thinking model wasn't fooled

qwen3 30B with thinking on, run on 20 items at 4k, found the fact among near misses 97% of the time, 21 points above qwen3 8B on the same items. It named a near miss on 1 of 20 items with the fact left out, and combined far-apart facts every time. That mixes size with thinking, rests on few items, and took about 7 seconds a call against 3.

08

What to keep in mind

Small local models and one kind of near miss, so the limits are part of the result:

  • 4-bit models at temperature 0 with one prompt template. Hosted models handle far longer contexts, and the lengths here stop at 16k.
  • The planted facts are short and about invented things, so they differ in style from the Wikipedia text around them, and every question repeats the entity's name. Both make finding the fact easier than in real documents.
  • The near misses are one hand-written kind: similar names with the same relation.
  • The 16k cells run on 16 to 20 items, so their intervals are wide.
  • Thirteen predictions were committed before the full run; twelve held. The answers were graded by code, and a hand-labelled check of 80 sampled replies is drawn and pending.

What I'd carry forward

  1. 1A needle-in-a-haystack pass says little about real documents. Test with near misses: facts of the same shape about similar things.
  2. 2Leave the answer out on some items. A model that says “not stated” on an empty document may still fill the gap from a look-alike.
  3. 3Ask questions that need two facts far apart. Finding each fact is not the same as connecting them.
  4. 4Check position only once the task is hard. With an easy needle every depth looks fine.
  5. 5Measure the noise first. Here the identical prompt changed 0 to 1 answers in 200, so the larger effects are real.

Get in touch

Questions about my work, a role, or a project idea. Send a message here or email me directly.

Srikarmks@gmail.com