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.
Glossary
Function Vector

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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
Related Terms
Function vectors are a key concept in the broader field of mechanistic interpretability. Explore these related terms to understand the full ecosystem of techniques used to decode transformer computations.

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.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us