Targeted Retrieval, Compact Representations: How CoT Reasoning Improves Long-Context Counting

Liang Twist Shan, Tianyu Hu, Hao Yan, Yiqiao Zhong
9/30/2026
cs.AIcs.CLcs.LG

No official code found. Best implementation: Twist-Shan/CoT_NIAH_Counting (0 stars, last commit 2026-09-23).

Abstract

Large language models (LLMs) have been rapidly improving in long-context tasks, powered by Chain-of-Thought (CoT) reasoning. However, the internal mechanisms underlying this improvement remain unclear. We investigate these mechanisms through a needle-in-a-haystack (NIAH) counting task, where an LLM is asked to count the number of records dispersed in a long text. Across twelve model comparison groups, Thinking (or reasoning) improves counting accuracy over Non-thinking, with pronounced gains at larger counts. This motivates our mechanistic analysis, which identifies two contrasting mechanisms: (i) broad retrieval, where Non-thinking models broadly attend to multiple needles; (ii) targeted retrieval, where Thinking models use enumeration in CoT traces to successively retrieve needles. Targeted retrieval concentrates attention on individual needles and is accompanied by more compact internal representations. Moreover, causal intervention analysis suggests that Thinking models use the CoT trace to maintain and update an internal counter as needles are successively retrieved, even without explicit numbering. In small controlled experiments, both retrieval mechanisms and counter states emerge under standard autoregressive training. Together, our results connect long-context retrieval with representation geometry of counting, supporting a state-tracking account of CoT reasoning.

View on arXivView PDF

Code Implementations(1)

Code for Targeted Retrieval, Compact Representations: How CoT Reasoning Improves Long-Context Counting, including both controlled synthetic Transformers and open-weight models experiments.

00Sep 19, 20263 weeks agoNOASSERTION
cot-reasoningllm-interpretabilityneedle-in-a-haystack

Similar Papers

Cite this paper

@article{shan2026targeted,
  title  = {Targeted Retrieval, Compact Representations: How CoT Reasoning Improves Long-Context Counting},
  author = {Liang Twist Shan and Tianyu Hu and Hao Yan and Yiqiao Zhong},
  year   = {2026},
  eprint = {2609.38958},
  archivePrefix = {arXiv},
  url    = {http://arxiv.org/abs/2609.38958v1}
}

Add a badge to your README

Shows whether official code is linked for this paper.

Code of Paper badge
[![Code of Paper](https://codeofpaper.com/badge/2609.38958.svg)](https://codeofpaper.com/paper/2609.38958)

Discussion