The Atlas of In-Context Learning: How Attention Heads Shape In-Context Retrieval Augmentation

Patrick Kahardipraja, Reduan Achtibat, Thomas Wiegand, Wojciech Samek, Sebastian Lapuschkin

Introduction

Large Language Models (LLMs) increasingly rely on Retrieval-Augmented Generation (RAG) to ground their outputs in external knowledge and mitigate hallucinations. Despite their success, the internal mechanisms governing how a model prioritizes provided context over its own pre-trained (parametric) memory remain poorly understood. This research investigates the transformer architecture to mechanistically locate, categorize, and manipulate the specific attention heads responsible for in-context learning and knowledge retrieval.

Methodology: Identifying Specialized Heads

To analyze models like Llama-3.1-8B, Mistral-7B, and Gemma-2-9B, the authors utilized AttnLRP (Attention-aware Layer-wise Relevance Propagation). Unlike simple attention weights, AttnLRP traces the causal influence of input tokens on the final output through backpropagation.

The study categorizes attention heads into three primary types:

  1. Parametric Heads: Active in "closed-book" settings, these heads recall information from the model's internal training data.
  2. Task Heads: A subset of in-context heads that focus on instructions or the structure of the question.
  3. Retrieval Heads: In-context heads that specifically extract answer tokens from the provided text.

The Functional Geography of Attention

The research reveals a highly structured functional map across transformer layers. Early layers typically enrich prompts with parametric knowledge. Middle layers are dominated by task heads that process instructions, while deep layers contain retrieval heads that perform the final extraction of answers from the context.

Mapping specialized attention heads across transformer layers.

This "geography" suggests that in-context learning is not a monolithic process but a sequence of specialized operations. For instance, task heads encode the intensional frame (e.g., identifying that a question asks for an "occupation"), while retrieval heads act as "copy-pasters" to fill that frame with contextual data.

Causal Validation and Results

To confirm the functional roles of these heads, the authors performed ablation studies and activation patching. Ablating (turning off) just 20 identified in-context heads resulted in a significant drop in open-book QA recall—up to 63.8%—demonstrating their causal necessity.

Impact of head ablation on model recall across different datasets.

Furthermore, the authors demonstrated that task concepts are transferable. By extracting "function vectors" from task heads in one prompt and patching them into another, they could induce specific behaviors (like extracting an occupation) even when the target prompt contained no explicit question.

Source Tracking and Hallucination Mitigation

A significant practical outcome of this work is the development of a source-tracking probe. By training a linear probe on retrieval head activations, the researchers could distinguish whether an answer originated from the provided context or parametric memory with an ROC AUC of 94–98%. This probe can also localize the exact source token in the text with high accuracy (84–97%).

Conclusion

This study provides a mechanistic "atlas" of how LLMs process information in RAG pipelines. By identifying the specific heads that drive retrieval and instruction following, the researchers have opened new avenues for controlling model behavior. Future work may involve using these insights to build dynamic "knobs" that force models to prioritize external context, thereby reducing hallucinations and increasing the transparency of AI systems.

Summary updated

BibTeX
@inproceedings{kahardipraja2025atlas,
  author = {Patrick Kahardipraja and Reduan Achtibat and Thomas Wiegand and Wojciech Samek and Sebastian Lapuschkin},
  title = {{The Atlas of In-Context Learning: How Attention Heads Shape In-Context Retrieval Augmentation}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2025},
  url = {https://openreview.net/forum?id=5h9mS87Pyt},
}
Figure