NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

In-Context Learning Creates Task Vectors

Roee Hendel, Mor Geva, Amir Globerson

Abstract

In-context learning (ICL) allows large language models (LLMs) to perform new tasks by simply providing a few examples in the prompt. While highly effective, the internal mechanisms of ICL have remained largely opaque. This paper investigates the hypothesis that LLMs implement ICL by compressing demonstration examples into a compact, intermediate representation called a "task vector." By isolating and "patching" these vectors, researchers demonstrate that the learning of a task is decoupled from its execution, providing a new lens through which to understand and manipulate model behavior.

Introduction

One of the most remarkable emergent properties of modern Large Language Models is In-Context Learning (ICL). Unlike traditional machine learning, which requires explicit parameter updates (fine-tuning) to learn a new task, ICL enables a model to grasp a rule—such as translating English to French or converting singular nouns to plural—from just a handful of examples provided in a single forward pass.

Despite its ubiquity, the internal logic of ICL has been a "black box." In standard supervised learning, we use a training set to find a function $f$ with parameters $\theta$. In ICL, the model appears to perform both the "learning" of $\theta$ and the application of $f$ simultaneously. This research explores whether the model internally follows a similar two-step logic: first converting demonstrations into a discrete internal state (a task vector) and then using that state to process new queries.

The Task Vector Hypothesis

The researchers propose a "Hypothesis Class" view of ICL, modeling the process as two distinct stages:

  1. The Learning Algorithm ($\mathcal{A}$): The initial layers of the model (up to layer $L$) process the demonstration examples ($S$) and a separator token. This process produces a task vector ($\theta$), which is extracted from the hidden state of the final token in the demonstration sequence.
  2. The Rule Application ($f$): This vector $\theta$ is then used to modulate the remaining layers of the network.

To test this, the authors employed a "patching" methodology. They extracted the task vector from a forward pass containing demonstrations and injected it into a new, separate forward pass that contained only the query ($x$) and no demonstrations. If the model could still perform the task correctly using only the injected vector, it would prove that the vector successfully captured the task's essence.

Experimental Methodology

The study evaluated this hypothesis across several model families, including LLaMA (ranging from 7B to 30B parameters), GPT-J, and Pythia. The researchers utilized 18 diverse tasks across four categories:

  • Algorithmic: e.g., "Next letter" (a $\rightarrow$ b).
  • Translation: e.g., French to English.
  • Linguistic: e.g., Antonyms or tense shifts.
  • Knowledge-based: e.g., Mapping countries to their capital cities.

These tasks were specifically chosen to produce single-token outputs, allowing for precise measurement of the task vector's effectiveness.

Key Insights

The "Sweet Spot" Layer

The effectiveness of task vectors is highly dependent on the layer from which they are extracted. The researchers found a specific intermediate "sweet spot" where the task representation is most potent. For LLaMA-7B, this occurs around layer 15. Extracting the vector too early (before the task is "learned") or too late (when the model is already focused on specific output tokens) results in a significant drop in performance.

Query Independence and Robustness

A striking finding was the robustness of these vectors. A task vector generated using one set of examples and a "dummy" query (e.g., using the word "Plum" as a placeholder) remains effective when applied to an entirely different query (e.g., "Corn"). This suggests the vector encodes the rule of the task rather than the specific context of the examples.

Semantic Encoding and the Logit Lens

By using the "logit lens" technique—projecting intermediate hidden states back into the model's vocabulary space—the authors discovered that these vectors carry semantic meaning. For instance, a vector extracted from a translation task often points toward tokens like "English" or "translate," even if those specific words never appeared in the prompt. This indicates that the model is mapping the ICL examples to known concepts within its latent space.

Dominance over Context

In "conflict" experiments, the researchers provided a text prompt for Task A (e.g., translation) but injected a task vector for Task B (e.g., antonyms). In these cases, the model consistently performed Task B. This demonstrates that the internal task vector is the primary driver of behavior, capable of overriding the literal text of the provided demonstrations.

Results

The "Hypothesis" method—relying solely on the patched task vector—maintained 80-90% of the accuracy of standard ICL across the tested models. In contrast, a baseline model (given the query without the vector) achieved only 10-20% accuracy. Furthermore, t-SNE visualizations revealed that task vectors for the same task cluster together tightly, regardless of the specific examples used to generate them, while remaining distinct from vectors of other tasks.

Discussion and Limitations

While this research provides a breakthrough in understanding the mechanics of ICL, several questions remain:

  • Complexity: The study focused on simple, single-token tasks. It remains to be seen if complex reasoning, such as Chain-of-Thought prompting, can be compressed into a single vector.
  • Mechanistic Circuitry: While the paper proves task vectors exist, the specific attention heads and MLP layers responsible for constructing these vectors from demonstrations are still being mapped.
  • Practical Utility: This discovery opens the door to "vector-based" prompting, where models could be adapted to new tasks by injecting pre-computed vectors, potentially saving significant context window space and computational costs.

Conclusion

By demonstrating that In-Context Learning creates portable, robust task vectors, this study shifts our understanding of LLMs from black-box predictors to systems that dynamically generate and apply internal "programs." This separation of learning and application provides a foundation for more efficient and controllable AI systems.

Summary updated

BibTeX
@inproceedings{hendel2023context,
  author = {Roee Hendel and Mor Geva and Amir Globerson},
  title = {{In-Context Learning Creates Task Vectors}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2023},
  doi = {10.18653/v1/2023.findings-emnlp.624},
}
Figure