Inferensys

Glossary

Function Vector

A compact representation derived from in-context learning examples that, when added to the model's activations, can steer the model to perform a specific task without the original prompt.
Engineer reviewing vector database search results on laptop, embeddings visualization on screen, home office coding session.
IN-CONTEXT LEARNING STEERING

What is a Function Vector?

A function vector is a compact representation of a task derived from in-context learning examples that, when injected into a model's activations, steers its behavior to perform that task without the original prompt.

A function vector (FV) is a compact, causal representation of a specific input-output mapping extracted from a model's internal activations during in-context learning. It is computed by averaging the hidden state differences produced by a set of few-shot examples at a specific layer and token position. When this vector is added to the residual stream of a new query, it triggers the execution of the learned task—such as a specific translation or lexical operation—without requiring the original exemplars in the context window.

Function vectors are distinct from simple prompt embeddings because they capture the algorithmic operation itself, not the semantic content of the examples. Discovered through causal mediation analysis, they are typically localized to a specific range of middle MLP layers and act as a task-specific steering mechanism. This makes them a powerful tool for representation engineering and mechanistic interpretability, as they prove that in-context learning compresses task definitions into compact, causally potent activation patterns that can be isolated and transplanted.

MECHANISTIC INTERPRETABILITY

Key Characteristics of Function Vectors

Function vectors are compact representations derived from in-context learning demonstrations that, when injected into a model's forward pass, can steer its behavior to perform a specific task without the original prompt. They reveal how transformers encode task-level algorithms in their residual stream.

01

Causal Mediation of Task Execution

A function vector is computed by extracting the sum of attention head outputs from specific layers during in-context learning demonstrations. When this vector is added to the residual stream of a zero-shot prompt, it causally induces the model to perform the demonstrated task. This demonstrates that the model compresses the algorithm of the task—not just the input-output mapping—into a direction in activation space. The effect is robust across different prompts for the same task, proving the vector encodes a generalized procedure rather than a specific lexical pattern.

02

Extraction via Demonstration Pairs

Function vectors are derived by averaging the attention head outputs over a set of in-context learning demonstrations. The standard procedure involves:

  • Running the model on multiple input-output pairs for a task
  • Recording the summed output of attention heads at specific layers
  • Computing the mean vector across all demonstrations This mean vector, when added to a new query's residual stream, steers the model to apply the demonstrated function. The extraction typically targets middle-to-late layers where task-level abstractions are formed, not early layers that process surface-level syntax.
03

Task Generalization and Compositionality

Function vectors exhibit systematic generalization: a vector extracted for one instantiation of a task (e.g., translating English to French) transfers to novel inputs within the same task class. Furthermore, function vectors can be composed by adding vectors for different subtasks, enabling the model to execute multi-step procedures without explicit prompting. This compositional property suggests that the residual stream acts as a linear task space where independent algorithmic skills are represented as orthogonal directions that can be combined additively.

04

Distinction from Prompt Embeddings

Function vectors are fundamentally different from the input embeddings of the demonstration tokens. While prompt embeddings encode the literal text, function vectors encode the causal mechanism that the prompt induces. Evidence for this distinction:

  • Function vectors extracted from one set of demonstrations work on entirely different prompts for the same task
  • They are localized to specific attention heads, not distributed across the full embedding
  • Ablating the function vector after extraction removes the task behavior, even if the original prompt tokens remain This confirms that in-context learning creates a latent task representation separable from the surface form of the instructions.
05

Layer-Specific Localization

Function vectors are not uniformly distributed across all transformer layers. Research shows they are concentrated in a critical window of middle layers (typically layers 8-16 in medium-sized models). These layers correspond to where the model transitions from processing token-level syntax to forming high-level semantic abstractions. Attention heads in these layers act as task-encoding modules whose outputs, when summed, define the function. Earlier layers handle input formatting, while later layers execute the task using the injected vector as a steering signal.

06

Relationship to Induction Heads

Function vectors are mechanistically related to induction heads, the attention patterns that enable in-context copying. While induction heads implement the primitive operation of 'attend to the token after a previous occurrence,' function vectors represent the compiled task program that emerges from orchestrating multiple such primitives. The function vector can be understood as the residual stream state that configures downstream attention heads and MLP layers to execute the task, effectively acting as a learned 'function pointer' that the model's forward pass dereferences into behavior.

FUNCTION VECTORS EXPLAINED

Frequently Asked Questions

Clear, technical answers to the most common questions about function vectors—compact representations that steer language model behavior without explicit prompting.

A function vector is a compact, task-specific representation derived from in-context learning examples that, when added to a language model's residual stream activations, steers the model to perform a specific task without requiring the original few-shot prompt. The mechanism works by extracting the hidden state differences induced by processing input-output demonstration pairs, averaging these activation deltas across examples, and injecting the resulting vector into the model's forward pass. This effectively simulates the presence of the in-context demonstrations, causing the model to execute the task—such as a specific translation, arithmetic operation, or formatting rule—on novel inputs. The function vector captures the algorithmic essence of the task rather than the surface-level prompt tokens, making it a powerful tool for understanding how transformers encode procedural knowledge in their residual stream.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.