AI Grimoire
Sheet
statusstandard
difficultyintermediate
timeO(L²)
described2022
revised10d ago

Induction Heads

Two attention heads composing across layers: one writes the previous token into the residual stream, the next searches for where the current token appeared before and copies what followed it.

[circuits][causal]Current standard

Standing

Current standardPart of the default recipe. Present in most frontier models trained today, and the thing a new design departs from rather than argues for.

The clearest fully traced circuit there is, and the existence proof the rest of the field argues from.

judged as of 2026-09 · what the labels mean

Theory

Present a model with a repeated random sequence. At the second occurrence of a token it predicts what followed the first occurrence — with tokens it has never seen in training, so this is not memorisation but an algorithm implemented in the weights.

[A][B]    [A]    [B][A]\,[B]\;\dots\;[A] \;\longrightarrow\; [B]
eq. 1 — the behaviour, stated as a rule

The circuit

Two heads in different layers, composing through the residual stream.

The previous-token head, in an early layer, attends from each position to the one before it and writes that token’s embedding into a subspace of the residual stream. The induction head, later, forms its query from the current token and matches it against those written-back embeddings — so it attends to the position after an earlier copy of the current token, and its OV circuit copies that position’s token to the output.

qi=WQxi,kj=WK(xj+WOVprevxj1written by head 1)q_i = W_Q\, x_i, \qquad k_j = W_K\bigl( x_j + \underbrace{W_{OV}^{\text{prev}} x_{j-1}}_{\text{written by head 1}} \bigr)
eq. 2 — Q-composition: the key comes from what the earlier head wroteElhage et al.

The match therefore fires when xixj1x_i \approx x_{j-1} — the current token equals the token before position jj — and attention lands on jj, whose content is then copied.

The phase change

Induction heads do not appear gradually. They form in a narrow window of training, and that window coincides with a visible bump in the loss curve and with the model’s abrupt acquisition of in-context learning. Ablating the heads after training removes most of the in-context learning gain.

ICL score=Ltoken 50Ltoken 500\text{ICL score} = \mathcal{L}_{\text{token } 50} - \mathcal{L}_{\text{token } 500}
eq. 3 — the standard measure of in-context learning

A model that uses context well predicts the 500th token of a document much better than the 50th; the gap is the score, and it jumps when the heads form.

The strong version of the claim — that induction heads are the mechanism behind in-context learning generally, including few-shot task performance — is supported by ablation and correlation, not proved. The weak version, that they implement literal copying, is not in doubt.

Implementation

python · torch — the standard detector
import torch
from torch import Tensor


@torch.no_grad()
def induction_score(model, seq_len: int = 64, vocab: int = 1000) -> Tensor:
    """Mean attention paid to the token after the current token's
    previous occurrence. Returns [layers, heads]."""
    half = torch.randint(0, vocab, (1, seq_len))
    tokens = torch.cat([half, half], dim=1)      # exact repeat

    _, cache = model.run_with_cache(tokens)
    scores = []

    for layer in range(model.cfg.n_layers):
        # [batch, head, query, key]
        pattern = cache["pattern", layer]
        # from position i in the second copy, the induction target is
        # i - seq_len + 1: one after where this token appeared before
        diagonal = pattern.diagonal(offset=-(seq_len - 1), dim1=-2, dim2=-1)
        scores.append(diagonal.mean(dim=-1)[0])

    return torch.stack(scores)

The offset is the whole detector: -(seq_len - 1) selects, for each query in the repeated half, the key one position after the matching token in the first half. Off by one and you are measuring a previous-token head instead, which scores high on a different diagonal and is easy to mistake for a positive result.

Layers needed
2
Heads in circuit
2
Formation
a visible loss bump
2-layer attention-only model, reported in the source

Related

References

[1]Olsson et al. — In-context Learning and Induction Heads (2022)transformer-circuits
[2]Elhage et al. — A Mathematical Framework for Transformer Circuits (2021)transformer-circuits
[3]Wang et al. — Interpretability in the Wild: a Circuit for IOI (2022)arXiv:2211.00593