Simple Algorithm
Artificial Intelligence · Deep Layer · 30 min

Mechanistic Interpretability

Circuits, features and superposition inside transformers.

Every weight of a transformer is visible, yet what it computes is not. Mechanistic interpretability tries to reverse-engineer trained networks into features and the circuits that connect them. This lesson covers the toolkit (probes, attributions, causal patching), the circuit view of attention, why features end up in superposition, and how sparse autoencoders try to undo it.

What you’ll learn

  • Distinguish probing, attribution and activation patching, and prove the completeness property of integrated gradients.
  • Decompose an attention-only transformer into direct, QK and OV paths, and describe circuits found this way.
  • Explain superposition with a toy model, and derive the sparsity at which sharing one dimension between two features beats dedicating it to one.
  • Formulate sparse autoencoders as dictionary learning, derive the shrinkage caused by the L1 penalty, and weigh what they have found against their limitations.

Questions and tools

Interpretability asks what information an activation encodes, which components cause a behavior, and what algorithm the weights implement. The mechanistic program works bottom-up. It looks for features, directions in activation space that track interpretable properties, and circuits, the weighted subgraphs that compute features from other features through the residual stream (Attention & Transformers).

MethodQuestionCaveat
Probing: a classifier, usually linear, on frozen activations (Alain & Bengio, 2016)Is property yy decodable at layer ℓ\ell?Decodable is not used. A flexible probe can learn the task itself, so compare with control tasks (Hewitt & Liang, 2019) and test causally.
Attribution: gradients, integrated gradients, SHAP (Explainability Methods)Which inputs or units did this output depend on, locally?Gradients are local and saturate; plausible-looking attributions can fail sanity checks.
Activation patching: replace an activation with its value on another input (Vig et al., 2020; Meng et al., 2022)Is component cc causally sufficient or necessary for the behavior?Results depend on the corrupted input and the metric; patching one component at a time misses redundancy.
DerivationProof, and its patching cousin
  1. F(x)−F(x′)=∫01ddαF(γ(α)) dα=∑i(xi−xi′)∫01∂iF(γ(α)) dα=∑iIGi(x)F(\mathbf x)-F(\mathbf x')=\int_0^1\frac{d}{d\alpha}F(\gamma(\alpha))\,d\alpha=\sum_i(x_i-x_i')\int_0^1\partial_iF(\gamma(\alpha))\,d\alpha=\sum_i\mathrm{IG}_i(\mathbf x)

    Differentiate FF along the straight path γ(α)=x′+α(x−x′)\gamma(\alpha)=\mathbf x'+\alpha(\mathbf x-\mathbf x') with the chain rule, and integrate:

  2. IEc=m(x~; ac←ac(x))−m(x~) ≈ (ac(x)−ac(x~)) ⁣⊤∂m∂ac∣x~\mathrm{IE}_c=m\big(\tilde{\mathbf x};\,a_c\leftarrow a_c(\mathbf x)\big)-m(\tilde{\mathbf x})\ \approx\ \big(a_c(\mathbf x)-a_c(\tilde{\mathbf x})\big)^{\!\top}\frac{\partial m}{\partial a_c}\Big|_{\tilde{\mathbf x}}

    Activation patching measures a component's indirect effect: run the corrupted input x~\tilde{\mathbf x}, set component cc to its activation ac(x)a_c(\mathbf x) on the clean input, and record the change in a metric mm, such as the logit difference between the right and a wrong answer. Attribution patching estimates every component's effect at once with a first-order Taylor expansion, from two forward passes and one backward pass:

  3. Integrated gradients is the same idea integrated along a path instead of linearized at one end. By completeness, its attributions add up exactly to the change in output; the one-step estimate does so only when mm is linear in the activations.

Circuits: reading attention as paths

Elhage et al. (2021) read an attention-only transformer as a sum of paths. With embedding WEW_E, unembedding WUW_U and, for head hh, the QK matrix WQKh=WQhWKh⊤W_{QK}^h=W_Q^hW_K^{h\top} and OV matrix WOVh=WVhWOhW_{OV}^h=W_V^hW_O^h (The Mathematics of Attention), a one-layer model without MLPs and ignoring layer normalization maps one-hot token rows TT to logits:

logits=TWEWU⏟direct path+∑hAh⏟QK: where T WEWOVhWU⏟OV: what,Ah=softmax⁡causal(TWEWQKhWE⊤T⊤/dk)\text{logits}=\underbrace{TW_EW_U}_{\text{direct path}}+\sum_h\underbrace{A^h}_{\text{QK: where}}\,T\,\underbrace{W_EW_{OV}^hW_U}_{\text{OV: what}},\qquad A^h=\softmax^{\text{causal}}\big(TW_EW_{QK}^hW_E^\top T^\top/\sqrt{d_k}\big)
Each underbraced product is a ∣V∣×∣V∣|V|\times|V| matrix you can inspect. The direct path approximates bigram statistics; each head's OV matrix says which output tokens it promotes when it attends to a given token. In deeper models, heads compose through the residual stream: a later head can read an earlier head's output in its queries, keys or values (Q-, K- and V-composition), which is how induction heads arise (In-Context Learning & Emergence).

Landmark circuits found this way, in small models or small pieces of larger ones:

  • Induction heads (Elhage et al., 2021; Olsson et al., 2022), with causal evidence in attention-only models.
  • Indirect object identification in GPT-2 small: after "When Mary and John went to the store, John gave a drink to", the model predicts "Mary". Path patching revealed 26 attention heads in 7 classes, including heads that detect the duplicated name, heads that suppress it and "name mover" heads that copy the other name (Wang et al., 2023).
  • Modular addition. A one-layer transformer trained on a+b mod 113a+b\bmod113 represents inputs as sines and cosines at a few frequencies and combines them with cos⁡ω(a+b)=cos⁡ωacos⁡ωb−sin⁡ωasin⁡ωb\cos\omega(a+b)=\cos\omega a\cos\omega b-\sin\omega a\sin\omega b. Its delayed generalization ("grokking") coincides with this circuit forming and the memorizing solution being cleaned up (Nanda et al., 2023).

Circuit discovery can be partly automated by patching edges and pruning those that do not matter (Conmy et al., 2023). A circuit is judged by faithfulness (does it alone reproduce the behavior?), completeness and minimality, all measured by patching and all dependent on the corrupted inputs chosen.

Check your understanding

In a one-layer attention-only transformer, which matrix tells you which output tokens head hh promotes when it attends to token tt?

  1. WEWQKhWE⊤W_EW_{QK}^hW_E^\top
  2. WEWUW_EW_U
  3. Row tt of WEWOVhWUW_EW_{OV}^hW_U
  4. The attention pattern AhA^h
Show the answer

C. Row tt of WEWOVhWUW_EW_{OV}^hW_U

The OV circuit maps the attended token's embedding through WVhWOhW_V^hW_O^h into the residual stream and on to the logits: row tt lists the logit change for every output token. The QK circuit decides where the head attends, not what it writes.

Superposition

Individual neurons are often polysemantic, responding to unrelated inputs. Elhage et al. (2022) explain this with superposition: when features are sparse, a network can represent more features than it has dimensions by giving them nearly orthogonal directions and tolerating interference (Representation Learning covers the geometry). Their toy model compresses nn features into m<nm<n dimensions and reconstructs them:

h=Wx∈Rm,x^=ReLU(W⊤h+b),L=E∑iIi (xi−x^i)2\mathbf h=W\mathbf x\in\R^m,\qquad\hat{\mathbf x}=\mathrm{ReLU}\big(W^\top\mathbf h+\mathbf b\big),\qquad\loss=\E\sum_iI_i\,(x_i-\hat x_i)^2
Each xix_i is 00 with probability SS (the sparsity) and uniform on [0,1][0,1] otherwise, with importance IiI_i. With dense features the model stores the mm most important ones in orthogonal directions, much like PCA, and drops the rest; as sparsity grows it packs in more, in arrangements such as antipodal pairs and pentagons, with sharp phase changes between regimes.
DerivationWhen does sharing a dimension pay?
  1. Lded=Var⁡(x2)=1−S3−(1−S)24\loss_{\text{ded}}=\Var(x_2)=\frac{1-S}{3}-\frac{(1-S)^2}{4}

    Take n=2n=2 equally important features, m=1m=1, and compare two candidate solutions. Dedicated: W=(1,0)W=(1,0) stores x1x_1 exactly and predicts x2x_2 by a constant bias, its mean 1−S2\tfrac{1-S}2, so the loss is the variance of x2x_2:

  2. Lsup=(1−S)2⋅2 E[min⁡(u,v)2]=(1−S)23\loss_{\text{sup}}=(1-S)^2\cdot2\,\E\big[\min(u,v)^2\big]=\frac{(1-S)^2}{3}

    Antipodal superposition: W=(1,−1)W=(1,-1) and b=0\mathbf b=\mathbf 0, so h=x1−x2h=x_1-x_2, x^1=ReLU(h)\hat x_1=\mathrm{ReLU}(h) and x^2=ReLU(−h)\hat x_2=\mathrm{ReLU}(-h). With at most one feature active, the ReLU filters the interference and reconstruction is exact. With both active, both estimates err by the smaller value, and E[min⁡(u,v)2]=16\E[\min(u,v)^2]=\tfrac16 for independent uniforms:

  3. S>37≈0.43S>\frac37\approx0.43

    Superposition wins when (1−S)23<1−S3−(1−S)24\frac{(1-S)^2}{3}<\frac{1-S}{3}-\frac{(1-S)^2}{4}; divide by 1−S1-S to get 712(1−S)<13\tfrac7{12}(1-S)<\tfrac13:

  4. Only two hand-picked solutions are compared, not the optimum over all WW and b\mathbf b, but the mechanism is general: interference costs scale with how often features co-occur, (1−S)2(1-S)^2, while dropping a feature costs in proportion to how often it occurs, 1−S1-S. Sparse enough features are cheaper to share than to drop, so polysemantic neurons should be expected.

Predict before you look

At sparsity S=0.9S=0.9, what is the ratio Lsup/Lded\loss_{\text{sup}}/\loss_{\text{ded}} of the two solutions above?

Show the answer

0.11

Lsup=0.01/3≈0.0033\loss_{\text{sup}}=0.01/3\approx0.0033 and Lded=0.1/3−0.01/4≈0.0308\loss_{\text{ded}}=0.1/3-0.01/4\approx0.0308, a ratio of about 0.110.11: at 90% sparsity, sharing the dimension costs about a ninth as much as dropping a feature.

Sparse autoencoders: undoing superposition

If activations x∈Rd\mathbf x\in\R^d are sparse combinations of many feature directions, x≈∑jfjdj\mathbf x\approx\sum_jf_j\mathbf d_j with most fj=0f_j=0, recovering the directions is dictionary learning (sparse coding appears in Autoencoders & Latent Spaces). A sparse autoencoder (SAE) learns an overcomplete dictionary of F≫dF\gg d features:

f(x)=ReLU(We(x−bd)+be)∈RF,x^=Wd f(x)+bd,L=∥x−x^∥22+λ∥f(x)∥1\mathbf f(\mathbf x)=\mathrm{ReLU}\big(W_e(\mathbf x-\mathbf b_d)+\mathbf b_e\big)\in\R^F,\qquad\hat{\mathbf x}=W_d\,\mathbf f(\mathbf x)+\mathbf b_d,\qquad\loss=\norm{\mathbf x-\hat{\mathbf x}}_2^2+\lambda\norm{\mathbf f(\mathbf x)}_1
Decoder columns are kept at unit norm, so the penalty cannot be dodged by shrinking f\mathbf f while growing WdW_d. Each column dj\mathbf d_j is a candidate feature direction; the inputs that activate it most show what it means.
DerivationThe L1 penalty shrinks activations
  1. min⁡f≥0 (a−f)2+λf\min_{f\ge0}\ (a-f)^2+\lambda f

    Take one feature with a unit-norm decoder column, and let a=dj⊤xa=\mathbf d_j^\top\mathbf x be its ideal activation. The optimal code solves a one-dimensional problem:

  2. f⋆=max⁡(0, a−λ2)f^\star=\max\big(0,\ a-\tfrac\lambda2\big)

    Setting the derivative −2(a−f)+λ-2(a-f)+\lambda to zero and respecting f≥0f\ge0 gives soft thresholding: small activations are zeroed, and every active feature is underestimated by λ/2\lambda/2.

  3. TopK:f(x)=TopK⁡k(We(x−bd)+be)\text{TopK:}\quad\mathbf f(\mathbf x)=\operatorname{TopK}_k\big(W_e(\mathbf x-\mathbf b_d)+\mathbf b_e\big)

    This shrinkage biases reconstructions toward zero and ties sparsity and fidelity to a single knob. Later variants choose which features fire without penalizing their size: TopK SAEs keep the kk largest pre-activations (Gao et al., 2024), and gated and JumpReLU SAEs separate the decision to fire from the magnitude.

What SAEs found. Bricken et al. (2023) trained SAEs on the 512-neuron MLP layer of a one-layer transformer, with dictionaries of up to 131,072 features. Most features were far more interpretable than neurons, responding to things like DNA sequences, Arabic script or base64 text, and features split into finer ones as the dictionary grew. Templeton et al. (2024) scaled the method to the residual stream of a production model, Claude 3 Sonnet, with up to about 34 million features. They found multilingual and multimodal features for abstract concepts, including safety-relevant ones such as deception and sycophancy, and showed that clamping a feature steers behavior: amplifying a "Golden Gate Bridge" feature made the model bring up the bridge in almost every reply.

Limitations. An SAE is a model of the model. Its reconstructions leave unexplained error ("dark matter") whose substitution measurably raises the language model's loss; features depend on dictionary size and training run; some features absorb parts of others; and meanings are read off top activations, which can mislead. Evidence on whether SAE features beat simple baselines such as linear probes on practical tasks is mixed. Newer work replaces layers with sparse transcoders and traces attribution graphs through them, extending circuit analysis to large models one prompt at a time.

Key takeaways

  • Probes test what is decodable, attributions what an output depends on locally, and activation patching what is causally sufficient or necessary; integrated gradients satisfy completeness, ∑iIGi=F(x)−F(x′)\sum_i\mathrm{IG}_i=F(\mathbf x)-F(\mathbf x').
  • An attention-only transformer is a sum of paths: a direct path plus, per head, a QK circuit (where to attend) and an OV circuit (what to write). Composed heads give induction circuits, and full circuits are known for indirect object identification and modular addition.
  • Superposition stores more sparse features than dimensions because interference scales with co-occurrence (1−S)2(1-S)^2 and dropping with frequency 1−S1-S; polysemantic neurons follow.
  • Sparse autoencoders learn overcomplete feature dictionaries, and the L1 penalty shrinks activations by λ/2\lambda/2. They have recovered interpretable, steerable features in production models, but faithfulness, completeness and practical value remain open.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

What does the completeness property of integrated gradients guarantee?

  1. The attributions sum exactly to F(x)−F(x′)F(\mathbf x)-F(\mathbf x')
  2. Each attribution equals that feature's causal effect
  3. The attributions are all nonnegative
  4. The choice of baseline does not affect the result
Show the answer

A. The attributions sum exactly to F(x)−F(x′)F(\mathbf x)-F(\mathbf x')

By the fundamental theorem of calculus along the straight path, ∑iIGi=F(x)−F(x′)\sum_i\mathrm{IG}_i=F(\mathbf x)-F(\mathbf x'). Completeness constrains the total, not how it is divided, and the result depends on the baseline.

Question 2

You run a corrupted prompt but restore one head's output to its value on the clean prompt, and the logit difference for the correct answer recovers 80% of its clean value. What does this show?

  1. The head is the only component involved
  2. The head's output carries information that, in this setup, largely suffices to restore the behavior
  3. The head is irrelevant
  4. The head's attention weights explain the decision
Show the answer

B. The head's output carries information that, in this setup, largely suffices to restore the behavior

Restoring a clean activation into a corrupted run tests the sufficiency of that component's information. It does not show uniqueness: other components may carry the same information, and the result depends on how the prompt was corrupted.

Question 3

In the toy model of superposition, why can a network store more sparse features than it has dimensions?

  1. Because ReLU networks cannot represent orthogonal directions
  2. Because the loss ignores rare features
  3. Because the features are dense and correlated
  4. Because interference costs scale with co-occurrence, (1−S)2(1-S)^2, while dropping a feature costs in proportion to 1−S1-S
Show the answer

D. Because interference costs scale with co-occurrence, (1−S)2(1-S)^2, while dropping a feature costs in proportion to 1−S1-S

In the two-feature calculation, sharing a dimension beats dedicating it once S>3/7S>3/7. The ReLU removes negative interference, so features that rarely co-occur barely disturb each other.

Question 4

An SAE feature with a unit-norm decoder column has ideal activation a=1.0a=1.0, and the L1 coefficient is λ=0.4\lambda=0.4. What activation does the optimal code assign?

  1. 1.01.0
  2. 0.60.6
  3. 0.80.8
  4. 00
Show the answer

C. 0.80.8

Soft thresholding gives f⋆=max⁡(0,a−λ/2)=0.8f^\star=\max(0,a-\lambda/2)=0.8. Every active feature is shrunk by λ/2\lambda/2, a bias that TopK and JumpReLU SAEs avoid.

Further reading

  • N. Elhage et al., "A mathematical framework for transformer circuits," Transformer Circuits Thread (2021).
  • N. Elhage et al., "Toy models of superposition," Transformer Circuits Thread (2022).
  • T. Bricken et al., "Towards monosemanticity: Decomposing language models with dictionary learning," Transformer Circuits Thread (2023).
  • A. Templeton et al., "Scaling monosemanticity: Extracting interpretable features from Claude 3 Sonnet," Transformer Circuits Thread (2024).
  • L. Sharkey et al., "Open problems in mechanistic interpretability" (2025).