In-Context Learning Mechanics
Learning without updating a single weight
In-context learning (ICL) is the ability of a large language model to perform a new task — classification, translation, following a new format — after seeing just a few labeled examples in its prompt, with absolutely no gradient update to its parameters. This is strange on its face: the model is, in some functional sense, 'learning' the task from the examples, purely through a single forward pass. Understanding the actual mechanism behind this has become a central question in LLM interpretability, because it directly bears on how reliable, controllable, and predictable few-shot prompting really is.
The induction-head hypothesis
Mechanistic interpretability work from Anthropic (Olsson et al., 2022) identified a specific circuit pattern, called induction heads, that appears consistently across transformer models and correlates strongly with in-context learning ability. An induction head implements a simple pattern-completion algorithm: given a token sequence [A][B] ... [A], it predicts the next token is likely [B] — essentially 'if this exact pattern occurred before, complete it the same way again.' This is a composition of two attention heads working together (a 'previous token' head and a 'copying' head) rather than a single mechanism, and it emerges reliably during training, often in a sudden, visible phase transition in training loss, coinciding closely with the point where models start showing strong in-context learning behavior.
Sequence seen so far: "... the cat sat on the mat . the dog sat on the ..."
^A ^B ^A (repeats)
Induction head mechanism (two-step attention composition):
1. "Previous token" head: for the current token, find what token came
right before the LAST occurrence of a similar earlier token.
2. "Copying" head: attend to that found token's position and boost
the probability of whatever token followed it originally.
Prediction: strongly favors "mat" (B) as the next token,
because [A][B] already appeared earlier in the exact same pattern.ICL as implicit gradient descent — a competing theoretical framing
A separate, complementary line of theoretical work (e.g., Dai et al., von Oswald et al.) has argued that, at least for certain simplified settings like linear regression, a transformer's forward pass over in-context examples can be mathematically shown to approximate performing gradient descent on those examples internally — the attention mechanism's weighted aggregation over context tokens plays a role structurally analogous to a gradient update, implemented entirely through the forward computation rather than through backpropagation. This doesn't contradict the induction-head finding so much as offer a different level of description: induction heads are a mechanistic circuit observed in real trained models, while the implicit-gradient-descent framing is a more abstract mathematical characterization of what effect that kind of circuit composition can produce.
Two framings of in-context learning
| Framing | Level of description | Main evidence | Main limitation |
|---|---|---|---|
| Induction heads | Mechanistic — specific attention circuit found in real models | Causally verified via ablation studies in trained transformers | Doesn't fully explain more complex, abstract forms of ICL |
| Implicit gradient descent | Mathematical/theoretical — forward pass approximates an optimization step | Proven for simplified settings (e.g. linear function classes) | Less clear it extends cleanly to complex, real-world tasks and large models |
Why this matters for prompt engineering
The mechanism isn't fully solved
What's next?
The attention mechanisms underlying in-context learning are themselves subject to engineering constraints at inference time — as context windows grow longer, the naive attention computation becomes a serious bottleneck, motivating techniques for handling very long sequences efficiently without losing the kind of pattern-matching behavior that in-context learning depends on.
I build these systems professionally.
Whether it's a RAG pipeline, analytics migration, or AI workflow — let's talk.