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.
| Step | What writes | Stream (d=4) | |||
|---|---|---|---|---|---|
| 0 | token + position embed | 1.0 | 0.0 | 0.0 | 0.0 |
| 1 | attn head adds a 'previous token' direction | 1.0 | 0.6 | 0.0 | 0.0 |
| 2 | MLP adds a 'noun-ish' direction | 1.0 | 0.6 | 0.5 | 0.0 |
| 3 | later attn copies a name into dim 4 | 0.9 | 0.6 | 0.5 | 0.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.
Further reading
- Deep Residual Learning for Image Recognition — He et al. (2016). CVPR.
- Attention Is All You Need — Vaswani et al. (2017). NeurIPS.
- A Mathematical Framework for Transformer Circuits — Elhage et al. (2021). Transformer Circuits (Anthropic).
- Layer Normalization — Ba, Kiros & Hinton (2016). arXiv.
- Interpreting GPT: the logit lens — nostalgebraist (2020). LessWrong.