All courses

Induction heads

Explain the two-head circuit that completes [A][B] … [A] → [B]. A previous-token head writes 'my predecessor was A' at B's position; an induction head at the later A searches for that marker and copies B. Relate it to the sudden drop during training in loss on tokens late in a context, which can be predicted from earlier parts of the same context.

Builds on: QK and OV circuits of a head, The residual stream as a shared channel, In-context learning

▶ Watch this lesson Start the whole course

What the animation says

A pair seen once

Here's a made-up pair: Zxqv, then Plorb. Give the model this line, and at the second Zxqv it predicts Plorb. It almost surely never saw that pair in training. So it must read it from the context. But one attention head can't do this alone. At Plorb's position, the vector only says: I am Plorb. Nothing there says: I came after Zxqv. So how does the model find it?

Step 1: leave a marker

The trick takes two heads. First, in an earlier layer, a previous-token head. Every position attends to the one just before it. It copies that token's identity into its own stream, as a marker. Now Plorb's vector carries a second note: my predecessor was Zxqv. Later, at the second Zxqv, a new head goes searching. What does it look for?

Step 2: the induction head

It looks for the marker. This is the induction head, in a later layer. Its query says: current token is Zxqv. Each key offers that position's marker. Only Plorb's marker matches, so nearly all attention lands on Plorb. Then it copies Plorb's content into the prediction, and Plorb's score goes up. Matching is the query-key circuit. Copying is the value-output circuit. Its keys read the first head's output. That's called K-composition.

A toy induction head in numpy

Here it is in numpy. Tokens three, one, four, two, three: that's A, B, then A again. This line makes one-hot rows: all zeros, with a one at the token's id. P shifts those rows down by one. That's the previous-token head's marker. Now the last position's query: I am token three. This line scores every marker against it, times ten. Which position matches?

Why loss drops late, and suddenly

Why does this circuit matter in training? Here are some illustrative numbers. Without copying, the model gives the follower five percent. That's a loss of three nats. With the circuit, it copies at sixty percent. That's about half a nat. Only tokens with an earlier repeat gain, and those sit mostly late in the context. Neither head lowers loss alone, so each gets little gradient until the other partly exists. Once both start forming, they boost each other, and late-token loss drops sharply.

How does a model copy a pair it saw once?

Give a model `Zxqv Plorb went home. Zxqv` and it predicts `Plorb`. For simplicity, treat each word as one token. The model has likely never seen that pair in training, so it must have read it from the context. One attention head cannot do this alone. At the second `Zxqv`, the head would need to find *the token that came after `Zxqv`*. But the vector at `Plorb`'s position only says "I am Plorb". Nothing there says "I came after Zxqv". So how does the model find it?

Two heads: leave a marker, then search it

**Step 1, previous-token head (earlier layer):** every position attends to the position just before it. It copies that token's identity into its own residual stream. Now B's vector also carries "my predecessor was A". **Step 2, induction head (later layer):** at the second A, the query asks "who has predecessor = A?". Only B's marker matches, so the head attends to B. It then copies B's identity, which raises B's logit.

The circuit in QK/OV terms

Let $e(x)$ be token $x$'s content in the stream. **Head 1:** $A^{(1)}_{t,t-1}\approx 1$, so it adds $e(x_{t-1})W_V^{(1)}W_O^{(1)}$ to $r_t$. This "predecessor" vector lands in a separate subspace from the token's own content. **Head 2:** its score is $r_t W_Q W_K^\top r_j^\top/\sqrt{d_{head}}$. The QK circuit pairs "current token = A" on the query side with "predecessor = A" on the key side. The OV circuit maps B's content to "predict B". Because its keys read Head 1's output, this is called **K-composition**.

Why loss drops late in context, suddenly

Illustrative numbers. Without copying, the model gives B probability $0.05$, so the loss is $-\ln 0.05 = 3.0$ nats. With the circuit it copies B at $0.6$, so the loss is $-\ln 0.6 = 0.51$ nats. Only tokens with an earlier repeat to copy gain this. Most such tokens sit late in a context, while tokens at position 3 rarely have one. Neither head lowers loss alone, so each gets little gradient until the other partly exists. Once both start forming, each makes the other useful. The pair then improves fast, and late-token loss drops sharply over a short stretch of training.

Toy induction head in numpy

Tokens `[3,1,4,2,3]` follow the pattern A=3, B=1, …, A=3. The previous-token head writes each predecessor as a one-hot vector, meaning all zeros except a 1 at that token's id. The last position's query matches positions whose predecessor is 3. That match picks index 1 (0-based), which holds B, so the head copies 1.
import numpy as np
x = np.array([3, 1, 4, 2, 3])          # A B . . A
V = 5
E = np.eye(V)[x]                        # token content, [T, V]
P = np.vstack([np.zeros(V), E[:-1]])    # prev-token head's marker
q = E[-1]                               # query: 'I am token 3'
s = 10 * P @ q                          # match marker 'prev = 3'
w = np.exp(s) / np.exp(s).sum()         # attention weights
print(w.round(3))                       # [0. 1. 0. 0. 0.]
print(x[np.argmax(w)])                  # copies B = 1

Remember this

An earlier previous-token head marks each position with "my predecessor was A". A later induction head at A attends to that marker and copies the token there, completing [A][B]…[A]→[B]. The two heads only work together, so they appear abruptly in training. Loss on later-context tokens, which have earlier repeats to copy, then drops sharply.

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.