arXiv:2609.38958cs.AISep 2026

Manuscript Offprint

arXiv:2609.38958

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

Liang Twist Shan, Tianyu Hu, Hao Yan, Yiqiao Zhong

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

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.

1 Introduction

Reasoning over long contexts requires large language models (LLMs) to retrieve relevant information while keeping track of progress. Chain-of-Thought (CoT) reasoning is a widely used inference paradigm in which models generate intermediate steps before producing a final answer (Wei et al., 2022). As tasks become more complex, context engineering and agent memory address how information and task state are maintained and tracked during inference (Rajasekaran et al., 2025; Packer et al., 2023). Despite these advances, a fundamental question remains: how do internal state-tracking mechanisms support long-context retrieval and reasoning?

We present long-context counting as a controlled testbed for studying state tracking, reducing much of the semantic ambiguity of open-ended reasoning. Adapted from needle-in-a-haystack (NIAH) benchmarks (Kamradt, 2023; Hsieh et al., 2024), this task asks a model to count records (or needles, denoted by N1,N2,N3,…N_{1},N_{2},N_{3},\ldots) scattered throughout a long passage (haystack), such as a passage drawn from Paul Graham essays. For example, the following input prompt contains two audit records followed by a query:

“<passage> …\ldots In the 2024 city score audit, Paris received a score of 92. …\ldots In the 2024 city score audit, Vienna received a score of 73. …\ldots </passage> How many city-score audit records are in the passage?”

A complete 1k-token passage example is provided in App. C.1. Accurate counting requires accounting for all relevant records without omissions or double counting. It thus combines retrieval and aggregation, two operations that language agents also need when working with long contexts, while providing explicit record and count labels for analyzing internal state tracking.

We compare Non-thinking mode (or models), which directly outputs a count such as ‘‘Total: 8’’, with Thinking mode (or models), which first generates a CoT trace. Our main analysis uses native Thinking traces. App. F extends the analysis to instructed unnumbered enumeration with Thinking disabled, showing that key aspects of the observed pathway also arise outside a dedicated Thinking mode. We call records in the prompt needles and their generated mentions trace items.

Our study goes beyond existing mechanistic analysis, as standard in-context learning and steering often works for short contexts without explicit CoT traces (Hendel et al., 2023; Todd et al., 2024; Turner et al., 2023; Rimsky et al., 2024). Here, counting provides a natural setting for studying this dynamic process: which needle to retrieve next and how to keep track of the count. Starting from an input prompt s1:ts_{1:t}, a model emits tokens st+1,st+2,…s_{t+1},s_{t+2},\ldots one by one, iteratively growing the context s1:t→s1:(t+1)→s1:(t+2)→⋯s_{1:t}\to s_{1:(t+1)}\to s_{1:(t+2)}\to\cdots.

We examine how retrieval interacts with internal representations of counting progress, and whether these mechanisms guide subsequent reasoning generation. Roughly speaking, representations at prompt-needle and trace-item positions follow a curve as the running index increases. We call internal counting representations counter states, and use running index kk for the number of prompt needles encountered or trace items enumerated. The answer state is the hidden representation at the answer query, labeled by total count NN. For deeper understanding, we ask: Q1 How do the two modes retrieve needles? Q2 How compact are their count representations? Q3 Do evolving counter states causally guide retrieval and final count prediction?

Our key finding is a contrast between two retrieval mechanisms. Non-thinking models depend on broad retrieval: they broadly attend to all prompt needles at the answer token but produce noisy representations and unreliable counts. Thinking models depend on targeted retrieval: enumeration traces couple successive record retrievals to compact, separable representations of progress, which may help avoid omissions and double counting, thereby improving the count accuracy. Fig. illustrates both modes; see App. A for details. Below, we summarize our main contributions.

  1. A comprehensive comparison across model families. We characterize how counting accuracy varies with needle count and context length, and show that Thinking substantially improves accuracy.
  2. A mechanistic contrast between the two modes. We answer Q1 and Q2 by showing that Non-thinking models collect noisy representations at prompt needles through broad retrieval, while Thinking models develop compact representations through targeted retrieval. For Q3, causal intervention results support evolving counter states in CoT traces (Secs. 3–4).
  3. A controlled experiment on the emergence of two mechanisms. We train small transformers from scratch under autoregressive losses, with or without CoT traces, which reproduce key aspects of Thinking and Non-thinking mechanisms (Sec. 5).

References (45)