# A Mathematical Framework for Transformer Circuits

Author: Ishwar Jangid
Canonical URL: https://ishwarj.com/papers/transformer-circuits/
Updated: 2026-10-17
Source: https://github.com/ishwar6/ishwar-books/blob/main/content/papers/transformer-circuits/index.md


## Why this paper

Large language models work, but nobody wrote down the rules they follow. The rules are hidden inside millions or billions of learned numbers. In December 2021 a team at Anthropic published a long web article that asked: can we read those rules out of the numbers, the way a programmer reads a compiled program back into source code?

They started small. They took transformers with zero, one and two layers, kept only the attention part, and showed that each model can be written as a **sum of simple pieces**. Each piece can be read directly from the weights, without running the model. A zero-layer model is a table of word pairs (bigrams). A one-layer model adds "skip-trigrams" and a simple kind of copying. A two-layer model builds **induction heads**, a small circuit that lets a model continue a pattern it has seen earlier in the text. Induction heads became one of the best-known findings in interpretability.

The paper also gave the field its vocabulary: the **residual stream**, **virtual weights**, the **QK circuit** and the **OV circuit**, **path expansion**. Most later work on reading the inside of language models uses these words. The paper is long and dense, so this series reads it slowly, in its own order, and derives every equation by hand.

## How the parts are organised

1. **[The Big Idea and the Residual Stream](https://ishwarj.com/papers/transformer-circuits/part-1-the-residual-stream/)**: the introduction, the model simplifications, the model written as four equations, the residual stream as a communication channel, virtual weights, bandwidth, and the zero-layer transformer.
2. **[One Attention Head, Taken Apart: the QK and OV Circuits](https://ishwarj.com/papers/transformer-circuits/part-2-one-attention-head/)**: why heads are independent and additive, attention as information movement, the tensor-product form of a head, the two low-rank matrices $${W_{\mathrm{Q}}}^{\top} W_{\mathrm{K}}$$ and $$W_{\mathrm{O}} W_{\mathrm{V}}$$, and the QK and OV circuits read from real weights.
3. **[Paths Through the Model](https://ishwarj.com/papers/transformer-circuits/part-3-paths-through-the-model/)**: the path expansion trick, which writes a whole model as a sum of simple end-to-end terms.
4. **[One-Layer Models: Skip-Trigrams, Copying and Their Bugs](https://ishwarj.com/papers/transformer-circuits/part-4-one-layer-models/)**: reading the bigram and skip-trigram tables from the weights of a one-layer model.
5. **[Two-Layer Models: How Heads Compose](https://ishwarj.com/papers/transformer-circuits/part-5-two-layer-models/)**: Q-, K- and V-composition, and virtual attention heads.
6. **[Induction Heads](https://ishwarj.com/papers/transformer-circuits/part-6-induction-heads/)**: the two-head circuit behind in-context copying, and how the paper checks it.
7. **[What the Paper Left Out, and What Came After](https://ishwarj.com/papers/transformer-circuits/part-7-after-the-paper/)**: MLPs, superposition, dictionary learning and circuit tracing.

## How to read the boxes

- A **teal "From the paper" box** shows the exact words of the paper as a highlighted screenshot (click it to open it full size), followed by **what it says**, the main words of the paper, and **what it means**, an explanation of those lines and of what the authors mean but do not write.
- A **yellow definition box** explains a technical word the first time it appears. If you already know the word, skip the box.
- The small tag above a heading names the section of the paper you are reading, so you can follow along on the paper's web page.
- A **green "Key takeaways" box** ends every part with what to remember.

Code blocks show real code, and the output under them is what that code printed. The scripts are in [`code/papers/circuits`](https://github.com/ishwar6/ishwar-books/tree/main/code/papers/circuits). They use the open-source TransformerLens library and small public models, and they run on the CPU of an Apple M5 Pro (64 GB) in under a minute.

## What you need to know first

You need to know what a vector and a matrix are, and how to multiply a matrix by a vector. Everything else is explained when it first appears. It helps to know roughly what a transformer is and what attention does; the [attention series](https://ishwarj.com/writings/attention-1-self-attention/) explains both from zero. Part 1 needs almost none of it: attention itself is opened up in Part 2.

