NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Haystack Engineering: Context Engineering for Heterogeneous and Agentic Long-Context Evaluation

Mufei Li, Dongqi Fu, Limei Wang, Si Zhang, Hanqing Zeng, Kaan Sancak, Ruizhong Qiu, Haoyu Wang, Xiaoxin He, Xavier Bresson, Yinglong Xia, Chonglin Sun, Pan Li · DOI

Abstract

Traditional "needle-in-a-haystack" (NIAH) tests for long-context Large Language Models (LLMs) often rely on synthetic, random distractors that fail to capture the complexities of real-world Retrieval-Augmented Generation (RAG) and multi-step agentic workflows. We introduce HaystackCraft, a benchmark designed to evaluate LLMs against realistic, noisy contexts generated by actual retrieval algorithms and dynamic agentic tasks. Our evaluation of 15 state-of-the-art models reveals that while models are increasingly robust to long contexts, they remain highly vulnerable to cascading errors in multi-round reasoning. We also demonstrate that graph-based reranking significantly enhances performance by structuring the context more effectively.

Introduction

The ability of LLMs to process massive context windows is often measured using NIAH benchmarks, where a single fact is buried in unrelated text. However, in practice, the "haystack" is not random; it consists of "near-miss" documents retrieved by semantic or keyword search. Furthermore, modern LLMs increasingly operate as agents, performing iterative research where their own intermediate outputs populate the context. This paper addresses the gap between synthetic benchmarks and real-world deployment by proposing "Haystack Engineering."

The HaystackCraft Benchmark

HaystackCraft moves beyond static, random distractors through two primary modes:

  1. Static NIAH with Heterogeneous Retrievers: Contexts are built using Sparse (BM25), Dense (Qwen3-Embedding), Hybrid, and Graph-based (Personalized PageRank) retrievers. This tests the model's ability to distinguish the "needle" from plausible but incorrect "near-misses."
  2. Dynamic NIAH (Agentic Simulation): This mode simulates multi-round research loops. The LLM must summarize findings and refine its query for subsequent retrieval rounds, testing its robustness against error propagation.

Experimental Methodology

The benchmark utilizes a recent (April 2025) English Wikipedia dump to minimize training data contamination, treating articles as full documents. The evaluation set comprises 500 high-quality questions drawn from Natural Questions (single-hop) and MuSiQue (multi-hop). We evaluated 15 long-context models, including Llama 3.1, Qwen2.5, GPT-5, and Gemini 2.5 Pro, at context sizes up to 128K tokens.

Key Findings

  • The "Near-Miss" Penalty: Dense retrievers, while effective at finding relevant documents, often pull in factually adjacent distractors. This makes the reasoning task harder for the LLM compared to the lexically distinct distractors found by sparse retrievers.
  • Graph-Based Reranking: Utilizing Wikipedia's hyperlink structure via Personalized PageRank (PPR) significantly improves retrieval recall and downstream LLM accuracy. By pushing structurally isolated distractors lower in the context window, PPR reduces the cognitive load on the model.
  • "Wide" vs. "Deep" Robustness: Models generally handle a single large context ("wide") better than iterative, multi-round reasoning ("deep"). In agentic loops, LLMs often become their own source of noise, with early hallucinations or query drifts cascading into failure.

Results and Discussion

Our results show that graph-based reranking (PPR) can improve LLM performance by up to 44% in specific setups. However, every model tested showed a performance decline as reasoning rounds increased. Notably, static NIAH performance is a poor predictor of agentic robustness; models that excel at finding a single needle often collapse when forced to manage their own evolving context and cascading errors.

Conclusion

HaystackCraft highlights a critical bottleneck in current LLM development: agentic robustness. While context window sizes continue to grow, the ability of models to self-correct and resist cascading errors in dynamic workflows remains limited. Future research must focus on improving these iterative reasoning capabilities to enable truly autonomous and reliable AI agents.

Summary updated

BibTeX
@misc{li2025haystack,
  author = {Mufei Li and Dongqi Fu and Limei Wang and Si Zhang and Hanqing Zeng and Kaan Sancak and Ruizhong Qiu and Haoyu Wang and Xiaoxin He and Xavier Bresson and Yinglong Xia and Chonglin Sun and Pan Li},
  title = {{Haystack Engineering: Context Engineering for Heterogeneous and Agentic Long-Context Evaluation}},
  year = {2025},
  eprint = {2510.07414},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2510.07414},
}
Figure