← Back to blog

Why function vectors can trigger in-context tasks

July 22, 2026 · 20 min read

Abstract

A narrow but revealing question about in-context learning is whether a model stores anything like a portable representation of the task currently being demonstrated. Todd et al. (2024) argue that a small set of attention heads carries a compact residual-stream vector that represents the demonstrated input-output mapping. If that vector is extracted from a few-shot prompt and added back into a different context, the model can often execute the task even when the surrounding prompt no longer contains usable demonstrations. My view is that this result matters because it sharpens the mechanistic story of in-context learning. The prompt is not only a place from which the model copies local patterns; it can also induce a task-level state that gets transported forward by specific heads. That makes function vectors a concrete bridge between induction-style copying stories and richer views of prompt-conditioned computation.

Related Work

The primary source is Todd et al., Function Vectors in Large Language Models (ICLR 2024). The paper studies decoder-only models including GPT-J, GPT-NeoX, and Llama 2, then uses causal mediation analysis to isolate attention heads whose outputs are especially important for carrying task information during few-shot prompting. Summing the task-conditioned outputs of those heads yields a function vector, or FV.

A useful precursor is Akyurek et al. (2022), which asks what algorithm in-context learning resembles on linear tasks and argues that transformers can implement update rules akin to learning algorithms in their forward pass. A second precursor is von Oswald et al. (2023), which shows that transformers trained on regression can behave like gradient descent. Those papers are more algorithmic than mechanistic. They suggest what computation may be happening, while the FV paper asks where in a real language model a portable task representation seems to live.

A nearby but distinct line is activation steering, especially Turner et al. (2023) on Activation Addition. That work shows that adding activation-space directions can steer topic, sentiment, or truthfulness. Function vectors differ in a crucial way: they are not generic style directions but task-conditioned vectors extracted from demonstrations of an input-output function.

Method/Mechanism

The method starts with a deliberately adversarial setup. Instead of evaluating a model only on helpful few-shot prompts, Todd et al. create corrupted prompts where the demonstrations keep the same surface format but the labels are shuffled, so the examples no longer specify a coherent task. They then ask which attention heads matter for recovering the correct answer when clean activations from the original task prompt are patched into that corrupted run. This gives each head an average indirect effect: a causal score for how much it helps transmit task-relevant information.

Once the high-effect heads are identified, the authors average those heads' outputs over prompts from the same task and sum them to form a single vector in residual-stream space. That vector is the function vector. At inference time, they inject it at the final token position in a new prompt, usually at an early-middle or middle layer. If the model starts producing antonyms, translations, or labels despite receiving a zero-shot or shuffled-label prompt, the intervention is taken as evidence that the vector is carrying task information rather than just memorized lexical content.

Two details make the mechanism more credible than naive vector arithmetic. First, the effect peaks in middle layers and collapses when the same vector is inserted late in the network. If the FV were just a direct vocabulary-space offset, late injection should work too. Second, the paper shows that the vector often contains output-space hints, but those hints alone do not reconstruct the full effect. The model appears to treat the FV as an instruction that triggers downstream computation, not as a simple logit bump for a set of answer words.

Key Findings

Two case studies make the claim concrete:

Five crisp insights follow:

The last point is the most interesting. The paper reports that some influential heads score highly on induction-style prefix matching, but several other high-effect heads do not. That is evidence against a reductionist view where all few-shot competence is just sophisticated copying. Function vectors look more like a compositional control state assembled from several head outputs, some copy-like and some not.

Limitations

The main limitation is task simplicity. Many demonstrations involve word-level transformations, classification labels, or compact mappings. Those are ideal for extracting a clean causal vector, but they may understate how entangled open-ended tasks are. If a task requires long-range planning, multi-step reasoning, or heavy world knowledge, a single compact FV may be an incomplete summary.

There is also a filtering issue in the evaluation. The paper often measures FV effects on prompts that the base model can already solve with ordinary 10-shot ICL. That is a reasonable way to ask how much of the model's existing competence is captured by the extracted vector, but it means the intervention is not creating new capabilities. Finally, causal localization is not the same as a full explanation. Knowing which heads transport the signal does not yet tell us the full downstream program that interprets it.

Future Directions

One direction is scale and task complexity. Do function vectors stay compact for code generation, multi-hop QA, or agentic tool use, or does the representation fragment across more heads and layers as the task becomes less like a short symbolic mapping? Another direction is alignment: if task identity can be extracted and reinserted as a residual-stream vector, then prompt injection and activation steering may share more structure than they first appear to. That could matter both for understanding jailbreaks and for designing more modular control mechanisms.

A third direction is composition. Todd et al. report partial success when summing function vectors to induce more complex behavior. If that scales, it would suggest that some task abstractions are represented in an algebraically reusable way rather than as brittle prompt-specific traces.

Open question: when function vectors fail on richer language tasks, is the bottleneck that the task cannot be compressed into one portable vector, or that the downstream circuits needed to interpret that vector are too distributed to be triggered by a single intervention?

Summary

Function vectors are compelling because they turn a vague intuition about "the prompt tells the model what task to do" into a concrete causal claim. Todd et al. show that a small set of attention heads can carry a portable task representation that survives transfer into zero-shot or corrupted contexts. The effect is strongest in middle layers, weaker in late layers, and not reducible to simple vocabulary biasing. That does not solve the whole puzzle of in-context learning, but it does identify one sharp mechanism: demonstrations can induce a compact task state that the model later executes.

References