← Explainer Library

Interactive Explainer

In-Context Learning, From the Inside

A frozen LLM learns a new task from a few examples in its prompt. No weight updates. Why? The mechanistic answer is the induction head: a two-step attention circuit that, once trained, can copy "patterns seen earlier in the context" almost arbitrarily.

Step 1

What ICL is, operationally

You pass a frozen model a prompt like:

apple → red
banana → yellow
broccoli → green
strawberry →

and the model outputs red. No fine-tuning, no gradient steps. The accuracy on the task usually grows with the number of in-context examples $k$, then plateaus. Empirically this works for translation, arithmetic, named entity recognition, function approximation, even small reasoning tasks.

Two facts that motivated the research:

  1. ICL emerges with scale. GPT-2 (1.5B) does it poorly; GPT-3 (175B) does it well. Below a critical model size and training-data scale, ICL accuracy is at chance.
  2. ICL works on tasks that don't appear in pretraining. The model isn't retrieving memorised answers; it's running something that functions like an algorithm at inference.
Step 2

The induction head — a 2-layer attention circuit

Olsson et al. (2022) localised the mechanism behind the simplest form of ICL — copying. It needs two attention heads working together:

That two-step circuit implements: "find the last time we saw this token; copy what came next." On a sequence X A Y B A ? it predicts Y. Combined with the attention article's mechanics, this is one short script.

Step 3

Watch it work — k-shot copy task, live

A toy task: each prompt has $k$ (key, value) pairs followed by a query key. The correct answer is the value of the pair whose key equals the query. The model is a 2-layer attention network with 4 heads pre-trained to do well at copy tasks. Slide $k$; watch accuracy.

accuracy
Sample prompt + model prediction
Layer 2 attention from the query token (the induction head in action)
k-shot accuracy vs k
Step 4

"ICL is implicit gradient descent"

A surprising line of work (Akyürek et al. 2023; von Oswald et al. 2022; Garg et al. 2022) shows that a single Transformer layer applied to a sequence of $(x_i, y_i)$ pairs can implement a step of gradient descent on linear regression. With enough layers, ICL approximately equals "fit a small model to the prompt's examples, then predict."

The construction: pack $(x, y)$ pairs into tokens; the attention layer's $W_Q, W_K, W_V$ can be chosen so the output at the query position equals the prediction of one step of GD on the data so far. Layer-by-layer compounds into multiple GD steps. This explains both why ICL needs scale (enough layers to do enough steps) and why it generalises to held-out function classes.

Step 5

Practical implications

The slogan. ICL is not magic: it's a circuit (induction heads) inside a model big enough to have learned it, doing what looks like gradient descent over your prompt. Once you see the circuit, you stop being surprised that prompts matter so much.