Foundation reference

In-Context Learning: Learning Without Weight Updates

In-context learning lets a language model take on a new task from a few examples in the prompt, without fine-tuning (Brown et al., 2020).

Concept 37 of 100RepresentationsStudy phase 5
Key equation
y^=arg⁡max⁡ypθ(y∣examples,xquery)\hat{y} = \arg\max_y p_\theta(y \mid \text{examples}, x_{\text{query}})
Reading map and next steps

Study guide

Where to start

Concept 37 of 100In-Context LearningRepresentations · Study phase 5: Representation & interpretability
Question

Few-shot gains grow with scale (Brown et al., 2020), yet small transformers can learn in context too (Garg et al., 2022; Olsson et al., 2022); what does scale add?

How to study this pagePredict · Check · Generalize · Continue
PredictSay what you expect.

Pick a concrete input and say what the equation should give before you read the math.

CheckCheck against the math and the papers.

Work through the math, then read the cited papers for the original argument.

GeneralizeName what stays the same.

Identify the part of the idea that survives a change of notation or model, so you can recognize it in another paper or system.

ContinueFollow the connections.

Use the prerequisites and related concepts at the end of the page to fill the next gap.

Why It Matters for Modern Models

  • In-context learning lets a language model take on a new task from a few examples in the prompt, without fine-tuning (Brown et al., 2020).
  • Changing behavior then means changing the prompt rather than the model, which makes it quick to try new tasks.
  • It explains why the choice, order and format of few-shot examples affect results, which is much of what prompt design is about.

Questions to Think About

  • What evidence links induction heads to in-context learning, and why does it fall short of showing that they are needed for every form of it?
  • When a model solves a task from demonstrations, how can we tell whether it learned the task from the examples or recognized one it had already seen in training?

Core Math

Key Equation
y^=arg⁡max⁡ypθ(y∣examples,xquery)\hat{y} = \arg\max_y p_\theta(y \mid \text{examples}, x_{\text{query}})

In-context learning is adapting to a task from examples given in the prompt, with no change to the weights.

Given demonstrations (x1,y1),…,(xk,yk)(x_1, y_1), \ldots, (x_k, y_k) and a query xk+1x_{k+1}:

y^k+1=arg⁡max⁡ypθ(y∣x1,y1,…,xk,yk,xk+1)\hat{y}_{k+1} = \arg\max_y p_\theta(y \mid x_1, y_1, \ldots, x_k, y_k, x_{k+1})

The parameters θ\theta stay fixed; the model only conditions on the demonstrations. Brown et al. (2020) found that the benefit of adding demonstrations grows with model size, but the ability itself does not require a large model: small transformers trained on suitable data learn in context too, for example fitting new linear functions from a few examples (Garg et al., 2022).

One proposed mechanism: implicit gradient descent. For linear regression, a single linear self-attention layer can compute the result of one gradient-descent step on the in-context examples (von Oswald et al., 2023):

Wupdated=W+η∑i=1k(yi−Wxi) xi⊤W_{\text{updated}} = W + \eta \sum_{i=1}^{k} (y_i - Wx_i)\,x_i^\top

This was shown for small models trained on regression tasks. Whether large pretrained language models learn in context this way is disputed (Shen et al., 2024).

Another: induction heads. These attention heads find an earlier occurrence of the current token and copy what followed it. Olsson et al. (2022) present evidence that they are a major mechanism of in-context learning, strongest for small attention-only models.

Key Papers

Language Models are Few-Shot Learners

Brown et al.2020NeurIPS
Read paper →

What Can Transformers Learn In-Context? A Case Study of Simple Function Classes

Garg et al.2022NeurIPS
Read paper →

In-Context Learning and Induction Heads

Olsson et al.2022Transformer Circuits Thread (Anthropic)
Read paper →

Transformers learn in-context by gradient descent

von Oswald et al.2023ICML
Read paper →

Connections

Where to go next

Chain of Thought builds directly on In-Context Learning. The other ideas that build on this page are listed under Leads to, above.

Chain of Thought