Foundations to Frontier
Tests
F. NLP and Large Language Models › F15 Interpretability of LLMs

Mechanistic interpretability: circuits and induction heads

Mechanistic interpretability tries to reverse-engineer the algorithms inside a network. It describes them as circuits: features connected through weights and attention heads. The best-understood example is the induction head, found by Elhage et al. (2021) and Olsson et al. (2022). A previous-token head writes 'the token before me' into each position. An induction head in a later layer then uses this to find an earlier occurrence of the current token, and copies the token that followed it: [A][B] … [A] → [B]. Induction heads form in a sudden phase change during training, at the same time as in-context learning improves. This page covers the residual stream, QK and OV circuits, composition between heads, ablation tests and circuit discovery. In the exercises you compute previous-token scores, induction targets and an in-context learning score. You then build the two-head induction circuit by hand and test it on repeated random sequences over 64 tokens. Loss on the repeated half fell from 4.16 nats (uniform) to 0.08. Removing either head destroyed this. A one-layer model, which cannot compose heads, did worse than uniform (7.95).

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Finding the little programs inside a model

Read ‘Captain Okafor said … later, Captain’ and you expect ‘Okafor’. Language models learn the same trick: when a word appears again, look back to where it appeared before and copy what came next. Researchers found the exact pair of attention heads that does this. One marks each word with the word before it; the other uses those marks to find the earlier occurrence. Removing either breaks the trick. The demo shows how loss drops on the repeated half of a sequence.

Interpretability lab
Loss per token on a repeated random sequence: first copy 3.91 nats (unpredictable), second copy 0.30 nats. An induction head looks back to the previous occurrence of the current token and copies what followed.
Why engineers care

Understanding circuits could one day let engineers verify what a model computes, as they verify flight software, instead of only testing it.

📝 Quick check (Level 0)
0/3 answered
  1. 1. An induction head completes [A][B] … [A] with…
  2. 2. The circuit needs a…
  3. 3. Removing one of the heads…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Residual stream: each layer reads from and writes to a shared vector; heads communicate through it.
  • QK circuit: W_Q^T W_K decides where a head attends; OV circuit: W_O W_V decides what it moves.
  • Composition: a later head can use an earlier head’s output in its queries (Q), keys (K) or values (V). Induction heads use K-composition with a previous-token head.
  • Evidence: induction heads appear in a phase change early in training, together with a jump in in-context learning; ablating them removes much of that ability (Olsson et al., 2022).
  • Other circuits: indirect object identification in GPT-2 small (Wang et al., 2022), greater-than, modular addition via Fourier features (Nanda et al., 2023).
  • Tools: TransformerLens, nnsight, automated circuit discovery (ACDC), attribution graphs and transcoders.
  • Limits: circuits found in small models or narrow tasks; features are often polysemantic (F15-4); results need ablation and patching (F15-5).
📝 Quick check (Level 1)
0/3 answered
  1. 1. The OV circuit decides…
  2. 2. Induction heads use…
  3. 3. Induction heads appear…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Finding circuits in practice

Exercise 1.

  • Previous-token score of an attention pattern.
  • Induction targets in a token sequence.
  • In-context learning score.

Practice.

  • Load models in TransformerLens (HookedTransformer) to cache activations and attention patterns by name.
  • Detect induction heads with repeated random sequences: attention to the token after the previous occurrence.
  • Confirm with ablation (zero or mean ablation) and activation patching.
  • Read QK and OV matrices through the embedding and unembedding to see what heads match and copy.
  • Start with small models (GPT-2 small, Pythia) where circuits are known.
from transformer_lens import HookedTransformer
import torch
model = HookedTransformer.from_pretrained("gpt2-small")
half = torch.randint(1000, 10000, (1, 50)); toks = torch.cat([half, half], 1)
_, cache = model.run_with_cache(toks)
for L in range(model.cfg.n_layers):
    A = cache["pattern", L][0]                               # heads × query × key
    score = A[:, 50:, 1:51].diagonal(dim1=1, dim2=2).mean(-1)  # attention to the token after the earlier copy
    print(L, score.round(decimals=2).tolist())
🐍 Exercise 1: heads and targets
Score a previous-token head, list induction targets and compute an in-context learning score.
📝 Quick check (Level 2)
0/3 answered
  1. 1. A library for hooked transformers:
  2. 2. Induction heads are detected with…
  3. 3. Circuit claims need…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Why one layer is not enough. A single head’s attention score depends on the query token and key token only (plus position). To attend to ‘the token after an earlier copy of me’, the key must contain information about the previous token, which is not in the key token itself. A first-layer head can write it there; the second-layer head reads it: K-composition.

Path expansion. For an attention-only transformer, the output is a sum over paths through heads; with h heads per layer and n layers there are (h + 1)^n paths. Induction is a two-head path; most paths contribute little.

Cost of copying. A copying head with nothing to copy still moves some token’s identity to the output; if the readout is confident, the model pays when no repeat exists. Trained models balance this with other heads and learned confidence.

📝 Quick check (Level 3)
0/3 answered
  1. 1. A one-layer head's attention depends on…
  2. 2. Paths in 2 layers with 12 heads each:
  3. 3. Induction is a path through…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

Does a hand-built two-head circuit implement induction, and which parts are necessary?

History.

  • N. Elhage et al., “A mathematical framework for transformer circuits” (2021).
  • C. Olsson et al., “In-context learning and induction heads” (2022).
  • K. Wang et al., “Interpretability in the wild: a circuit for indirect object identification in GPT-2 small” (2022).

Exercise 2. A hand-built attention-only transformer.

  • Residual stream: token one-hot (64), position one-hot and a ‘previous token’ slot.
  • Layer 1: a previous-token head writes the previous token into the slot.
  • Layer 2: an induction head; its query is the current token and its keys read the slot; it copies the attended token to the logits. Attention sharpness 8; logit scale 8.
  • Data: 300 sequences of 20 distinct random tokens, then the same 20 repeated; also sequences with no repeats.
  • Variants: remove either head; a one-layer model whose single head matches the current token against earlier tokens.
model loss, first copy loss, repeated copy induction score
uniform guess 4.159 4.159 –
full circuit 4.597 0.082 0.965
without previous-token head 4.597 4.028 0.034
without induction head 4.159 4.159 0
one-layer model 4.576 7.945 0
  • The circuit works: loss on the repeat fell to 0.08 nats; the head put 96.5% of its attention on the right position.
  • Both heads are necessary: removing either restored near-uniform loss.
  • One layer fails: matching the current token copies the current token, which is the wrong prediction, so loss rose above uniform.
  • Cost: with nothing to copy, the circuit paid 0.44 nats over uniform on the first copy (4.293 on sequences without repeats).
  • Seeds: the construction is deterministic in structure; seeds 1 and 2 gave identical numbers.
  • Caveat: built by hand, not learned; it shows that the mechanism is sufficient, not that a given trained model uses it.
🐍 Exercise 2 (research): does a hand-built two-head circuit implement induction, and which parts are necessary?
Build a previous-token head and an induction head by hand; measure loss on repeated sequences, ablate each head, and compare with a one-layer model.

Circuits in AI, robotics and aerospace

  • Verification: circuit-level understanding of small controllers could complement testing for certification.
  • Copying behaviour: induction-like mechanisms explain why models repeat call signs, IDs and numbers from context, and also why they copy errors.
  • Monitoring: known circuits can be monitored at run time for unusual activation patterns.

References

  1. N. Elhage et al. (2021). “A mathematical framework for transformer circuits.” Transformer Circuits Thread.
  2. C. Olsson et al. (2022). “In-context learning and induction heads.” Transformer Circuits Thread.
  3. K. Wang et al. (2023). “Interpretability in the wild: a circuit for indirect object identification in GPT-2 small.” ICLR.
  4. A. Conmy et al. (2023). “Towards automated circuit discovery for mechanistic interpretability.” NeurIPS.