Towards Generalizable Implicit In-Context Learning with Attention Routing

Jiaqian Li, Yanshu Li, Ligong Han, Ruixiang Tang, Wenya Wang · DOI

Abstract

In-Context Routing (ICR) addresses the efficiency and brittleness "taxes" of standard in-context learning (ICL) by internalizing reasoning patterns at the structural level of attention logits. Unlike previous "implicit ICL" methods that use additive task vectors, ICR employs a lightweight router to dynamically bias attention dynamics based on Principal ICL Directions (PIDs) extracted from diverse tasks. Our evaluation across 12 datasets shows that ICR consistently outperforms existing baselines and even explicit few-shot prompting in several out-of-domain scenarios, all while maintaining zero-shot inference speeds.

Introduction

In-context learning (ICL) is the standard paradigm for adapting Large Language Models (LLMs) to new tasks without retraining. However, it suffers from an Efficiency Tax, where example tokens increase latency and compute costs, and a Brittleness Tax, where performance varies significantly based on example order and formatting.

Recent "implicit ICL" research attempts to mitigate these issues by compressing examples into "task vectors" injected into internal layers. While efficient, these methods often struggle to generalize, merely mimicking task-specific outputs rather than internalizing the underlying mechanisms of context utilization. Consequently, they frequently fail on out-of-domain (OOD) tasks.

Methodology

The proposed In-Context Routing (ICR) framework shifts the intervention from the residual stream to the attention logits, fundamentally changing the model's matching geometry. The process consists of three core stages:

  1. Principal ICL Direction (PID) Extraction: Diverse tasks are processed with explicit examples to collect Query ($Q$) and Key ($K$) projections. Principal Component Analysis (PCA) identifies shared subspaces representing universal context-processing patterns.
  2. Query-Conditioned Router: A lightweight MLP router analyzes zero-shot inputs to dynamically determine which PIDs to amplify and which attention heads to engage.
  3. Attention Routing: During inference, the router applies a low-rank bias to attention logits, steering the model's dynamics toward the generalizable patterns identified during extraction.
Overview of the ICR framework, illustrating the PID extraction process and the query-conditioned router training.

Crucially, the LLM parameters remain frozen; only the minimal router is trained, ensuring high efficiency.

Experimental Setup

ICR was evaluated using Llama2-7B, Qwen2.5-7B, and Llama3.1-8B across 12 datasets categorized by their relationship to the training data:

  • In-Domain (ID): AGNews, SST-2, TREC, CSQA, and PIQA.
  • Near Out-of-Domain (OOD): SST-5, MR, and MRPC.
  • Far Out-of-Domain (OOD): CB, COPA, CREAK, and AI2SciE.

Key Results and Insights

ICR demonstrates superior generalization capabilities compared to prior implicit ICL methods like I2CL and LIVE. As shown in the results table, ICR consistently achieves the highest overall accuracy across different model architectures.

Performance comparison of ICR against baseline methods across multiple datasets for Llama2-7B and Qwen2.5-7B.

Core Findings:

  • Internalization vs. Mimicry: By modifying attention logits, ICR taps into a universal "reasoning mode." Analysis shows internal probability shifts toward tokens associated with structural reasoning (e.g., "connections," "signs") rather than just task labels.
  • Superior Generalization: On Llama2-7B, ICR improved average performance by 3.0% over the best baseline; on Qwen2.5-7B, the improvement reached 6.5%. In several OOD cases, ICR even outperformed explicit few-shot prompting by providing a "cleaned" version of the learning pattern.
  • Hub Layers: ICL logic is concentrated in specific "hub layers" (e.g., layers 23 and 26 in Llama2-7B), which act as stable anchors for generalization.
  • Robustness: Unlike baseline methods that occasionally collapse on OOD tasks, ICR remained robust across all tested scenarios, maintaining zero-shot inference speeds regardless of sequence length.

Conclusion and Future Work

ICR provides a robust, efficient path toward generalizable implicit ICL. While currently focused on the final third of LLM layers and requiring an initial PID collection phase, future research could explore multi-layer interactions and cross-model routing, where patterns from larger models steer smaller ones.

Summary updated

BibTeX
@misc{li2025towards,
  author = {Jiaqian Li and Yanshu Li and Ligong Han and Ruixiang Tang and Wenya Wang},
  title = {{Towards Generalizable Implicit In-Context Learning with Attention Routing}},
  year = {2025},
  eprint = {2509.22854},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2509.22854},
}
Figure