Ultralytics YOLO27: Join the waitlist.
License YOLO
Back to Ultralytics Glossary

Needle In A Haystack (NIAH)

Needle in a haystack (NIAH) tests whether an LLM can find one fact hidden at any depth of a long context. Covers its origin, results, variants, and limits.

Needle in a haystack (NIAH) is an evaluation that hides a single fact, the needle, at a chosen depth inside a long stretch of unrelated text, the haystack, and asks a large language model (LLM) to retrieve it. Repeating the test across many context lengths and needle positions produces a heatmap that shows where in its context window a model stops finding information. Greg Kamradt introduced the test in November 2023 with an open-source NIAH repository: he hid the sentence "The best thing to do in San Francisco is eat a sandwich and sit in Dolores Park on a sunny day" inside a collection of Paul Graham's essays and asked each model for the best thing to do in San Francisco. The first runs tested OpenAI's GPT-4 Turbo, with a 128K-token window, and Anthropic's Claude 2.1, with 200K.

How the Test Works#

The haystack is trimmed to a target length in tokens, the needle is inserted at a fraction of that length, from 0% at the start to 100% at the end, and the model's answer is scored against the needle. A full run sweeps both axes:

The needle has nothing to do with the essays around it, so the test isolates one ability of the attention mechanism: locating and copying a specific passage anywhere in a long prompt.

Headline Results#

Kamradt's first heatmaps showed GPT-4 Turbo missing the needle above about 73K tokens, mostly when it sat between about 7% and 50% depth, close to the mid-prompt weakness that the Lost in the Middle study measured the same year. Claude 2.1 scored 27% on the original prompt, and Anthropic raised that to 98% with a prompt engineering change: starting Claude's answer with "Here is the most relevant sentence in the context:". NIAH became a standard chart in model launches. Google's Gemini 1.5 report claimed over 99% retrieval up to at least 10 million tokens and extended the test to audio and video haystacks, and Anthropic reported that Claude 3 Opus passed 99% recall and sometimes remarked that the needle looked artificially inserted into the text.

Variants and Limits#

Plain NIAH is close to a lexical search, since the question shares its key words with the needle, and frontier models now saturate it. Harder variants change what the needle demands:

  • Passkey retrieval: A random number hidden in repeated filler text, used in 2023 to test Landmark Attention before NIAH existed.
  • Multi-needle and aggregation: NVIDIA's RULER benchmark hides several keys and values, adds variable tracking and common-word aggregation, and found that only half of 17 models claiming 32K tokens or more performed satisfactorily at 32K.
  • No literal match: NoLiMa words each question so it barely overlaps with the needle. At 32K tokens, 11 of 13 models fell below half of their short-context scores.

The accuracy loss these harder tests expose as inputs grow is called context rot. NIAH also differs from retrieval-augmented generation (RAG), where an external vector search picks passages before the model reads them, and from anomaly detection, which looks for unknown deviations instead of a known target.

Needles in Images#

Computer vision uses the same phrase for small object detection: a person in aerial imagery, a ship in a satellite scene, or a tumor cell in a whole-slide pathology scan for medical image analysis. The obstacle is resolution: Ultralytics YOLO26 resizes each image to 640 pixels on its longest side by default, so a 4,000-pixel drone frame shrinks more than sixfold and a 10-pixel pedestrian ends up under 2 pixels wide. Sliced inference with SAHI runs YOLO26 on overlapping slices, such as 256 × 256 tiles, at native resolution and merges the bounding boxes. In the SAHI paper's tests on the VisDrone and xView aerial datasets, slicing raised object detection AP by 5.1% to 6.8% for three different detectors.

Explore solutions

Computer vision in sports

Computer vision in sports

Discover how Ultralytics YOLO powers sports vision with player and ball tracking, performance analytics, coaching insights, and athlete rehabilitation.
Learn more
Computer vision for aerial imagery

Computer vision for aerial imagery

Transform drone and aerial imagery into real-time insights for agriculture, aquaculture, environmental monitoring, conservation, and geospatial analysis with Ultralytics YOLO.
Learn more
Computer vision in aerospace

Computer vision in aerospace

Bring vision AI to aerial monitoring with Ultralytics YOLO models. Power drones, aerospace workflows, environmental conservation & geospatial industries with computer vision solutions.
Learn more
Computer vision in security

Computer vision in security

Protect every site with Ultralytics YOLO models. Vision AI powers perimeter monitoring, threat detection, license-plate recognition, and workplace safety.
Learn more
Computer vision in robotics

Computer vision in robotics

Power smarter machines with Ultralytics YOLO models. Vision AI in robotics drives autonomous navigation, perception, object tracking, and real-time control.
Learn more
Computer vision in logistics

Computer vision in logistics

Streamline logistics with Ultralytics YOLO models. Vision AI enables package inspection, sorting, vehicle tracking, and real-time warehouse safety monitoring.
Learn more
Computer vision in retail

Computer vision in retail

Reimagine retail with Ultralytics YOLO models. Vision AI powers inventory tracking, shelf monitoring, queue management, and smarter customer insights.
Learn more
Computer vision in healthcare

Computer vision in healthcare

Build healthcare solutions with Ultralytics YOLO models. Vision AI in healthcare powers faster medical imaging, smarter diagnostics, and patient monitoring.
Learn more
Computer vision in manufacturing

Computer vision in manufacturing

Optimize manufacturing with Ultralytics YOLO models. Vision AI drives quality control, defect detection, PPE compliance, and assembly line automation.
Learn more
Computer vision in automotive

Computer vision in automotive

Apply computer vision in automotive with Ultralytics YOLO models. Vision AI elevates road safety, driver assistance, and vehicle automation for smarter roads.
Learn more
Computer vision in agriculture

Computer vision in agriculture

Bring vision AI to smart agriculture with Ultralytics YOLO models. Power crop monitoring, livestock tracking, and precision farming for higher, smarter yields.
Learn more
Computer vision in sports

Computer vision in sports

Discover how Ultralytics YOLO powers sports vision with player and ball tracking, performance analytics, coaching insights, and athlete rehabilitation.
Learn more
Computer vision for aerial imagery

Computer vision for aerial imagery

Transform drone and aerial imagery into real-time insights for agriculture, aquaculture, environmental monitoring, conservation, and geospatial analysis with Ultralytics YOLO.
Learn more
Computer vision in aerospace

Computer vision in aerospace

Bring vision AI to aerial monitoring with Ultralytics YOLO models. Power drones, aerospace workflows, environmental conservation & geospatial industries with computer vision solutions.
Learn more
Computer vision in security

Computer vision in security

Protect every site with Ultralytics YOLO models. Vision AI powers perimeter monitoring, threat detection, license-plate recognition, and workplace safety.
Learn more
Computer vision in robotics

Computer vision in robotics

Power smarter machines with Ultralytics YOLO models. Vision AI in robotics drives autonomous navigation, perception, object tracking, and real-time control.
Learn more
Computer vision in logistics

Computer vision in logistics

Streamline logistics with Ultralytics YOLO models. Vision AI enables package inspection, sorting, vehicle tracking, and real-time warehouse safety monitoring.
Learn more
Computer vision in retail

Computer vision in retail

Reimagine retail with Ultralytics YOLO models. Vision AI powers inventory tracking, shelf monitoring, queue management, and smarter customer insights.
Learn more
Computer vision in healthcare

Computer vision in healthcare

Build healthcare solutions with Ultralytics YOLO models. Vision AI in healthcare powers faster medical imaging, smarter diagnostics, and patient monitoring.
Learn more
Computer vision in manufacturing

Computer vision in manufacturing

Optimize manufacturing with Ultralytics YOLO models. Vision AI drives quality control, defect detection, PPE compliance, and assembly line automation.
Learn more
Computer vision in automotive

Computer vision in automotive

Apply computer vision in automotive with Ultralytics YOLO models. Vision AI elevates road safety, driver assistance, and vehicle automation for smarter roads.
Learn more
Computer vision in agriculture

Computer vision in agriculture

Bring vision AI to smart agriculture with Ultralytics YOLO models. Power crop monitoring, livestock tracking, and precision farming for higher, smarter yields.
Learn more
Computer vision in sports

Computer vision in sports

Discover how Ultralytics YOLO powers sports vision with player and ball tracking, performance analytics, coaching insights, and athlete rehabilitation.
Learn more
Computer vision for aerial imagery

Computer vision for aerial imagery

Transform drone and aerial imagery into real-time insights for agriculture, aquaculture, environmental monitoring, conservation, and geospatial analysis with Ultralytics YOLO.
Learn more
Computer vision in aerospace

Computer vision in aerospace

Bring vision AI to aerial monitoring with Ultralytics YOLO models. Power drones, aerospace workflows, environmental conservation & geospatial industries with computer vision solutions.
Learn more
Computer vision in security

Computer vision in security

Protect every site with Ultralytics YOLO models. Vision AI powers perimeter monitoring, threat detection, license-plate recognition, and workplace safety.
Learn more
Computer vision in robotics

Computer vision in robotics

Power smarter machines with Ultralytics YOLO models. Vision AI in robotics drives autonomous navigation, perception, object tracking, and real-time control.
Learn more
Computer vision in logistics

Computer vision in logistics

Streamline logistics with Ultralytics YOLO models. Vision AI enables package inspection, sorting, vehicle tracking, and real-time warehouse safety monitoring.
Learn more
Computer vision in retail

Computer vision in retail

Reimagine retail with Ultralytics YOLO models. Vision AI powers inventory tracking, shelf monitoring, queue management, and smarter customer insights.
Learn more
Computer vision in healthcare

Computer vision in healthcare

Build healthcare solutions with Ultralytics YOLO models. Vision AI in healthcare powers faster medical imaging, smarter diagnostics, and patient monitoring.
Learn more
Computer vision in manufacturing

Computer vision in manufacturing

Optimize manufacturing with Ultralytics YOLO models. Vision AI drives quality control, defect detection, PPE compliance, and assembly line automation.
Learn more
Computer vision in automotive

Computer vision in automotive

Apply computer vision in automotive with Ultralytics YOLO models. Vision AI elevates road safety, driver assistance, and vehicle automation for smarter roads.
Learn more
Computer vision in agriculture

Computer vision in agriculture

Bring vision AI to smart agriculture with Ultralytics YOLO models. Power crop monitoring, livestock tracking, and precision farming for higher, smarter yields.
Learn more

Let's build the future of AI together!

Begin your journey with the future of machine learning