Skip to content

In-context Learning and Induction Heads

Skip →TransformerLens
main track #3corefundamentalpaperrubric 12/122022 · ~60 min

Catherine Olsson, Nelson Elhage, Neel Nanda, …, Chris Olah

Read first: A Mathematical Framework for Transformer Circuits

Status

TL;DR. An induction head is an attention head that, at the second occurrence of a token A, attends to the token that followed the first A and copies it. Given “A B … A”, it predicts B. This paper argues that such heads may be the mechanism behind most in-context learning. The argument has six parts. Induction heads and in-context learning appear together in a sudden “phase change” early in training. Architecture changes move both together. Ablations in small models remove most in-context learning. And the same heads do more abstract matching in large models.

Why it matters now. RAG and search agents depend on the model using its context: copying an answer span, following a demonstrated format, reusing an entity from a tool output. Induction heads are the best-understood circuit for “look back and copy”. They help explain why repeated or near-duplicate retrieved content can dominate outputs. The paper is also a model of how to grade evidence, from strong for small models to circumstantial for large ones.

Operational definition (§Key Concepts). On a sequence of repeated random tokens, a head is an induction head if it shows:

  • Prefix matching. It attends to the token right after an earlier copy of the current token.
  • Copying. Its output raises the logit of the attended token.

Minimal circuit (from the framework paper). A layer-1 previous-token head writes “previous = A” at position B. A layer-2 head uses K-composition to match its query “current = A” against that key. Its OV circuit then copies B. In the standard attention-only architecture this needs at least two layers; the smeared-key variant below supplies previous-token information within one layer.

In-context learning score. Loss at token 50 minus loss at token 500, averaged over evaluation contexts of 512 tokens. A larger positive score means later tokens benefit more from context. (The paper’s Key Concepts box writes the difference in the opposite order, loss₅₀₀ − loss₅₀; Argument 1 and the reported 0.15 → 0.4 nat gains use the positive orientation adopted here.)

Smeared key (Argument 2). One learned scalar per head mixes in the previous position’s key:

kj←σ(α) kj+(1−σ(α)) kj−1.k_j \leftarrow \sigma(\alpha)\,k_j + \big(1-\sigma(\alpha)\big)\,k_{j-1}.

This gives even one-layer models access to the previous token.

Induction heads: a previous-token head and an induction head compose to complete [A][B] … [A] → [B] A token sequence contains A then B, later A again. In layer 1, a previous-token head at the position of B attends one step back and writes "the previous token was A" into B's residual stream. In layer 2, the induction head at the second A uses its own token A as the query; the key at B now carries "previous token was A", so the query and key match and the head attends to B (K-composition). Its OV circuit copies B, raising the logit for B as the next token. Right: the two conditions, a prefix-matching QK circuit and a copying OV circuit. Two heads, two layers A B … A next: B e.g. "Harry Potter … Harry" → "Potter"; also works on repeated random tokens layer 1 prev-tok attends 1 back, writes "prev = A" into B layer 2 induction "prev = A" at B (K-composition) query "A" matches key OV copies B → logit of B ↑ Operational test QK: prefix matching attend to the token right after an earlier copy of the current token OV: copying raise the logit of the attended token (large positive eigenvalues) Attn-only: needs ≥ 2 layers the key must hold info about the previous token. Smeared keys supply it within one layer
Original sketch. A layer-1 previous-token head writes “prev = A” into position B. The layer-2 induction head's query at the second A matches that key (QK), then copies B (OV).
  • Scale of evidence. 34 decoder-only models from four series: small attention-only, small with MLPs (1–6 layers), full-scale models from 4 layers/13M to 40 layers/13B parameters, and smeared-key models. The study includes more than 50,000 attention-head ablations.
  • Phase change. In-context learning goes from under 0.15 nats to about 0.4 nats during roughly 2.5–5B tokens of training, which is about 1–2% of training for the large models. It then stays roughly constant (Argument 1).
  • Per-token view. On the first paragraph of Harry Potter, the last token of ” The Dursleys”, which recurs several times, gets much easier to predict across the phase change. The first ” Mrs Potter” gets harder, because both earlier ” Mrs” tokens were followed by ” Dursley”.
  • Ablation. For small attention-only models, almost all in-context learning comes from induction heads, from the start of the phase change to the end of training (Argument 3).
  • Large-model anecdotes. Literal copying, translation and pattern-matching heads in the 13B model (Argument 4).
  • Circumstantial for large models. Without ablations at scale, the large-model claim relies on co-occurrence and continuity.
  • One training run per model and a single data distribution. Hyperparameter changes near the phase-change window are a possible confound, which the authors discuss.
  • ICL defined as token-index loss gain. This is broader than few-shot task learning and may mix several mechanisms.
  • Mechanism in large models. It is open how fuzzy or abstract induction heads match, since they likely compose with MLP features in superposition.

Questions to think through

These questions have no single right answer. Write or say your answer before you open the hints. Mark a question once you could defend your answer in a reading group.

  1. Derive / compute

    For the sequence "… A B … A", trace an induction head's computation step by step through the QK and OV circuits, naming which head writes what into the residual stream at which position. Then explain why a one-layer attention-only model cannot implement it.

    What a strong answer covers
    • Layer 1: a previous-token head at the position of B attends to A and, through its OV circuit, writes "previous token = A" into B's residual stream.
    • Layer 2, QK: at the second A (the query), the induction head's key at each earlier position reads that layer-1 output (K-composition). Its QK circuit matches "current token = A" with "previous token = A", so it attends to B. This is prefix matching.
    • Layer 2, OV: the head copies the attended token, raising the logit of B (copying OV circuit with positive eigenvalues).
    • In one layer, keys contain only each position's own token (plus position). Nothing has moved "previous token" information into the key yet, so the head cannot condition on what preceded the source token.
  2. Mechanism

    The paper measures in-context learning as the gap between loss at token 50 and loss at token 500 (loss₅₀ − loss₅₀₀, so that improvement is positive) and finds it jumps from under 0.15 nats to about 0.4 nats in a window of roughly 2.5–5 billion training tokens. Explain why this measure captures in-context learning, why the jump appears as a bump in the training loss curve, and what alternative explanation for the coincidence with induction head formation the paper must rule out.

    What a strong answer covers
    • If later tokens are predicted better than earlier ones, the model is using its context. The difference isolates that gain from overall loss, which falls everywhere over training.
    • During the phase change the model quickly gains a new capability (copying from context), so loss drops faster for a short time; plotted against tokens, this looks like a bump or bend in the loss curve.
    • The ICL score then stays roughly constant for the rest of training, across model sizes.
    • Alternatives: a hyperparameter schedule change (for example learning-rate warm-up or weight-decay changes) or another mechanism forming at the same time. Co-occurrence alone is correlational, which is why the paper adds co-perturbation and ablation arguments.
  3. What if…

    The "smeared key" architecture sets k_j = σ(α)·k_j + (1 − σ(α))·k_{j−1} with a learned α per head. Explain what this change is designed to test, what the results were for one-layer and two-layer models, and why this counts as stronger evidence than the phase-change co-occurrence.

    What a strong answer covers
    • The smear lets a head's key directly contain information about the previous token, so an induction head can form without a separate layer-1 previous-token head.
    • Prediction: if induction heads drive in-context learning, the smear should let one-layer models learn in-context and move the phase change earlier for deeper models.
    • Result: one-layer smeared models develop in-context learning they otherwise lack, and it forms earlier in two-layer models.
    • This is a co-perturbation: changing whether and when induction heads can form shifts the in-context-learning change in the same way, which is harder to explain by coincidence than co-occurrence.
  4. Critique

    The paper is careful to grade its evidence. Explain which claims are strong for small attention-only models but only suggestive for large models, and what experiment would most directly close the gap for a 13B-parameter model.

    What a strong answer covers
    • Ablation evidence (removing heads at test time) exists only for small models; the authors note full ablations for their large models were not run because cost grows superlinearly with model size.
    • For small attention-only models, almost all in-context learning is attributable to induction heads by ablation, so the claim is strong there.
    • For large models with MLPs, the evidence is co-occurrence, continuity of trends and anecdotal examples (for example translation and pattern-matching heads in a 13B model), so it is circumstantial.
    • Closing the gap: targeted ablation or activation patching of heads with high prefix-matching and copying scores in the large model, measuring the ICL score and few-shot accuracy, against random head ablations of matched size.
  5. Design

    In retrieval-augmented generation, the model often copies a span from a retrieved passage. Design an experiment to test whether induction-style heads mediate this copying in an open LLM, and how you would distinguish copying from genuine reasoning over the passage.

    What a strong answer covers
    • Find candidate heads with the paper's operational test: on repeated random token sequences, measure prefix-matching (attention to the token after an earlier copy) and copying (logit increase of the attended token).
    • Build RAG prompts where the answer appears verbatim in a passage, and variants where it must be paraphrased or combined from two sentences.
    • Ablate or patch the candidate heads and measure the drop in exact-copy answers versus paraphrase or synthesis answers, against random-head ablations.
    • Prediction if heads mediate copying: large drop for verbatim answers, small drop for synthesised ones. Check also whether fuzzy-matching heads (matching on meaning, not tokens) are involved.
  6. Research idea

    Search agents and deep-research systems put very long tool outputs into the context. Propose a research question about how induction heads behave in this regime (long, noisy, repetitive contexts), a minimal experiment and a result that would change how you build such agents.

    What a strong answer covers
    • Question example: do induction heads cause the model to repeat earlier tool outputs or earlier reasoning loops when the context contains near-duplicate passages?
    • Minimal experiment: insert controlled near-duplicate passages at different positions in a long context and measure repetition rate and attention of prefix-matching heads to the duplicates.
    • Intervention: down-weight or ablate the top induction heads at late positions and check repetition and task accuracy.
    • Actionable result: if induction heads drive the loops, deduplicating tool outputs before insertion or position-aware context packing should reduce them; if not, look elsewhere (for example sampling settings).

Done with this page?

Status