All courses

Explain in-context learning

Explain how a frozen model adapts to examples in its prompt, both mechanistically (induction-style attention circuits) and probabilistically (inferring the task). Predict when few-shot prompting helps and what it is sensitive to.

Builds on: Induction heads, In-context learning as implicit task inference

▶ Watch this lesson Start the whole course

What the animation says

Learning with frozen weights

Show a base model three examples of a made-up format. Each line is a word, an arrow, then the same word in capitals between stars. Give it a fourth word, and it continues the pattern. Now compare the weights before and after. Identical. Not one number changed. So where did the learning happen?

The copying circuit

The answer: in the activations. Same weights, but a different prompt means a different computation. Attention lets the last position read every earlier token. Some heads, called induction heads, look for spots like the current one: right after an input and its arrow. Then they copy what followed there, the star-and-capitals format, into the query's residual stream. They match abstract patterns, not just exact tokens. Fox never appeared, but the slot did. Your turn. Tap an earlier spot that this head attends to.

Examples as evidence

Here's a second view of the same thing. A base model predicts how a document continues. So the examples are evidence about which task produced this document. Each candidate task starts with a prior. Each example multiplies in how well it fits that task. The answer averages over tasks, weighted by that belief. One view says how the computation runs. This one says what it achieves. Which prompt helps more: consistent format with one wrong label, or correct labels in a messy format?

What the two views predict

Consistent format wins. Task inference keys on format and label space, more than on every label being right. Copying circuits add their own biases. Labels that are frequent, or come last, get a boost. And examples that look like the query pull hardest. Few-shot helps most when the task is ambiguous. Once one task dominates, more examples add little. And it only locates skills the model already has, plus new specifics like an invented word's label. It can't create a skill.

Flipped labels: setup

Now a test. Sentiment examples with flipped labels: happy sentences marked negative, and the other way round. Hypothesis one, normal sentiment: prior one half. A flipped example looks like noise, likelihood point one. Hypothesis two, reversed sentiment: prior one in a thousand. But a flipped example fits it, likelihood point four. So the odds of reversed versus normal start at one to five hundred. Each flipped example multiplies them by four. After three flipped examples, which does the model follow?

The tipping point

After three: four times four times four is sixty-four. Sixty-four over five hundred is about point one three. Normal still wins. Keep adding flipped examples. After five: one thousand twenty-four over five hundred, about two. Now the flip wins. The weights never moved. Evidence simply piled up against a tiny prior. So a frozen model adapts by reading its prompt. Attention copies patterns, and the examples select a task it already knows. Watch format, label balance, order and similarity. Few-shot locates skills. It doesn't create them.

Learning with frozen weights?

Show a base model three examples of a made-up format, and it continues the pattern on a fourth input. No weight changed: $\theta$ is identical before and after. So where did the "learning" happen? Why does the model sometimes ignore your examples? When will adding more examples actually help? A working answer lets you predict few-shot behavior instead of finding it by trial and error.

Mechanism and purpose

The adaptation lives in activations. Attention lets later positions read the examples and writes the result into the residual stream at the query. **Mechanistically**, induction-style heads generalize from exact tokens to abstract matches: "this spot is like the earlier spots right after an input" → copy what followed there (a label, a format). **Probabilistically**, the examples are evidence about which task produced this document. The two views are complementary: one describes how the computation runs, the other what it achieves.

What the two views predict

Copying circuits favor what is **frequent and recent**. A label that dominates the examples, or comes last, gets a boost. They also match on **surface similarity**, so examples resembling the query pull hardest. Task inference keys on **format and label space**, so consistent formatting matters more than whether every example's label is correct. Inference only *selects* among tasks learned in pretraining, and copying only reuses what the prompt shows. Few-shot can locate an existing skill and supply new specifics, such as an invented word's label. It cannot create a skill the model lacks.

ICL as a posterior over tasks

$$p_\theta(y\mid e_{1..n},q)\approx\sum_c p(y\mid q,c)\,P(c\mid e_{1..n})$$ $$P(c\mid e_{1..n})\propto P(c)\prod_{i=1}^n p(e_i\mid c)$$ - $c$: a latent task or format - $e_i$: example $i$, treated as independent given $c$ - $q$: the query input - $y$: the answer Gains are biggest when the prior $P(c)$ is spread out, as when the task is ambiguous or there is no clear instruction. Each example multiplies in a likelihood, so returns diminish once one $c$ dominates.

Flipped labels: when do they win?

Sentiment examples with *flipped* labels. Two hypotheses: - $c_1$ = normal sentiment, prior 0.5. A flipped example has likelihood 0.1 (treated as noise). - $c_2$ = reversed sentiment, prior 0.001. A flipped example has likelihood 0.4. Prior odds $c_2{:}c_1=0.001/0.5=1/500$. Each example multiplies the odds by $0.4/0.1=4$. - After 3 examples: $64/500\approx0.13$, so the model still answers *normal* sentiment. - After 5 examples: $1024/500\approx2$, so it follows the flip.

Remember this

A frozen model adapts by reading its prompt. Mechanistically, attention circuits copy patterns from the examples into the query's residual stream. Functionally, the examples act as evidence for which already-learned task is in play. Few-shot helps most when the task is ambiguous. It is sensitive to format, label balance, order and similarity. It locates skills and supplies specifics, but does not create new skills.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.