When we provide a Large Language Model (LLM) with a few examples of a task—such as translating words or reversing strings—it performs In-Context Learning (ICL). Traditionally, we view this as the model dynamically attending to all previous tokens to figure out what to do next. However, this process is computationally expensive, sensitive to prompt formatting, and token-heavy.
A growing body of research reveals a surprising alternative mechanism: the transformer compresses the entire abstract logic of a task into a single, compact mathematical vector within its internal activations. Rather than re-evaluating the examples at every step, the model uses the context to generate a portable macro, or a "task vector," which steers its subsequent behavior.
The Discovery of Task Vectors
How do we know these vectors exist? As shown by Mor Geva et al. in In-Context Learning Creates Task Vectors, if you feed a few-shot prompt into an LLM, the model constructs a unified internal representation at the intermediate layers. By extracting this vector from the final token position of the demonstrations and "patching" (injecting) it into a completely new, zero-shot query, the model can execute the task with high accuracy without ever seeing the original examples.
Similarly, Eric Todd et al. demonstrated in Function Vectors in Large Language Models that these internal activation vectors can elevate zero-shot accuracy from under 10% to over 80%. This suggests that the vector itself encapsulates the logic of a specific transformation or mapping, acting as a virtual parameter set that bypasses the need for explicit context tokens.
How Task Vectors Are Formed and Extracted
To understand why this works, we must look at how these vectors are isolated. A raw activation vector from a single prompt contains both the abstract task logic and the specific noise of the tokens used in that prompt. To distill the pure task representation, researchers use an averaging technique across multiple distinct sets of few-shot examples for the same underlying task.
[Prompt 1: Examples of Translation] ---> Extract Final Token Activation \
[Prompt 2: Examples of Translation] ---> Extract Final Token Activation ---> Average ---> Pure Task Vector
[Prompt 3: Examples of Translation] ---> Extract Final Token Activation /
As described by Zhuowei Li et al. in Implicit In-context Learning, extracting and averaging these internal activations from attention and feed-forward modules yields a fixed-size context vector that captures the essence of a task. This vector serves as a neural fingerprint of the task's requirements (Guy Davidson et al., Do different prompting methods yield a common task representation in language models?).
To scale this up to "many-shot" scenarios without blowing up the context window, Dongfang Li et al. in In-Context Learning State Vector with Inner and Momentum Optimization proposed optimizing these state vectors using momentum methods, treating them effectively like virtual model parameters that capture the logic of numerous demonstrations.
Localization in Attention Circuits
These vectors are not uniformly distributed across the model; instead, they are highly localized within specific attention circuits. Kayo Yin and Jacob Steinhardt in Which Attention Heads Matter for In-Context Learning? discovered that specialized "Function Vector heads" drive few-shot accuracy. Interestingly, many of these heads begin their lives as simple induction heads during training before evolving into more complex mechanisms that encode abstract task representations.
When these specific heads are isolated, their activations can be used as a continuous nudge to the model's internal state. For example, Théo Lasnier et al. identified in Translation Heads: Disentangling meaning from language in LLM-based machine translation that machine translation is mechanistically localized in roughly 1% of attention heads. Averaging their activations creates a steering vector that triggers high-quality translation without any explicit textual instructions or examples.
This aligns with the findings of Haolin Yang et al. in Localizing Task Recognition and Task Learning in In-Context Learning via Attention Head Analysis, showing that activation steering via task vectors effectively replicates few-shot performance by directly injecting the required functional logic into the hidden states of a zero-shot prompt.
Cross-Modal Universality
One of the most profound insights of this research direction is that task vectors are not unique to text. They represent a fundamental property of transformer architectures across modalities. Alberto Hojel et al. demonstrated in Finding Visual Task Vectors that vision transformers also develop internal function vectors capable of steering image-to-image transformations.
Furthermore, in Vision-Language Models (VLMs), these representations appear to be modality-invariant. Grace Luo et al. showed in Task Vectors are Cross-Modal and Vision-Language Models Create Cross-Modal Task Representations that a task vector extracted from a text prompt can be patched into a visual prompt (and vice versa) to guide the model. This mechanism bypasses the common failure mode where a VLM simply describes an image instead of performing the intended task on it.
To leverage this for long contexts, Brandon Huang et al. in Multimodal Task Vectors Enable Many-Shot Multimodal In-Context Learning compressed hundreds of token-expensive image examples into a single vector by averaging the activations of targeted attention heads. This allows the model to leverage information from hundreds of shots without exceeding context window limits or incurring massive computational costs.
Why It Works: Geometry and Bayesian Inference
What is the underlying mathematical reason that transformers allow this? Haolin Yang et al. provided a geometric explanation in Unifying Attention Heads and Task Vectors via Hidden State Geometry in In-Context Learning. They show that ICL operates as a two-stage geometric process: early layers separate hidden states by label, while later layers align those states with output vectors. Task vectors act as directional nudges that correct the alignment of these internal representations, fixing the issues that plague zero-shot performance.
From a statistical perspective, Yufeng Zhang et al. argued in What and How does In-Context Learning Learn? Bayesian Model Averaging, Parameterization, and Generalization that transformers act as Bayesian statisticians. The attention mechanism computes implicit Bayesian Model Averaging (BMA) over hidden "concepts" from prompt examples to predict the next token. The task vector is the physical manifestation of this estimated concept vector, stored within the attention heads.
As summarized by Haitao Mao et al. in A Survey to Recent Progress Towards Understanding In-Context Learning, these vectors serve as internal triggers for specific pre-existing skills or newly learned functions. By understanding and manipulating this latent language, we can steer transformers directly, bypassing the noise and overhead of traditional prompting.
Key Foundational Work
Function Vectors in Large Language Models (2023) — contributed 2 concept elements to this direction.
Task Vectors are Cross-Modal (2024) — contributed 2 concept elements to this direction.
Multimodal Task Vectors Enable Many-Shot Multimodal In-Context Learning (2024) — contributed 2 concept elements to this direction.