mej7.comHow AI Works

What is the residual stream in a transformer?

Author: Mehdi Sadriu, independent educator. Last reviewed: 25 November 2026. How we check claims: methodology.

The residual stream is the notebook each token carries through a transformer: a vector that starts as the token embedding plus position, and that every attention block and every MLP adds to instead of replacing. That picture is the one Elhage et al. (2021) put at the centre of transformer mechanistic interpretability.

In one paragraph. The residual stream is the d_model-dimensional vector at each position that residual connections keep alive: x ← x + Attn(x) and x ← x + MLP(x) (up to layer-norm placement). Components communicate by reading and writing directions in this shared stream.

Toy residual stream Four dimensions, four writes. Teaching numbers, not a model.
StepWhat writesStream (d=4)
0token + position embed1.00.00.00.0
1attn head adds a 'previous token' direction1.00.60.00.0
2MLP adds a 'noun-ish' direction1.00.60.50.0
3later attn copies a name into dim 40.90.60.50.8

Move the slider (if JavaScript is on) to watch the running sum. The rule is always x ← x + Δ, never x ← Δ.

What does that mean in plain language?

Imagine each word in the sentence carrying a backpack of numbers. The backpack starts with a label for the word and a label for its place in the sentence. Every layer looks in the backpack, computes a small update, and drops another note in. Old notes stay unless a later note cancels them. That is why information from layer 1 can still be sitting there at layer 11.

Attention is the only moment when backpacks trade notes with other positions. The MLP rewrites the note at one position only.

What is the precise technical picture?

He et al. (2016) introduced residual connections so a layer learns a correction F(x) added to x, not a whole new representation. Vaswani et al. (2017) put residual adds around attention and around the feed-forward network. Elhage et al. (2021) then treated that leftover vector as the object of study: the residual stream.

Each head reads a linear projection of the stream (queries and keys; values) and writes a rank-constrained update back. Each MLP reads the stream at one position and writes a higher-rank update. Layer-norm rescales the stream before or after those writes (Ba, Kiros & Hinton (2016); GPT-style models are usually pre-norm).

Because writes add, two heads can share the stream without erasing each other if they write in different directions. If they write in the same direction they interfere. Orthogonal subspaces are invisible to each other.

How should I picture communication?

The stream is a bulletin board with d_model slots (768 in GPT-2 small). A head cannot hand a private envelope to another head. It pins a vector on the board; later components read the board. That is why 'which direction did this head write?' is a mechanistic question, and why a pretty attention map is not a complete explanation — the map is only the QK half.

What is the math?

Let xℓ ∈ ℝd be the stream at one position entering block ℓ. A common pre-norm GPT block is:

xℓ(1) = xℓ + Attn(LN(xℓ))

xℓ+1 = xℓ(1) + MLP(LN(xℓ(1)))

Attn is a sum over heads of softmax(QKᵀ / √dk) V, each Q, K, V linear in the (normalized) stream. The 2017 paper used post-norm (layer-norm after the add). The algebra changes; the 'add, don't replace' rule does not.

Worked example

Take the unfinished sentence The cat sat on the. Position 2 ('cat') starts as embed(cat) + pos(2). A layer-1 previous-token head may add a direction that means 'my left neighbour was The'. A later MLP may add a direction that is used, in other work, as a noun or animal feature — that identification would have to be measured, not assumed from this paragraph. At the last layer the unembedding reads the whole sum and scores the next token. The logit lens is the experiment of reading that sum too early (nostalgebraist (2020)).

Check your understanding

Five questions. Answers are in the disclosure under each item, so this page still works if JavaScript is off.

1. What does a residual connection do?
Show answer

B. Add, don't replace: x ← x + F(x).

2. How do attention heads share information with later heads?
Show answer

C. There is no private envelope; there is a bulletin board.

3. Which paper made this residual-stream picture standard in mech interp?
Show answer

B. The 2017 paper has residual adds; the 2021 framework treats the leftover vector as the object of study.

4. If two writes are orthogonal, what happens?
Show answer

B. Dot product zero means invisible.

5. Are the 768-dimensional sliders on mej7.com measurements of GPT-2?
Show answer

B. The lab's own note calls the overlap slider a guess.

Further reading

  1. Deep Residual Learning for Image Recognition — He et al. (2016). CVPR.
  2. Attention Is All You Need — Vaswani et al. (2017). NeurIPS.
  3. A Mathematical Framework for Transformer Circuits — Elhage et al. (2021). Transformer Circuits (Anthropic).
  4. Layer Normalization — Ba, Kiros & Hinton (2016). arXiv.
  5. Interpreting GPT: the logit lens — nostalgebraist (2020). LessWrong.

Related toy lab (teaching model)