Don't Take Things Out of Context: Attention Intervention for Enhancing Chain-of-Thought Reasoning in Large Language Models

Shaotian Yan, Chen Shen, Wenxiao Wang, Liang Xie, Junjie Liu, Jieping Ye

TL;DR

While few-shot Chain-of-Thought (CoT) prompting dramatically improves LLM reasoning, models frequently get distracted by isolated, local tokens within the exemplars instead of just mimicking the overarching logic. To combat this, the authors introduce Few-shot Attention Intervention (FAI), a lightweight, inference-time method that dynamically identifies these "isolated" tokens and zeroes out their attention weights to the output token, yielding consistent performance gains across multiple reasoning benchmarks.

What Problem Does This Paper Address?

Few-shot Chain-of-Thought (CoT) prompting is the gold standard for guiding Large Language Models (LLMs) through complex multi-step reasoning tasks. Ideally, the model should treat the few-shot exemplars as holistic templates, learning the abstract style and logical structure of the solution.

However, LLMs are notoriously sensitive to prompt selection. The authors discover a major culprit behind this fragility: contextual distraction. Instead of absorbing the global logic, the model often fixates on specific, isolated numbers or words in the prompt examples and incorrectly injects them into the current problem.

Figure 1: An example of a Few-shot Chain-of-Thought demonstration distracting the reasoning of LLMs. Questions are collected from the GSM8K (Cobbe et al., 2021) dataset, and the responses are generated by Llama-3-8B-Instruct (AI@Meta, 2024).

As shown above, an exemplar discussing "quarters in a jar" causes the model to hallucinate the number "160" into a completely unrelated problem about baking cupcakes. Simply deleting these tokens isn't a viable solution, as it breaks the semantic integrity of the prompt.

Methods

To understand how this distraction happens, the authors first trace the internal information flow using attention saliency scores (the Hadamard product of attention weights and gradients). They observe that distracting tokens share a unique signature: they do not aggregate information from surrounding tokens in early layers (remaining semantically isolated), yet they directly channel their raw information to the prediction token in later layers.

To fix this without the massive computational overhead of calculating gradients at inference time, the authors propose Few-shot Attention Intervention (FAI):

  1. Identify Isolated Tokens: For every token $t_i$ in the demonstration, the model computes an aggregation coefficient $\alpha$ based on its average self-attention score. If a token has a high self-attention score, it means it hasn't integrated much context from other tokens.
  2. Apply a Dynamic Threshold: A token is flagged as "isolated" if its aggregation coefficient exceeds a position-dependent threshold: $$\tau = \frac{\lambda}{\text{index}_{t_i}}$$
  3. Intervene in Information Flow: In each layer, for any token flagged as isolated, FAI dynamically zeroes out its attention weight toward the current output token across all attention heads. This effectively blocks the isolated token from hijacking the prediction.
Figure 3: Overview of the proposed FAI method. In each layer, FAI identifies the positions that require intervention by analyzing the attention matrix from the demonstration. It then applies these interventions to the attention matrix of the subsequent layer, relative to the positions of the output tokens.

Datasets

The method was evaluated across several diverse reasoning benchmarks:

  • Mathematical Reasoning: GSM8K and AQuA.
  • Commonsense Reasoning: CSQA.
  • Algorithmic/Symbolic Reasoning: Big-Bench-Hard (Date Understanding, Sport Understanding) and Last Letter Concatenation.

Additionally, the authors constructed two targeted subsets from GSM8K to validate their findings: $\text{GSM}_{\text{good}}$ (questions answered consistently correctly regardless of the prompt) and $\text{GSM}_{\text{bad}}$ (questions highly prone to being derailed by prompt distractions).

Key Insights and Ideas

  • The "Isolated Token" Signature: Distracting tokens are characterized by high semantic isolation. Because they don't blend into the prompt's global context, their raw, un-aggregated values remain highly salient and easily corrupt the generation process.
  • The Retrieval Paradox: Common practice dictates using semantic similarity to retrieve the "best" few-shot examples. However, the authors find that retrieved exemplars often exert a stronger distracting effect than randomly selected ones, likely because their overlapping terminology makes it easier for the model to take things out of context.
  • Surgical vs. Blunt Intervention: Completely blocking all information flow from the prompt to the generation step (a contrastive baseline) stops the distractions but destroys the model's ability to follow the CoT format. FAI succeeds because it is surgical—intervening only on the ~15% of tokens identified as isolated.

Key Results

FAI consistently boosted performance across multiple model families (GPT-2, GPT-Neo, Llama-3, Mistral) and benchmarks:

  • AQuA: Achieved a remarkable 5.91% absolute accuracy improvement when applied to Llama-3-8B.
  • GSM8K: In a 6-shot setting with Llama-3-8B, FAI pushed accuracy from 71.65% to 75.21%.
  • Preserving CoT Behavior: FAI successfully suppressed distractions without degrading the Rate of Answer Following Rationale (RAFR). The model still generated clean, step-by-step reasoning chains rather than jumping straight to a broken answer.
Figure 4: (a) Comparison of the Overall Accuracy on GSMbad and GSMgood. (b) Comparison of the Rate of Answer Following Rationale on GSMbad and GSMgood.

Limitations and Future Work

The primary limitation acknowledged by the authors is model scale. Due to hardware constraints, experiments were restricted to models within the 7-billion to 13-billion parameter range. Whether massive frontier models (e.g., 70B+ parameters or proprietary models) naturally suffer less from this local token distraction—or if they would benefit even more from FAI—remains an open question for future research.

Summary updated

Abstract

Few-shot Chain-of-Thought (CoT) significantly enhances the reasoning capabilities of large language models (LLMs), functioning as a whole to guide these models in generating reasoning steps toward final answers. However, we observe that isolated segments, words, or tokens within CoT demonstrations can unexpectedly disrupt the generation process of LLMs. The model may overly concentrate on certain local information present in the demonstration, introducing irrelevant noise into the reasoning process and potentially leading to incorrect answers. In this paper, we investigate the underlying mechanism of CoT through dynamically tracing and manipulating the inner workings of LLMs at each output step, which demonstrates that tokens exhibiting specific attention characteristics are more likely to induce the model to take things out of context; these tokens directly attend to the hidden states tied with prediction, without substantial integration of non-local information. Building upon these insights, we propose a Few-shot Attention Intervention method (FAI) that dynamically analyzes the attention patterns of demonstrations to accurately identify these tokens and subsequently make targeted adjustments to the attention weights to effectively suppress their distracting effect on LLMs. Comprehensive experiments across multiple benchmarks demonstrate consistent improvements over baseline methods, with a remarkable 5.91% improvement on the AQuA dataset, further highlighting the effectiveness of FAI.
BibTeX
@inproceedings{yan2025don,
  author = {Shaotian Yan and Chen Shen and Wenxiao Wang and Liang Xie and Junjie Liu and Jieping Ye},
  title = {{Don't Take Things Out of Context: Attention Intervention for Enhancing Chain-of-Thought Reasoning in Large Language Models}},
  booktitle = {International Conference on Learning Representations},
  year = {2025},
  url = {https://openreview.net/forum?id=W6yIKliMot},
}
Figure