Skip to content

A Mathematical Framework for Transformer Circuits

Anthropic’s foundational mechanistic-interpretability paper reframes a decoder-only Transformer as a sum of independent end-to-end paths through a shared residual stream communication channel, each attention head viewed as an independently additive operator that reads from and writes to subspaces of that stream. By rewriting attention as a tensor product of a QK (where-to-read) circuit and an OV (what-to-write) circuit, the paper turns one- and two-layer attention-only Transformers into fully contextualized weight tables — one-layer models become ensembles of bigram + skip-trigram look-ups, two-layer models unlock induction heads via Q/K/V-composition and perform a primitive but genuine in-context-learning algorithm. The residual-stream-as-bandwidth framing (dimensions as memory, subspaces as channels, 100× more computational than stream dimensions, “memory management” neurons that delete prior writes) is the vocabulary the field has since used to explain nearly every modern depth-side architectural choice.

  • Each attention and MLP sublayer reads its input from the residual stream via a linear projection and writes its result back by adding a linear projection — so the residual stream is a shared, high-bandwidth communication channel across all layers, not a per-layer activation [§Transformer Overview].
  • The residual stream has no privileged basis; it can be rotated by rotating every matrix that interacts with it without changing model behavior [§Residual stream].
  • Any two layers (even non-adjacent) are connected by virtual weights obtained by multiplying out their output-projection and input-projection matrices through the stream, making non-local circuit analysis a straight matrix multiplication [§Residual stream].
  • The residual stream is deeply bandwidth-constrained: ~100× more “computational dimensions” (neurons + head-output dimensions) must communicate through it than it has dimensions, forcing communication in superposition across subspaces [§Residual stream].
  • Some MLP neurons and attention heads appear to perform memory management — reading in a direction and writing out its negation — i.e. actively deleting residual-stream entries [§Residual stream].
  • Multi-head attention is mathematically equivalent to independent per-head attention operations each writing their own linear projection into the residual stream and summing; concatenate-then-project is just a compute-efficient fusion of the same operation [§Attention Heads are Independent and Additive].
  • An attention head factors cleanly into a QK circuit (bilinear form determining where to attend) and an OV circuit (linear map determining what to move from source to destination), and the two can be studied independently [§Attention Heads as Information Movement].
  • A one-layer attention-only Transformer is an ensemble of a bigram model (the direct path) plus a sum of skip-trigram models (one per head, defined by the expanded OV and QK matrices) [§One-Layer Attention-Only Transformers].
  • Most attention heads in one-layer models dedicate most of their capacity to copying behavior: the OV circuit boosts attended-to tokens, the QK circuit attends to tokens that could plausibly be the next token [§Skip-Trigrams and Copying].
  • Two-layer attention-only Transformers exploit Q-composition, K-composition, and V-composition of heads through the residual stream to express much more powerful algorithms than skip-trigrams — including induction heads that implement a primitive but general in-context-learning algorithm (match-and-copy on repeated patterns) [§Two-Layer Attention-Only Transformers, §Induction Heads].
  • Induction heads only appear in models with at least two attention layers, because they require composition of two heads through the residual stream [§Induction Heads].

The core move is a change of representation, not of model. The paper leaves the standard decoder-only Transformer untouched and rewrites it as a product of three terms (embedding, attention stack, unembedding) and then expands the product into a sum over end-to-end paths — one path per subset of heads that composes along the depth axis. Attention is rewritten using tensor products so that the sequence-mixing map (QK^T softmaxed, acting on token positions) and the feature-mixing map (W_O W_V, acting on residual-stream dimensions) can be read off separately as two matrices per head. Both expansions are mathematically equivalent to the original forward pass; the point is interpretability, not compute.

With this representation the residual stream shows up as a bus that every layer reads/writes linearly, every head becomes an independent additive operator that lives in whatever subspace its W_O writes into, and composition between heads at different depths factors into Q-, K-, and V-composition (whether head B’s query, key, or value reads a subspace that head A writes). The paper then applies this machinery concretely: for one-layer models it reads skip-trigram tables directly off expanded OV + QK matrices, uses the eigenvalue spectrum of the OV matrix as a copying detector (10/12 heads in a toy model are “copying”), and surfaces that most capacity goes to context-conditional copying. For two-layer models it diagnoses induction heads as the mechanism behind in-context pattern matching, and shows they require composition and therefore cannot appear in one-layer models.

The paper is interpretability rather than benchmarks, but it ships two concrete empirical findings. On toy one-layer attention-only Transformers, the eigenvalue-based copying statistic flags 10 of 12 heads as copying, matching qualitative inspection of the expanded OV/QK tables; the skip-trigram tour includes code (def … ( → self, open … , → "r", super … self → ).__), LaTeX (\left … \ → \right), URL completion (twitter … / → status), and multi-token word repair (R … → alph when the model has seen " Ralph" nearby). On two-layer attention-only Transformers it identifies induction heads as a composition pattern that implements a general match-and-copy in-context-learning algorithm, and demonstrates these heads are structurally impossible in one-layer models. A follow-up paper confirms induction heads also appear in large, realistic models.

The residual-stream-as-communication-bus framing is the piece of vocabulary that nearly every modern depth-side architectural trick in the wiki is implicitly reaching for. Attention Residuals (AttnRes / Block AttnRes) — Technical Report replaces unit-weight residual addition with a per-layer softmax over the depth history — i.e. it treats “which prior layer to read from” as a learned attention pattern over the bus instead of the fixed “sum of everything written so far” that standard residuals impose. Multi-Head Attention Residuals takes the next step and makes that depth-attention multi-head, exactly on the grounds this paper flags as the central bandwidth problem: feature subspaces have genuinely different depth-read needs and a single shared query forces them into a compromise. mHC: Manifold-Constrained Hyper-Connections generalizes residual connections to matrix-valued, manifold-constrained hyper-connections — another way to say “give the bus more structured bandwidth.” Mean Mode Screaming: Mean-Variance Split Residuals for 1000-Layer Diffusion Transformers diagnoses deep-DiT collapse in exactly these terms: residual writers produce a mean-coherent shock that saturates the bus’s shared basis, and the Softmax Jacobian’s null-space then suppresses the gradient that would re-inject centered variation. A Unified View of Attention and Residual Sinks: Outlier-Driven Rescaling is Essential for Transformer Training frames attention and residual sinks jointly as an outlier-rescaling story on the same bus. 2605.06708-gated-attention-large-language-models-era “attention sink” discoveries (StreamingLLM and the Discovery of Attention Sinks) are literally the “memory management” neurons this paper predicted — specific residual-stream directions that are deliberately pinned to absorb the attention budget so other tokens can communicate cleanly. Even the Looped Transformers line (depth-weight-sharing, with Hyperloop Transformers stacking looping on top of hyper-connections) is a bet on reusing bus capacity across depth, not expanding it. The paper is also where virtual weights come from — the primitive underneath later cross-layer analyses like SPD, Universal SAEs, and the Natural Language Autoencoder audit stack in Mechanistic Interpretability.