Search

Search papers and lessons

Residual Stream

Level 3 · 12 min read

The residual stream is the running vector each token carries through a transformer, where every block adds its output so later blocks can read what earlier ones wrote.

On this page

  • Colour = topic mix
  • Calculus
  • Discrete math
  • Neural network basics
  • Linear algebra
  • Probability
  • Architectures
  • ReadReady to readNot yet

TL;DR

The residual stream is the vector that one token carries through a Transformer from the first layer to the last. Each block reads from it, computes something, and adds its result back in, so the vector is a running sum rather than something replaced at every step. Because every block writes into the same place, a later block can read anything an earlier block put there, and the Residual Connection that makes deep training possible also gives the model a shared channel of communication.

Builds on: Transformer, Residual Connection, Layer Normalization · Leads to: Mechanistic Interpretability, Sparse Autoencoders Find Highly Interpretable Features in Language Models

Where it's used

Reading inside large language models. Almost every decoder-only Language Model, from the GPT-2 family onward, is a stack of Transformer blocks that all add to one per-token stream. When a model completes a sentence, the final stream at the last position is what gets turned into next-token probabilities by a Softmax. Engineers who ask why a model made a choice are, in effect, asking which earlier blocks wrote what into this stream.

Circuit analysis of a small model. Wang and colleagues studied GPT-2 small on a task called indirect object identification, where the model must finish a sentence like "When Mary and John went to the store, John gave a drink to" with "Mary". Their circuit is a set of attention heads that move information between token positions, and the analysis is framed in terms of what each head reads from and writes to the residual stream. The paper treats the stream as the place where these moves meet.

Finding features with sparse autoencoders. Cunningham and colleagues trained sparse autoencoders on activations taken from the residual stream of small Pythia language models, and then checked how well the learned directions could be interpreted. Because the residual stream carries the output of every block, it is a natural place to look for the model's internal features.

How it works

Take a tiny model whose stream has only two numbers per token, and follow one token through it. Ignore normalization for now (it comes back in the math).

  1. Start the stream with the embedding. The token "cat" is looked up in the Embedding Layer and gives the vector (1, 0). This is the first value of the stream. Check: the stream has two numbers, so its width is 2.
  2. The attention block writes its output into the stream. Attention looks at earlier tokens and produces a vector of the same width, here (0, 2). The block does not replace the stream; it adds its output to it: (1, 0) + (0, 2) = (1, 2). Check: each number changed by the matching number from the block output.
  3. The feedforward block adds its own output. The feedforward block sees the stream (1, 2) and produces (3, -1). Adding it gives (1, 2) + (3, -1) = (4, 1). Check: the first number is 1 + 3 = 4 and the second is 2 + (-1) = 1.
  4. The final stream is the answer. Nothing else touches the stream after the last block, so (4, 1) is what the model passes on to produce its output. Check: the final value is the embedding plus both block outputs, since 1 + 0 + 3 = 4 and 0 + 2 + (-1) = 1.

The point of step 4 is that the output depends on the embedding and on every block's contribution, summed. The block outputs do not have to be aware of each other; each one just adds to the same vector, and a later block can use whatever sum it finds there.

flowchart LR
  E["Embedding (1, 0)"] --> A["Attention adds (0, 2)"]
  A --> S1["Stream (1, 2)"]
  S1 --> F["Feedforward adds (3, -1)"]
  F --> S2["Stream (4, 1)"]
  S2 --> O["Output"]

The math

Definition: Residual stream
The running vector xlx_l that one token carries through a transformer: it starts as the token's embedding x0x_0, and after each block ll it equals the previous stream plus that block's output.

Write x0x_0 for the stream at the start, the embedding of one token, a vector of width dd (the number of entries). Let block ll compute a function flf_l of the stream, so the stream after block ll is

xl=xl−1+fl(xl−1).x_l = x_{l-1} + f_l(x_{l-1}).

This is the same rule as a Residual Connection: the new stream is the old stream plus a block output. Read it as "keep what is already there and add a correction."

Step 2 in symbols

Step 2 of the walkthrough was x1=x0+f1(x0)x_1 = x_0 + f_1(x_0) with x0=(1,0)x_0 = (1, 0) and f1(x0)=(0,2)f_1(x_0) = (0, 2). Adding component by component gives x1=(1+0,0+2)=(1,2)x_1 = (1 + 0, 0 + 2) = (1, 2).

Step 4 in symbols: unrolling the sum

Apply the rule for LL blocks (where LL is the number of blocks) and substitute each result into the next line:

xL=x0+f1(x0)+f2(x1)+⋯+fL(xL−1).x_L = x_0 + f_1(x_0) + f_2(x_1) + \cdots + f_L(x_{L-1}).

The final stream is the embedding plus the outputs of all blocks, each block evaluated on the stream as it was when that block ran. This is why the walkthrough could check the answer by adding the embedding and both block outputs.

Normalization inside the stream

Real transformers do not feed the raw stream into a block. Most modern models apply Layer Normalization first, so that block ll computes fl(LN(xl−1))f_l(\mathrm{LN}(x_{l-1})), where LN\mathrm{LN} rescales one vector to mean 0 and variance 1 across its own entries. The stream itself is not normalized, only the copy the block reads. The addition in the update rule still uses the un-normalized stream, so the sum keeps its scale from one block to the next. The original 2017 transformer placed the normalization after the addition instead, which is a different arrangement. Both are called residual streams in the interpretability literature, and the difference matters when you trace exact numbers.

Worked examples

Example
Tiny: two blocks on a two-number stream
  1. Start with the stream x0=(2,−1)x_0 = (2, -1).
  2. The first block outputs (1,1)(1, 1). Add it: x1=(2+1,−1+1)=(3,0)x_1 = (2 + 1, -1 + 1) = (3, 0).
  3. The second block outputs (0,3)(0, 3). Add it: x2=(3+0,0+3)=(3,3)x_2 = (3 + 0, 0 + 3) = (3, 3).
  4. Check by unrolling: x0+(1,1)+(0,3)=(2+1+0,−1+1+3)=(3,3)x_0 + (1, 1) + (0, 3) = (2 + 1 + 0, -1 + 1 + 3) = (3, 3). It matches.
  5. Interpretation: the final stream holds the embedding's first value plus everything the blocks added to that value, so the second block did not erase what the first block wrote.
Example
Standard: a three-number stream and a probe direction

Suppose a researcher wants to know how much the stream points along the direction d=(0,1,0)d = (0, 1, 0), which stands for some feature, such as "this token is a place name". The stream is x0=(0.5,1.0,−0.5)x_0 = (0.5, 1.0, -0.5), the first block outputs (0.5,0,0.5)(0.5, 0, 0.5), and the second block outputs (−1.0,0.5,0)(-1.0, 0.5, 0).

  1. After the first block: x1=(0.5+0.5,1.0+0,−0.5+0.5)=(1.0,1.0,0)x_1 = (0.5 + 0.5, 1.0 + 0, -0.5 + 0.5) = (1.0, 1.0, 0).
  2. After the second block: x2=(1.0−1.0,1.0+0.5,0+0)=(0,1.5,0)x_2 = (1.0 - 1.0, 1.0 + 0.5, 0 + 0) = (0, 1.5, 0).
  3. Read the feature: x2⋅d=0⋅0+1.5⋅1+0⋅0=1.5x_2 \cdot d = 0 \cdot 0 + 1.5 \cdot 1 + 0 \cdot 0 = 1.5.
  4. Read the contributions separately, since the stream is a sum: the embedding gives x0⋅d=1.0x_0 \cdot d = 1.0, the first block gives 00, and the second block gives 0.50.5. They add to 1.0+0+0.5=1.51.0 + 0 + 0.5 = 1.5, which matches step 3.
  5. Interpretation: in this made-up stream, the feature is present mostly from the embedding, and the second block adds a little more. This is the kind of accounting that circuit analysis does on real models, with real vectors and many more dimensions.
Example
Realistic: which block wrote a feature

In a real model, a researcher wants to know whether a later block adds a feature or only keeps one that was already there. Take the stream before a block to be x=(1,2,0)x = (1, 2, 0), and the block's output to be f=(0,−1,3)f = (0, -1, 3). Suppose the feature direction is d=(0,1,0)d = (0, 1, 0).

  1. Compute the stream after the block: x+f=(1+0,2+(−1),0+3)=(1,1,3)x + f = (1 + 0, 2 + (-1), 0 + 3) = (1, 1, 3).
  2. Compute the feature before: x⋅d=2x \cdot d = 2.
  3. Compute the feature after: (x+f)⋅d=1(x + f) \cdot d = 1.
  4. The block changed the feature by 1−2=−11 - 2 = -1, which is exactly f⋅d=−1f \cdot d = -1. The block removed some of the feature rather than adding to it.
  5. Interpretation: a block with a negative component along the feature direction is suppressing the feature, and this bookkeeping is how researchers tell suppression from addition.

Try it: the stream before a block is (2,0,1)(2, 0, 1) and the block outputs (1,3,−1)(1, 3, -1). What is the stream after the block, and what is its second component?

Answer: (2+1,0+3,1+(−1))=(3,3,0)(2 + 1, 0 + 3, 1 + (-1)) = (3, 3, 0). Its second component is 33.

Answer: the stream after the block is (2+1,0+3,1−1)=(3,3,0)(2 + 1, 0 + 3, 1 - 1) = (3, 3, 0), so the second component is 33.

Common mistakes and tradeoffs

It is tempting to think each layer replaces the vector it receives with a new one. In fact a block adds to the stream, because the update is xl=xl−1+fl(xl−1)x_l = x_{l-1} + f_l(x_{l-1}). The original input survives to the output unless a block actively cancels it, which is why the walkthrough could add all the blocks together at the end.

It is tempting to think the residual stream is a memory with a separate slot for each layer. In fact there is one vector per token position, and every layer writes into that same vector. Its width stays the same from the embedding to the output, which is what makes addition possible at all.

It is tempting to think a direction in the stream has a fixed meaning that the model assigns in advance. In fact the meaning of a direction is found after the fact, by probing or by training something like a sparse autoencoder on the stream, and the same direction can be used for different things in different contexts.

It is tempting to treat the Residual Connection and the residual stream as the same thing. They are related but not identical: the residual connection is the local rule "add the block output to its input", and the residual stream is the resulting running vector viewed as a shared channel across all blocks.

When to use this view: when you want to trace which block contributed a feature, compare two models of the same design, or design an intervention on activations. When not to use it: when the block structure is different, for example in models where some blocks do not add to a shared stream, or when you only care about the model's outputs and not its internals. Alternatives include looking at each sub-layer's output separately or at attention patterns alone, which discard part of the sum.

History

Residual connections became standard in 2015, when He and colleagues trained very deep image networks by adding each block's input back to its output. The idea that the same vector could carry a signal across many blocks was not new to the Transformer, but it was built into its design in 2017, when every attention and feedforward sub-layer was wrapped in a residual addition. For several years the stream was mostly treated as a training trick.

In the early 2020s, researchers working on mechanistic interpretability began treating it as an object to study in its own right. Elhage and colleagues wrote a mathematical framework for transformers in 2021 that described attention heads and MLPs as reading from and writing to a shared residual stream, and the term became the standard way to talk about that picture. Later work such as the IOI circuit paper and sparse autoencoder studies applied the same framing to real models.

Recap

  • A token's residual stream is one vector of fixed width, starting at its embedding.
  • Each block reads a normalized copy of the stream and adds its output back to the stream.
  • The final stream is the embedding plus every block's output, so later blocks can read what earlier ones wrote.
  • Directions in the stream can be measured by taking dot products, and each block's contribution along a direction can be read separately.

What to read next: Mechanistic Interpretability uses this view to explain model behaviour, and Sparse Autoencoders Find Highly Interpretable Features in Language Models applies it to finding features with sparse autoencoders.

Check yourself

QuestionA stream is (1,2)(1, 2) and a block outputs (3,−2)(3, -2). What is the stream after the block?Show answer

(1+3,2+(−2))=(4,0)(1 + 3, 2 + (-2)) = (4, 0). The block adds its output to the stream.

QuestionWhy can a block at layer 10 use something that a block at layer 2 wrote into the stream?Show answer

Because every block adds to the same per-token vector, so the information from layer 2 stays in the stream unless a later block changes it. The layer 10 block reads the current sum, which includes the layer 2 contribution.

QuestionA stream has the value 3 along a feature direction. A block contributes -1 along that direction. What is the value afterwards, and what does the sign of the contribution tell you?Show answer

The value afterwards is 3+(−1)=23 + (-1) = 2. The negative contribution means the block suppresses the feature, because it pushes the stream away from the feature direction.

QuestionApplied: a researcher finds that a feature's value drops from 4 to 1 across one block. Did that block remove the feature entirely?Show answer

Not necessarily. The drop is −3-3, so the block's contribution along the feature direction is −3-3. The feature is not necessarily gone: the value is still 11, and other blocks may have written the feature earlier or may add it back later. A drop shows the block's effect, not that the stream lost everything.

Sources