arXiv:2610.01054cs.CLOct 2026

Manuscript Offprint

arXiv:2610.01054

Capturing In-Context Learning Dynamics with Task Operators

Guangzhi Xiong, Zhenghao He, Bohan Liu, Sanchit Sinha, Wenqian Ye, Aidong Zhang

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

Guangzhi Xiong

University of Virginia

guangzhi@virginia.edu

Sanchit Sinha

University of Virginia

sanchit@virginia.edu

Zhenghao He

University of Virginia

zhenghao@virginia.edu

Wenqian Ye

University of Virginia

wenqian@virginia.edu

Bohan Liu

University of Virginia

bohan@virginia.edu

Aidong Zhang

University of Virginia

aidong@virginia.edu

Abstract

In-context learning (ICL) enables language models to perform new tasks from demonstrations without weight updates. However, every ICL inference requires processing the full set of examples, resulting in inefficient deployments, and how ICL works mechanistically is not fully understood. Prior work compresses ICL into fixed activation vectors extracted from specific layers or positions, but these input-independent interventions fail on complex tasks where the output depends on fine-grained interactions with the input. By analyzing the ICL forward pass, we show that each attention head's output is an affine transformation of its context-masked counterpart, and that the parameters of this transformation are empirically stable across samples for a given task. Building on this, we introduce Task Operator (TO), which replays this transformation as an analytically derived update to the attention output projection. Across lexical, algorithmic, and reasoning tasks, TO achieves the best overall performance among prior methods and substantially narrows the gap between zero-shot inference and ICL. We further show that the extracted knowledge concentrates in a task-specific sparse circuit across layers and positions, and that averaging operators from disjoint demonstration batches enables effective many-shot scaling without expanding the context window. Our code is available at https://github.com/gzxiong/task_operator.

1 Introduction

In-context learning (ICL) enables large language models (LLMs) to quickly adapt to new tasks by providing input-output demonstrations in the context [2]. This allows LLMs to improve performance on tasks where they fail in the zero-shot learning (ZSL) setting, without any additional training. However, ICL requires processing all demonstrations for the inference of each new input, which can be computationally expensive and inefficient even with KV caching [26]. Therefore, it is important to understand the mechanisms underlying ICL and to develop methods that can distill the ICL representation into a compact form, enabling efficient replay at inference time without repeatedly processing demonstrations.

Recent approaches tackle this inefficiency by developing methods that compress ICL into lightweight, fixed representations, allowing models to bypass demonstrations and efficiently replay task knowledge during inference [13, 33, 21, 27, 18]. These methods typically extract task-specific activation vectors from particular layers or token positions during ICL and inject them as fixed interventions during zero-shot inference. While effective for simple tasks with clear input-output mappings, these approaches

Preprint.

fail when output generation requires complex multi-step reasoning over the input content, indicating that the activation vectors do not faithfully capture the knowledge encoded in the ICL process.

By analyzing how demonstrations reshape the model’s internal computation, we show that at every attention head, the output under ICL is precisely an affine transformation of its context-masked counterpart, with the parameters of these transformations showing high consistency across samples (Appendix D). With extracted parameters for all attention heads, we show that the effects of demonstrations can be cleanly represented as a parameter update to the attention output projection, establishing a mechanistic link between ICL and parameter fine-tuning.

Building on this analysis, we introduce the Task Operator (TO), a training-free method that captures ICL knowledge as affine transformation parameters and replays it during inference to dynamically update the attention outputs. Through experiments on four LLMs across eight tasks, we demonstrate that TO faithfully captures ICL knowledge and consistently outperforms prior knowledge extraction methods when replaying the knowledge in zero-shot inference. Causal circuit analysis and ablation studies reveal that the key sites for storing ICL knowledge are sparse and task-specific, explaining why previous methods that extract knowledge from fixed locations fall short. Scaling analysis further demonstrates the effectiveness of TO under many-shot ICL, where knowledge is averaged from disjoint mini-batches of demonstrations, highlighting its scalability within a fixed context budget. Our main contributions are:

• We propose Task Operator (TO), a training-free method that captures ICL knowledge as an affine transformation of the attention output, enabling efficient and dynamic replay of ICL knowledge during zero-shot inference.

• Evaluation across four LLMs and eight tasks demonstrates that TO consistently outperforms prior methods, highlighting the importance of dynamic interventions for faithfully capturing and replaying ICL knowledge.

• Our analysis reveals the diverse and sparse circuits leveraged by ICL across different tasks, and shows that TO enables efficient many-shot scaling by averaging knowledge extracted from disjoint demonstration batches.

References (36)