Notes on A Mathematical Framework for Transformer Circuits
Transformer Circuits Thread Notes · 1 of 7
- 1. Notes on A Mathematical Framework for Transformer Circuits
- 2. Notes on In-Context Learning and Induction Heads
- 3. Notes on Toy Models of Superposition
- 4. Notes on Towards Monosemanticity and Scaling Monosemanticity
- 5. Notes on Sparse Crosscoders
- 6. Notes on Circuit Tracing and the Biology of a Large Language Model
- 7. Notes on Tracing Attention Computation Through Feature Interactions
Notes on A Mathematical Framework for Transformer Circuits (Elhage et al., 2021), the first paper in Anthropic’s Transformer Circuits thread.
The paper gives a way to rewrite small, attention-only transformers as sums of paths from input tokens to output logits. Each path is a chain of matrices with a fairly specific job: choose which position to read from, move information between positions, or turn that information into a change in the output distribution. The authors use this view to reverse engineer zero-, one-, and two-layer models.
The residual stream is the shared state
A transformer is usually drawn as a sequence of blocks, which makes it look as though each layer replaces the representation produced by the one before it. The paper uses a different view. Token embeddings initialize a residual stream, and each attention head or MLP reads from that stream and adds an update to it. The unembedding reads the final stream and converts it to logits.
The residual-stream diagram makes the change in viewpoint concrete. The long horizontal bar is the shared state; the heads and MLP sit beside it, each with one path for reading and another for adding its result back. There is no direct arrow from one component box to the next.
No component owns the stream. The residual connection preserves earlier writes unless a later component changes them. A head can add new information alongside an earlier write or cancel it by writing the negative of what is already there. Two components interact only when the first writes into a direction that the second reads. The subspace diagram (Fig. 1) shows both cases: overlapping reads and writes create a connection, while reading a subspace and writing its negative deletes the information carried there.
Suppose one component writes with a matrix and a later component reads with . Their effective connection is the product . That product is a virtual weight: there is no parameter with that value stored in the model, but it describes how the two components communicate through the residual stream. Multiplying weights through the stream exposes an implicit connection between every compatible pair of components. The left side of the next figure (Fig. 2) shows the literal read-and-write architecture; the right side has multiplied through the stream and made those pairwise connections explicit.
An attention head has two jobs
A single attention head computes
The queries, keys, and values in Equation 1 are linear projections of the residual stream (, and so on). The softmax term is the attention pattern . At each position, it gives a distribution over earlier positions to read from. The scaling keeps the scores in a range where the softmax has usable gradients.
The paper combines the four projection matrices into two. Queries and keys meet only through , while values reach the residual stream only through :
The QK circuit determines where the head attends. The OV circuit determines what the head does with the token it finds. Both matrices are low rank because they factor through the smaller head dimension. In the diagram (Fig. 3), the pink QK path touches both the destination and source tokens to produce a score. The gold OV path starts at the selected source and ends at the output logits.
In a one-layer model, multiplying the QK circuit through the token embeddings gives , a matrix that scores pairs of tokens. Multiplying the OV circuit through the embedding and unembedding gives , which maps a source token to its direct effects on the output logits.
For a pattern such as [keep] … [in] → [mind], the QK circuit gives the token in a reason to attend to the earlier keep. Once attention lands there, the OV circuit makes keep vote for mind. The split lets us inspect two questions separately: which source tokens does the head select, and what do those source tokens contribute?
Expanding the model into paths
The attention pattern depends on the input because it comes from a softmax over query-key scores. If is held fixed for a particular input, an attention head is linear. An attention-only model can then be expanded into a sum of paths through the model.
Zero layers. Embed, then immediately unembed. The logits are applied to the current token. A model with no attention can only learn bigram statistics: which tokens tend to follow the current one.
One layer. With attention patterns fixed, the model is a sum of paths:
The tensor product keeps two operations separate. The left factor describes movement between token positions; the right factor describes a transformation within the residual-stream space. The direct path is the zero-layer bigram model. Each head adds another path in which selects a source position and turns the token at that position into a logit update.
This makes a one-layer attention-only model a collection of “skip-trigram” rules. Each contribution can depend on the current token and one earlier token, but the earlier token has not yet received information from anywhere else. Its key and value are functions of its own embedding.
Why a second layer is different
In a two-layer model, the second layer reads a residual stream that already contains the first layer’s outputs. An earlier head can therefore affect the values, queries, or keys used by a later head.
With V-composition, the later head moves information that the earlier head already moved. The path contains an attention pattern and a transform
This behaves like a virtual attention head spanning both layers.
Q- and K-composition affect the later head’s attention pattern itself. An earlier head writes information that becomes part of a later query or key, so the later head can select a token based on context gathered from another position. The induction-head circuit uses K-composition: a previous-token head writes the identity of the preceding token into the current position, and a later head uses that write as a key. The later head can then find a token according to what came before it.
That operation is unavailable to a one-layer model. One layer can compare token identities; two layers can compare a token with information that another head has moved beside it.
What is exact here
The additive decomposition of attention heads and the path expansion are algebraic facts about the architecture. Given fixed attention patterns, the paths sum exactly to the model’s output. The decomposition does not guarantee that an individual path will have a simple interpretation. It gives the analyst a smaller object to inspect.
The paper demonstrates the method on attention-only models with at most two layers. In that setting, the authors can read behaviors from the QK and OV matrices, identify composition between heads, and recover the induction-head circuit. These models are small enough for fairly complete analysis, but they leave out MLPs and the complications that come with scale.
Freezing attention also sets aside part of the computation. The resulting paths explain what information flows through a head under the observed attention pattern. They do not, by themselves, explain why the head attended to those positions. QK analysis handles that question in the small models studied here; doing the same in a large model is harder.
What carried forward
TransformerLens (opens in a new tab) exposes models using this residual-stream and per-head decomposition, and the ARENA curriculum (opens in a new tab) uses it as its introduction to mechanistic interpretability.
Later work separates the two gaps left by the paper. Sparse autoencoders and transcoders try to find workable units for MLP computation. Attribution graphs return to the path-expansion idea: they freeze attention patterns for a prompt and trace linear effects through residual connections and OV circuits. A later attention-circuits paper (opens in a new tab) adds QK interactions back into those graphs. The July 2025 circuits update (opens in a new tab) also revisits the original framework using learned features instead of matrix eigenvectors as the objects moving through the residual stream.
Further material
- The paper itself (opens in a new tab) — the exercises and videos linked in its appendix are good, and the two-layer section rewards a slow read.
- Neel Nanda’s mechanistic interpretability quickstart (opens in a new tab) — orientation for the field this paper started.
- TransformerLens (opens in a new tab) — the standard library for poking at models in this framework.
References
These are notes on the work marked ★ — cite the original rather than this page.
- Elhage, N., Nanda, N., Olsson, C., & others. (2021). A Mathematical Framework for Transformer Circuits. Transformer Circuits Thread. https://transformer-circuits.pub/2021/framework/index.html ★