How do large language models work?
What is a large language model?
On the surface, a large language model (LLM) does something deceptively simple: given the beginning of a text, it predicts the next piece of it. It receives a sequence of tokens — “The cat climbed up the” — and returns a probability distribution over every possible next token in its vocabulary: “tree” 62%, “stairs” 11%, “fence” 7% (illustrative numbers), and so on across tens of thousands of further options. Generating text is nothing but the repetition of that single operation: the model draws a token from the distribution, appends it to the text, then computes the next-token distribution for the extended text. In practice part of the earlier computation can be reused from a cache; the chapter on inference covers this. This is called autoregressive generation. This guide focuses mainly on such autoregressive transformer models that produce text token by token.
What the definition hides is this: to predict the next token well, you implicitly need to know a great deal. Continuing “Paris is the capital of France, and Berlin is …” takes geographical facts; continuing “if 3 apples cost $6, then 7 apples …” takes arithmetic; continuing a half-finished Python function takes programming. Next-token prediction is therefore not a narrow task: learning many linguistic and factual regularities can help with it. Over the course of training, the model's parameters can organize into internal patterns that represent grammar, facts, style and patterns of inference, because these improve prediction. Some studies have also found internal representations of the task's world: for example the state of a board game in a model trained only on move sequences, or directions for places and times in large language models (Li et al., 2023; Gurnee & Tegmark, 2024). This does not imply that every model forms a unified or human-like world model.
Why “large”?
“Large” refers to the number of parameters: the learned numbers that determine the model's behavior. A modern LLM has billions of them — open models typically range from ~1 billion to several hundred billion, and the largest systems go beyond that. The overwhelming majority of parameters sit in a handful of enormous matrices (embedding, attention and MLP weights; see chapter 4). The size is not an end in itself: in section 5.6 we will see that performance follows surprisingly regular scaling laws — more parameters and more data give predictably better prediction.
The life cycle of a model
A modern LLM is built in two large stages. During pretraining the model learns next-token prediction on an enormous corpus of trillions of tokens — this is where the raw capability comes from (chapter 5). Post-training then shapes it into a usable assistant: supervised fine-tuning, preference-based training (RLHF, DPO) and safety training (chapter 6). Running the finished model — inference — is an engineering field of its own, with its own tricks (chapter 7). Finally, chapter 8 is about what researchers still understand only partly: what actually happens inside.
Mathematics The model as a probability distribution — stated precisely
Formally, a language model is a parameterized distribution over token sequences. By the chain rule, the joint probability of any sequence x1, …, xT factorizes into a product of conditional probabilities:
The model — with parameters θ — computes exactly these conditional terms: at every position it produces a distribution over the vocabulary of size V, conditioned on the context (the prefix) so far. This factorization is exact, not an approximation; the modeling decision is that a neural network represents the conditional distribution, taking the prefix as input and returning a V-dimensional probability vector.
Two consequences matter. First, the training objective follows naturally: maximise the log-probability of the training corpus (this becomes the cross-entropy loss, 5.1). Second, generation is sampling from this distribution, and the sampling strategy (temperature, top-p) is a separate degree of freedom that is not part of the model at all (chapter 7).
Research A historical arc: why did the transformer win?
Several generations of architecture competed to represent that conditional distribution. N-gram models (dominant until the 2000s) simply counted how often a word follows the previous n−1 words — but the context length is fixed and short, and there is no generalization to combinations never seen. Recurrent networks (RNN, LSTM; ~2014–2017) compressed the entire history into a fixed-size hidden state: unlimited context in principle, with two fatal weaknesses in practice. Information has to “survive” many steps inside the hidden state (the gradient vanishes or explodes, 5.3), and processing is inherently sequential — step t cannot begin before step t−1, which runs against the parallel nature of GPUs.
The transformer (Vaswani et al., 2017, “Attention Is All You Need”) solved both problems at once. Attention creates a direct connection between any two positions — information does not have to travel step by step, and the gradient path between any two tokens is O(1) long. And because during training the prediction at every position can be computed simultaneously and independently (using the causal mask, 4.3), the entire sequence runs as one huge matrix operation on the GPU. The victory was therefore not merely a “better inductive bias” but a fit to the hardware: the transformer is the architecture that converts available compute into learning most efficiently. That consideration — compute efficiency as the primary design principle — has driven architectural progress ever since (4.8).
Five questions; four correct answers to pass. If you fall short you can retake it right away, as many times as you like — the questions and the options are shuffled every round.
Tokenization: from text to a sequence of numbers
A neural network does not see letters, it sees numbers. The first step is therefore to cut the text into the entries of a fixed vocabulary — these are the tokens — and then to replace each piece with its index in that vocabulary. The question is: how big should the pieces be?
Two extremes present themselves, and both are bad. If we tokenize character by character, the vocabulary is tiny (a few hundred entries), but the sequences become very long and the model would have to reassemble the meaning of every word from characters every single time. If we tokenize word by word, the sequences are short, but the vocabulary explodes (even in English, “play, plays, played, playing, player, players, replay, unplayable…” would each need a separate entry — and in morphologically richer languages the explosion is far worse), and every unfamiliar word — a new name, a typo, a rare compound — becomes unmanageable. The practical answer lies in between: subword tokenization, where frequent words get a single token and rare ones break into meaningful pieces. The dominant algorithm is byte-pair encoding (BPE).
The core idea of BPE is a compression principle: whatever frequently occurs together, merge into one symbol. The vocabulary is not written by hand but learned from data: we start from characters (more precisely, bytes) and repeatedly merge the most frequently adjacent pair in the corpus until the vocabulary reaches the desired size — typically somewhere between 32,000 and 260,000 entries.
strawberry? Because the model does not see letters: for it the word consists of tokens such as str+aw+berry, and the internal letter composition of a token is available only indirectly, learned from the training data. This is also why arithmetic is more reliable in models that tokenize digit by digit: 12345 as a single token is “atomic”, whereas as 1+2+3+4+5 the place-value structure becomes visible.
The vocabulary and special tokens
A vocabulary may contain learned subwords and reserved special token IDs for text endings, role boundaries or tool calls. Typing a marker as text is different from inserting the actual control token. The tokenizer API and chat serializer determine when special tokens are allowed; BPE alone does not guarantee this separation. Untrusted input must be encoded as content rather than as a role boundary. This alone does not prevent prompt injection. Example: tiktoken allowed_special / disallowed_special. A larger vocabulary may shorten sequences but increases the embedding table.
Mathematics The BPE algorithm precisely, and the main alternative
Building the vocabulary (training time):
# corpus: byte sequences; goal: a vocabulary of size V
vocab = {all 256 bytes}
merge_rules = []
while |vocab| < V:
pair = the most frequent adjacent (x, y) token pair in the corpus
new_token = concat(x, y)
vocab.add(new_token)
merge_rules.append((x, y) → new_token)
corpus = replace every occurrence of (x, y) with new_token
Tokenizing (use time): we break the input into bytes, then apply the merge rules in the order they were learned, for as long as they apply. That order is what makes the result deterministic. In practice the split is preceded by a regular-expression pre-segmentation (separating words, numbers and punctuation) so that merges cannot cross word boundaries.
BPE starting from all 256 bytes can represent every byte of encoded text, avoiding unknown tokens at the vocabulary level. An API may still require valid Unicode text and perform preprocessing: this does not mean arbitrary binary data can be passed in unchanged. Not every tokenizer is byte-level BPE; SentencePiece supports both BPE and unigram models.
Research Research angles: glitch tokens, linguistic inequality, token-free models
Rare and unusual tokens. A token with little direct training exposure can trigger erroneous or unusual behavior. A mismatch between tokenizer and language-model training is a possible cause, not a proven explanation for every case. Rarity alone does not establish that an embedding remains unchanged initialization noise: tied output weights, weight decay and optimizer state may modify it. Investigate the specific model and token empirically.
Fertility and linguistic fairness. A vocabulary built mostly on English tokenizes English compactly and other languages wastefully — the same content in Hungarian can take substantially more tokens (higher “fertility”), and its agglutinative morphology is often broken at points that are not morpheme boundaries. This is not only a cost question: a longer token sequence means less effective context and a diluted per-token learning signal. Newer, larger vocabularies (100k–260k) built on multilingual corpora reduce this considerably.
Do we need a tokenizer at all? Replacing tokenization is an active research direction: byte-level models (ByT5), and dynamic, learned segmentation in which the model itself decides where to draw the boundaries (the Byte Latent Transformer, for instance, forms variable-sized “patches” based on predictability — entropy). The motivation is that the BPE vocabulary and merge rules are learned separately from data, but ordinary gradient-based language-model training does not update the discrete tokenization procedure. Its fixed granularity contributes to several difficulties above; it is not their only possible cause. The price is the compute demanded by longer sequences — the open question is when hierarchical architectures will make that back.
Five questions; four correct answers to pass. If you fall short you can retake it right away, as many times as you like — the questions and the options are shuffled every round.
Embeddings: meaning as a direction in a vector space
Token IDs on their own are meaningless serial numbers: token 9042 is no more “similar” to 9043 than it is to 517. The network needs a representation in which similarity is computable. So we assign every token a learned vector — a sequence of several thousand numbers — and from then on the model works with these vectors rather than with tokens. This assignment is called the embedding.
The decisive point: embeddings are not designed by hand but learned by the model, with the same gradient method as every other parameter. Under the pressure of training the geometry starts to carry meaning: tokens that occur in similar contexts — and therefore have to contribute similarly to similar predictions — receive vectors close to one another. The vectors for “dog” and “cat” end up near each other; “dog” and “integral” far apart. What is more, the directions carry meaning too: moving along a particular direction changes grammatical number, gender, tense, or even formality.
How large is this space?
The length of the embedding vectors is the model's hidden dimension, dmodel — a few hundred in small models, around 4,000–20,000 in large ones. The embeddings of all tokens form a single table: a matrix of size V × dmodel (where V is the vocabulary size). With a 128k vocabulary and a dimension of 8,192, that alone is more than a billion parameters. An important look ahead: this vector is only the starting point — the dictionary meaning of the token. The transformer layers (chapter 4) gradually rewrite it into the contextual meaning: the vector of the token “bank” becomes something different after passing through the layers in “on the river bank” than in “the bank approved the loan”.
Mathematics The embedding as matrix multiplication, and measuring similarity
The embedding table is the matrix WE ∈ ℝV×d. If we encode the i-th token with the one-hot vector ei (all zeros with a single 1 in position i), then the embedding is formally a matrix multiplication:
In practice this is of course implemented as a row lookup, but the matrix view matters: it makes clear that WE is a learnable weight matrix like any other, and that the gradient from the input lookup touches only rows used in the current batch. With tied input and output weights, the output softmax also sends gradients to other rows; optimizer state and weight decay can affect updates too.
The standard measure of similarity between two vectors is cosine similarity — the agreement of directions, independent of length:
It is close to 1 when the two directions are nearly identical, and around 0 when they are (nearly) orthogonal. High dimensionality is the key player here: in d dimensions there is room for exponentially many nearly orthogonal directions — which is why a 10,000-dimensional space has room, in principle, for hundreds of thousands of well-separated directions. This fact is also the basis of the superposition hypothesis in section 8.4.
A note: on the output side of the model sits a similar “unembedding” matrix WU (4.7), which projects the internal vector back onto the vocabulary. In smaller models the two are often shared (weight tying, WU = WE⊤), which saves parameters and regularizes; large models typically train them separately.
Research The fine structure of embedding geometry
Anisotropy. The naive picture — vectors scattered evenly — is empirically false: learned embeddings typically concentrate in a narrow cone, and the average pairwise cosine similarity sits well above zero. Part of the distortion comes from frequency (the norm and placement of frequent tokens differ systematically from those of rare ones), part from the dynamics of optimization. As a result raw cosine similarity can mislead, and in representation analysis it is common to subtract the mean vector first, or to whiten the space.
How much “knowledge” is already in the embedding? Less than one would think. A static embedding compresses only the token's distributional profile; resolving polysemy (every sense of “bank” in a single vector), syntactic role and coreference are all the work of the layers. On the modern view (8.2) the embedding is the initial state of the residual stream: the first message on a communication channel that every layer reads and overwrites.
The linear representation hypothesis. The generalization of the “meaning ≈ direction” observation is the claim that the model's internal features are, to a good approximation, encoded as linear directions, and that the layers perform linear-algebraic operations on them. This is the working hypothesis of mechanistic interpretability today — with strong empirical support (direction arithmetic, linear probes, SAE results, 8.5), but also with known counterexamples: circular, multidimensional representations have been found (the days of the week on a circle in a plane, for instance) that cannot be described by a single direction.
Five questions; four correct answers to pass. If you fall short you can retake it right away, as many times as you like — the questions and the options are shuffled every round.
The transformer architecture
4.1 · The block: attention and MLP
The transformer is the engine of modern LLMs. Its structure is surprisingly repetitive: the same block repeats one after another, a few dozen or a few hundred times. Every block consists of two sub-layers: an attention layer, responsible for the flow of information between tokens — the only place where positions of the same sequence can “talk” to one another — and an MLP layer, which processes every position on its own, but with large capacity. The division of labor between them is the heart of the whole architecture: attention moves information, the MLP processes it.
4.2 · The residual stream: the backbone of the architecture
Before going into the details of attention, it is worth fixing the best mental model of the whole. Every token position carries a dmodel-dimensional vector that receives additive updates through the layers — this is the residual stream. The blocks do not “process and pass on” the data like a conveyor belt: instead, every sub-layer reads the current contents of the stream, computes something useful from it, and adds the result back to the stream. Moving toward the output, the stream carries the contributions of more and more layers, as a sum. Additions can also cancel previous components: information does not necessarily accumulate or improve monotonically.
This seemingly minor design decision — the residual connection — matters in three ways. Trainability: addition provides an identity path for gradients, helping optimize deep networks without guaranteeing stable training. Incremental updates: each sub-layer updates the existing state, potentially reinforcing or cancelling earlier components. Interpretability: the additive structure helps separate component contributions; this is one important tool in chapter 8.
4.3 · Attention: which token should read from where?
Attention moves information between tokens, but causal direction matters. In “The crane nested high”, the position of “crane” cannot yet see the later word “nested”. The position of “nested” and subsequent positions can combine the earlier “crane” with evidence for the bird meaning. Attention can help through learned retrieval. Each position has three roles:
- Query — “what am I looking for?”: the token states what information it needs.
- Key — “what do I offer?”: every token advertises what information it can provide.
- Value — “what do I hand over if chosen?”: the content actually to be passed on.
The mechanism: the token's query vector is compared with the key vectors of the current and all earlier tokens (by a dot product: q · k = ‖q‖ ‖k‖ cos θ, so both direction and vector lengths matter). Wherever the match is strong, a large weight goes; softmax normalizes the weights into a distribution; and finally the token receives the weighted average of the value vectors and adds it to its own stream. So it does not read from a single addressee but from everyone at once, in proportion to relevance.
The causal mask and multi-head attention
Two important additions. First: since the task is predicting the next token, a position may read only from the past — attention toward future tokens is forbidden by the causal mask (before the softmax we write −∞ into the scores of future positions). This is what lets training practice prediction at every position at once: the output at position t depends only on the first t tokens, so a single forward pass yields T training examples.
Second: attention does not run once but many times in parallel — this is multi-head attention. Every head works with its own query/key/value projections in a smaller subspace, so different heads can specialize in different relations: one attends to the immediately preceding token, another to the subject of the sentence, a third to earlier occurrences of the same word (this will be the induction head of 8.3). The outputs of the heads are concatenated and returned to the stream through a shared output projection.
Mathematics The full mathematics of attention
Let X ∈ ℝT×d be the matrix of the stream vectors of the T positions. One head works with three learned projections, WQ, WK, WV ∈ ℝd×dh, where dh = d/H is the head dimension (H being the number of heads):
The output of the head is the famous formula:
where M is the causal mask: Mij = 0 if j ≤ i, and −∞ otherwise. The softmax runs row by row, so row i is a probability distribution over positions 1…i.
Why divide by √dh? If the components of q and k are independent, zero-mean variables with unit variance, the standard deviation of their dot product grows with √dh. At large dh the spread of the scores would be such that the softmax saturates to practically one-hot — and there the gradient vanishes. The scaling keeps the scores in the “sensitive” range of the softmax.
Concatenating the heads. The outputs of the H heads are concatenated and projected back with a learned matrix WO ∈ ℝd×d:
Cost. For full attention, QK and AV products together require O(T²d) arithmetic. Naively stored attention matrices use O(HT²) elements across H heads; Q/K/V arrays use O(Td). Projections separately cost O(Td²). FlashAttention avoids storing the full T × T matrix while retaining the quadratic arithmetic of dense attention. KV caching reuses previous K/V computations during token-by-token decoding.
Research The QK and OV circuits: two analyzable parts of attention
Two useful weight products in an attention head are QK and OV. This simplified account uses row vectors; X is the actual projection input, already normalized in a pre-norm block. Biases, RoPE and QK normalization are omitted here. The two parts jointly determine the output; full attention is not two independent linear functions.
The QK circuit: sij = xi(WQWK⊤)xj⊤/√dh. The d × d product defines a bilinear score between two row vectors; masking and softmax turn scores into weights. In this simplified setting, replacing the projections by WQR, WKR−⊤ for invertible R preserves scores. An arbitrary change of basis generally ceases to be a symmetry when RoPE or QK normalization is present.
The OV circuit: multiply this head’s WV ∈ ℝd×dh by its row block of the full output projection, WO(h) ∈ ℝdh×d. Thus WVWO(h) ∈ ℝd×d. This maps source content into a residual-stream update; attention weights combine contributions from different source positions.
A consequence: the head's “moving” operation is linear in the value content; when attention weights are held fixed. The full input-dependent operation is not linear: QK scores are bilinear and softmax transforms the weights nonlinearly. This is why compositions of heads — one head's output modifying another's query or key (Q-, K- and V-composition) — can be studied as algebraic objects, as so-called virtual weights. The discovery of the induction head (8.3) fell out of this framework.
Further research observations: the attention sink phenomenon (heads “park” surplus weight on the first token, because the softmax must always sum to 1 — newer architectures therefore add a learned sink token or an “off” option); weights are not explanations (a large attention weight does not necessarily mean causal importance — ablation tests often refute it, 8.6); and positional heads (many heads learn a purely positional pattern: previous token, start of sentence, rhythmic offsets).
Five questions; four correct answers to pass. If you fall short you can retake it right away, as many times as you like — the questions and the options are shuffled every round.
4.4 · The MLP layer: the processing and knowledge unit
The other half of the block, the MLP (multi-layer perceptron, also called the feed-forward layer), runs at every position separately, with the same weights — no new information flows directly between positions here. Its structure is simple: a large matrix projects up the stream vector into a much wider space (typically about 4× the model dimension), an element-wise nonlinearity acts on it there, and another matrix projects it back down into the original dimension. Most of the parameters — roughly two thirds — sit in these matrices.
What is it for? In a conventional two-projection MLP, removing the activation collapses the projections into one affine map (linear when there are no biases). This reduces the MLP's expressiveness; the full transformer remains nonlinear, because attention and normalization remain nonlinear. Gated variants also introduce nonlinearity through multiplication. The original architecture describes these as separate components. More intuitively: the MLP can also be read as a key–value memory. In the row-vector convention below, each column of the up-projection matrix is a pattern detector (a key): to what degree is a particular combination of features present in the input? The nonlinearity modulates detector activations — and the corresponding row of the down-projection matrix (the value) writes the content belonging to the activated detectors into the stream. Many factual associations — “Eiffel Tower → Paris” — are demonstrably stored in the MLPs of the middle layers.
The key–value expression is algebraically exact, but attaching human meaning is an interpretation. Neuron input-weight vectors are concrete parts of the matrix; nevertheless, a neuron can respond to multiple patterns and a concept can be distributed across components. Superposition is one researched explanation. A mathematical “key” should not be equated with a unique human concept.
Mathematics Formulas: from GELU to SwiGLU
The classic form, with two matrices (Win ∈ ℝd×4d, Wout ∈ ℝ4d×d):
The nonlinearity today is typically not ReLU but its smooth variant, GELU: GELU(x) = x·Φ(x), where Φ is the standard normal cumulative distribution function — it does not cut sharply to zero at small negative values, which gives smoother optimization. Most modern models use a gated variant, SwiGLU, with three matrices:
where Swish(x) = x·σ(x) and ⊙ is element-wise multiplication. The gating allows a multiplicative interaction between two projections — empirically it consistently gives a better loss at the same parameter count (the hidden dimension is then taken to be about 8d/3, so that the three matrices add up to the same total).
With row vectors, the key–value expression is MLP(x) = Σn an(x)wout,n, where an(x) = GELU(xwin,n). Here win,n is column n of Win, and wout,n is row n of Wout. Biases are omitted. The weighted sum is an exact identity; interpreting individual neurons as human concepts is a separate empirical question.
Research Storing and editing knowledge; polysemantic neurons
Work aimed at localizing “factual recall” (Geva et al. 2021 — the MLP as key–value memory; Meng et al. 2022 — ROME/MEMIT knowledge editing) shows that an individual association can be edited in a targeted way: with a low-rank modification of mid-layer MLP weights the model can be made to believe that “the Eiffel Tower stands in Rome” — while the surrounding knowledge stays largely intact. The localization is not clean, however: causal tracing indicates that the fact lives smeared across several layers, and the side effects of edits (ripple effects) remain an open research problem.
The main obstacle to neuron-level interpretation is that most individual MLP neurons have no single meaning: the same neuron fires for, say, academic citations, English dialogue and HTTP requests — this is polysemanticity. According to the superposition hypothesis of section 8.4, one possible reason is a compression strategy: the model may store more features than it has neurons, in non-basis directions. This is why the field moved from neurons to learned dictionary directions (SAEs, 8.5).
4.5 · Normalization: keeping the signal in shape
Normalization helps control the scale of sublayer inputs. LayerNorm subtracts the component mean and divides by √(variance + ε); RMSNorm divides by √(mean(x²) + ε) without centering. A learned elementwise gain follows, usually with a bias for LayerNorm. Pre-norm places normalization before the sublayer, inside the residual branch. The direct residual path remains an identity, which can improve gradient flow. Xiong et al. obtained good results without warmup in certain settings; this is not a general stability guarantee, and warmup requirements depend on the full training recipe.
4.6 · Positional encoding: how does the model know what is where?
Without positional information or a mask, self-attention is permutation equivariant: permuting input rows permutes output rows in the same way. Both rows and columns of the attention matrix are reordered; the matrix does not remain unchanged. Explicit positional encodings provide location or distance information. A causal mask itself already introduces an ordering asymmetry by restricting which positions are available.
First-generation solutions added position as a vector to the embedding (a fixed sinusoidal pattern or a learned positional embedding). Today's standard is RoPE (rotary position embedding), which is more elegant: it adds nothing to the content but rotates the query and key vectors by an angle proportional to their position. The dot product of two rotated vectors depends on position solely through the difference of the two rotation angles — formally ⟨Rmq, Rnk⟩ = ⟨q, Rn−mk⟩ — that is, the attention score automatically senses the relative distance of the tokens rather than their absolute places. (The value of the dot product of course also depends on the two vectors themselves; the point of the claim is that position enters only through the difference m−n — the rotation is orthogonal, so it leaves the norms untouched.) This is exactly the property language calls for: the relation “next to each other” means the same at the start of a text as in the middle.
Mathematics RoPE with formulas
The dh-dimensional query/key vector is split into coordinate pairs, and pair i (treated as a 2D plane) is rotated at position m by the angle mθi, where the frequencies form a geometric series:
The rotation is orthogonal, hence ⟨R(mθ)q, R(nθ)k⟩ = ⟨q, R((n−m)θ)k⟩ — the score really does depend only on the difference n−m. Together, the many frequencies form a kind of multi-band “clockwork”: the fast bands give fine resolution among neighboring tokens, the slow ones coarse resolution over long distances.
A practical consequence: the context window can be stretched. If a model was trained on 8k tokens, rescaling the RoPE frequencies (position interpolation, NTK-aware scaling, YaRN) can extend it to longer sequences than it was trained on, with relatively little post-training — one of the key techniques behind long-context models.
4.7 · The output head: from vector to probability
After the last block, the stream vector passes through final normalization and the unembedding matrix (WU ∈ ℝd×V). Each token’s logit is a dot product with the corresponding matrix column, plus an output bias if used. Dot products depend on both direction and magnitude: the highest cosine similarity alone need not give the highest logit. Softmax converts logits into probabilities:
Exponentiation gives positive weights, and normalization makes the probabilities sum to 1. Softmax turns logit differences into ratios: pᵢ / pⱼ = exp(zᵢ − zⱼ). Increasing one logit by 1 multiplies that token's probability ratio relative to each other token by e (~2.7×), not its own probability. With two initially equal logits, its probability goes from 50% to e / (e + 1) ≈ 73%. Chapter 7 lets you also explore temperature.
4.8 · Modern variants: what has the industry changed?
The core of the 2017 recipe is surprisingly stable, but almost every component has been optimized since. The most important directions:
| Technique | What does it solve? | How? |
|---|---|---|
| GQA / MQA (grouped-/multi-query attention) | The memory footprint of the KV cache at inference time (chapter 7) | Many query heads share a few key/value heads — the size of the cache shrinks in proportion to the number of KV heads, with minimal loss of quality. |
| MoE (mixture of experts) | Decoupling parameter count from per-token computation | Instead of one MLP, many “expert” MLPs; a learned router activates only a few per token. Enormous total capacity, small compute per token — the typical choice of modern large models. |
| Sparse / sliding-window attention | The T² cost of attention | Some layers attend only within a local window; a few global layers maintain long-range connections. Often alternating layer by layer. |
| FlashAttention | The memory-bandwidth limit of attention | Not an architectural redesign but a kernel one: the T×T matrix is never written out to slow GPU memory — it is computed tile by tile, on chip. Exact result, several-fold speedup. |
| SSM hybrids (e.g. the Mamba line) | Linear-time sequence processing | State-space models: rolling a fixed-size learned state along the sequence. In pure form, exact retrieval is weaker; in practice hybrids mixed with attention are spreading. |
Five questions; four correct answers to pass. If you fall short you can retake it right away, as many times as you like — the questions and the options are shuffled every round.
Training: where do the weights come from?
The weights of a freshly initialized model are random numbers and its output is meaningless noise. The aim of training is that out of this noise — purely from examples and a measure of error — the good parameters of the machinery in chapter 4 should emerge. The principle of the recipe is simple, and essentially all of deep learning is built on this one loop:
- Take a batch of text from the corpus and run the model on it.
- Measure with a loss function how bad the prediction was.
- Compute, for every single parameter, how much a small change to it would improve the loss — this is the gradient, and backpropagation computes it.
- Move every parameter a little in the direction of improvement (the optimizer).
- Repeat — for large models, over many millions of steps and many trillions of tokens.
5.1 · The loss function: cross-entropy
What does “a bad prediction” mean? At every position the model produced a distribution over the next token — and the corpus tells us what the next token actually was. The cross-entropy loss is simply the negative logarithm of the model's probability for the correct token: if the model gave 90% to the correct token, the loss is small (−ln 0.9 ≈ 0.105); if it gave 1%, the loss is large (−ln 0.01 ≈ 4.6). Training therefore literally optimizes for the texts that were actually written to be as probable as possible according to the model — this is the maximum likelihood principle.
Mathematics The exact form of the loss
For a training sequence x1…xT of length T, the loss is the average over all positions:
During training, shifted inputs and targets from a known sequence allow predictions at all positions in parallel; the causal mask prevents copying from the future. For a one-hot target, H(y,p) = −Σ yi ln pi is the observed token’s negative log probability. The expected cross-entropy under the true data-generating distribution P is H(P,Q) = H(P) + KL(P‖Q), so it is at least H(P). This does not impose the same bound on one example or the measured loss of a finite training sample; a small corpus can be memorized. Natural logarithms give nats/token; base-two logarithms give bits/token.
5.2 · The computational graph and automatic differentiation
To compute the gradient, notice that the loss is a function of the parameters — a monstrously complex one, but built out of elementary steps (matrix multiplication, addition, softmax…). Modern frameworks (PyTorch, JAX) record this structure during the forward pass into a computational graph: every node is an operation, every edge a data dependency. Computing the gradient is then mechanical: moving backwards through the graph, each operation multiplies the incoming gradient by its own (known, simple) derivative. This is automatic differentiation (reverse-mode autodiff) — backpropagation is its name in the neural-network world.
5.3 · Backpropagation: industrializing the chain rule
The essence of backprop is the chain rule, familiar from school: the derivative of a composite function is the product of the partial derivatives. In the computational graph, a long chain of operations stands between the loss and a deep parameter — and the gradient multiplies its way back along that chain. The genius is in the organization: instead of walking the chain separately for every parameter (which would be one forward pass per parameter — unworkable at a billion parameters), moving backwards shares the common partial products: the gradient at every node is computed once, and every parameter picks up its own at its own node. This is how all the gradients together come out for roughly the price of two forward passes.
Mathematics Derivation: the gradient of softmax + cross-entropy
The most famous and most elegant partial result is the gradient of the output layer. Let zi be the logit, pi = ezi/Σjezj the softmax output, c the index of the correct token, and the loss L = −log pc. Written out:
Differentiate with respect to zi. The derivative of the first term is −1 if i = c, and 0 otherwise. That of the second term:
Putting it together, with a one-hot target (yi = 1 if i=c):
The gradient is therefore literally the difference between the prediction and reality. If the model gave 0.51 to the correct token, the gradient there is −0.49: “push further up”. Every wrong token receives exactly as much downward push as the probability it received. It is numerically kind as well: it contains no division by a near-zero number, and softmax combined with log is stable (the log-sum-exp trick).
At branches, gradient contributions arriving at the same variable are added. For vectors, local derivatives are Jacobians; autodiff computes the appropriate vector–Jacobian products. A general graph is not a single chain of scalar products.
The rules for matrix layers. Through a linear layer Z = XW, the gradient backwards (writing G = ∂L/∂Z):
These two formulas are the workhorse of the entire transformer backward pass: the backward run is the same sequence of matrix multiplications as the forward one — only with transposed weights. The classic cost estimate comes from here too: the forward pass ≈ 2ND FLOPs (N parameters, D tokens, multiply plus add), the backward pass twice that (two matrix products per operation: the input gradient and the weight gradient) — training in total C ≈ 6ND. This formula is the currency of the scaling laws (5.6).
Research The vanishing gradient, and why the transformer is trainable
Backpropagation paths contain products of Jacobian matrices. They can contract some directions and amplify others, producing vanishing or exploding gradients. RNNs require many sequential transitions between distant positions; gated variants partly address this. In a general nonlinear network, one weight matrix’s spectral radius does not determine the outcome by itself: activations, inputs and changing local derivatives also matter.
The transformer's three structural answers:
(1) Residual connections. For xℓ+1 = xℓ + F(xℓ), the local Jacobian is I + JF. Across layers, these Jacobians are still multiplied. Expanding the product yields an identity term and paths of different lengths; the direct path can help gradients propagate. It does not replace the entire product with a simple sum, and cancellation or amplification by other terms remains possible.
(2) Normalization. The pre-norm arrangement brings the input of every sub-layer to a standard scale, so the gain of the sub-layers stays controlled even as depth grows.
(3) Short attention paths. Between any two positions, information (and the gradient) can travel in a single attention hop — there is no T-step chain as in an RNN; learning a long-range dependency does not require a long chain of products.
Residual instabilities remain even so — loss spikes, softmax/attention logits running away, low-precision numerics (5.5) — and gradient clipping (capping the gradient norm), QK-norm, z-loss and their relatives exist to handle them. In the practice of large runs, stability engineering is a profession of its own: during a training run of several months, divergence has to be spotted, checkpoints restored, and data or learning rate adjusted.
Five questions; four correct answers to pass. If you fall short you can retake it right away, as many times as you like — the questions and the options are shuffled every round.
5.4 · Optimizers: from the gradient to the step
AdamW maintains two exponential moving averages per parameter: the gradient and its square. The first provides momentum; the second supports coordinatewise scaling. Step size still depends on the learning rate, these statistics and ε: updates are not uniformly unit-sized, and good conditioning or convergence is not automatic. SGD remains a valid method, although adaptive optimizers are often more practical for large language models.
Mathematics AdamW with formulas, and the learning-rate schedule
Let gt denote the gradient at step t. Adam's update:
One common setting is β₁ = 0.9, β₂ = 0.95 and ε = 10⁻⁸; these are not universally optimal values. The final term is decoupled weight decay, shrinking weights separately from the gradient. For N parameters, two N-element moment estimates are stored. This is twice the weight storage in bytes only when their data types match: BF16 weights use 2 bytes/parameter, while two FP32 states use 8 bytes/parameter. Gradients, activations and optional FP32 master weights add further storage.
The learning rate η is not constant: at the start there is warmup (a linear ramp from zero over a few thousand steps — until the Adam statistics and the fresh network stabilize), then a long cosine decay, or more recently a constant phase plus a short, aggressive cooldown (the WSD schedule). The drop in loss during the decay is characteristic: smaller steps allow the fine structure to be polished.
Muon approximately orthogonalizes momentum updates for matrix-shaped parameters, for example using Newton–Schulz iterations. It does not explicitly compute a Hessian; the name Newton–Schulz does not automatically make it a second-order optimizer. Some experiments report better efficiency than AdamW, rather than a universal ranking. Other parameter groups often use AdamW alongside it. Original method description.
5.5 · Training in practice: data, precision, parallelism
All of the above works on a single GPU — but training a large model is an engineering operation running on tens or hundreds of thousands of accelerators for months. The main topics:
Data. The quality of the multi-trillion-token corpus (web crawl, books, code, scientific text, and increasingly synthetic data) is the number one determinant of the end result. The pipeline: language identification, quality filtering (heuristics and learned classifiers), deduplication (duplicates cause memorization and distorted capabilities), PII and toxicity filtering, and then careful design of the mixture ratios — toward the end of training often a “curriculum”: over-weighting the highest-quality data.
Precision. bfloat16 uses 1 sign bit, 8 exponent bits and 7 stored fraction bits; float32 uses 1, 8 and 23 respectively. Their exponent ranges match, but bfloat16 has lower precision and half the storage. It does not have “half the mantissa”. Mixed-precision training can use different formats for matrix operations and sensitive accumulations, including FP8. Speedups depend on hardware and operations rather than being automatically 2–4×. Loss scaling is particularly relevant to FP16’s narrower range. BFLOAT16 study.
Parallelism. A large model fits on a single GPU neither in weights nor in activations, so the work is sliced along three axes, typically all three at once (“3D parallelism”):
| Axis | What does it split? | Communication |
|---|---|---|
| Data parallel | The examples of the batch — every replica runs the full model on different data | Gradient summation (all-reduce) at every step; ZeRO/FSDP also distributes the weights and the optimizer state across the replicas |
| Tensor parallel | Individual matrices, cut between GPUs (heads and the columns of the MLP can be split) | Two all-reduces per layer — worth it only over a fast, within-node interconnect |
| Pipeline parallel | The layers, placed on different GPUs as stages | Passing activations between stages; the “bubble” (idle time) can be reduced by micro-batch scheduling |
Memory tricks. Backprop requires keeping the intermediate activations of the forward pass — at long sequences this dominates memory. Gradient checkpointing strikes a bargain: it stores the activations of only every k-th layer and recomputes the rest during the backward pass (about a third more computation, a fraction of the memory).
5.6 · Scaling laws: the physics of the field
In many experiments, test loss is well approximated by power laws in model size, data and compute. With a nonzero floor E, the ideal straight line on logarithmic axes applies to excess loss L − E. Fits support planning within the studied data, architecture and scale ranges; extrapolation adds uncertainty. A lower loss does not automatically establish success on a particular new task.
Mathematics Kaplan, Chinchilla and the “compute-optimal” recipe
The form of the laws (in the parametrization of Hoffmann et al., 2022 — “Chinchilla”):
Here N is parameter count, D is training tokens, and E is a fitted loss floor, not an independently measured entropy of language. Under the dense-model approximation C ≈ 6ND (when long-sequence attention does not dominate), the optimum gives Nopt ∝ Cβ/(α+β) and Dopt ∝ Cα/(α+β). The reported fit α ≈ 0.34 and β ≈ 0.28 therefore yields exponents 0.45 and 0.55, not exactly 0.5. Other estimates in the Chinchilla paper suggest nearly equal scaling rates. Roughly 20 tokens/parameter is a useful historical rule of thumb, not a universal constant; compute is not two additive costs split in half.
An important subtlety: the Chinchilla optimum minimizes training compute. If the model is then run a great deal, the cost of inference pushes the optimum toward the smaller model: this is why, in practice, many models are trained “too far”, well beyond 20 tokens per parameter (for example, hundreds or thousands of tokens per parameter) — the efficiency lost in training is won back in serving. The laws also sharpened the question of the data wall: high-quality web text is finite, and the way forward is synthetic data, multi-epoch reuse, and research into data quality.
Research The emergence debate and test-time scaling
Emergent abilities? Benchmark performance often appears not smoothly but seemingly in jumps as size grows (multi-step arithmetic, certain reasoning tasks). How much of this is an artifact of measurement is disputed: Schaeffer et al. (2023) showed that many “jumps” disappear if the discrete, sharp metric (exact match) is replaced by a smooth one (token-level log-likelihood) — the underlying ability builds continuously, and only the thresholded measure jumps. At the same time, genuine breakpoints have also been documented in loss space (for example the phase transition during training tied to the formation of induction heads, 8.3), so the picture is nuanced: the smoothness of scaling holds for the average loss, while at the level of individual circuits there can be sharp transitions.
Test-time computation. More attempts, search, verification and longer reasoning traces can improve some tasks. Gains depend on the model, question difficulty and final-answer selection. Accuracy does not increase indefinitely or universally log-linearly with compute; additional work may saturate or follow an erroneous path. Training and test-time budgets should be measured separately.
Five questions; four correct answers to pass. If you fall short you can retake it right away, as many times as you like — the questions and the options are shuffled every round.
Fine-tuning and alignment: from raw model to assistant
The pretrained “base model” has formidable knowledge but is not an assistant: asked “How do I bake bread?”, it may well answer with a list of further questions — because on the web, questions often continue that way. The base model continues what is likely, not what is useful. The job of post-training is to close that gap: to shape the model's behavior to be helpful, honest and harmless — without damaging the raw capabilities.
The three stages
1 · Supervised fine-tuning (SFT). The model is trained further on high-quality example dialogues — with the same next-token loss as before, only now on examples of “correct behavior”, and with the loss typically computed on the assistant's response tokens alone. This is what establishes the role: a question is followed by an answer, not by another question.
2 · Reward model. The notion of a “good answer” is hard to write down as rules, but people find it easy to pick the better of two answers. From such pairwise judgments a separate model learns — the reward model — which assigns a score to any answer. It is a compressed, machine substitute for human taste.
3 · Reinforcement learning. The model now learns from its own generated answers: it responds, the reward model scores, and the parameters shift so that highly scored behavior becomes more likely. A critical component is the KL brake: the model is penalized for drifting too far from the starting (reference) model — without it the policy finds the reward model's weak spots and exploits them (reward hacking: confident nonsense, flattery, artificial length).
Mathematics The objectives: RLHF and DPO
Training the reward model. Under the Bradley–Terry model, the probability that people prefer answer yw over yl is the sigmoid of the score difference. The reward model's loss:
The RLHF objective. The goal of policy optimization (typically PPO, or the simpler, critic-free GRPO):
DPO (direct preference optimization). The key observation: the optimum of the objective above can be written in closed form, and substituting it back, the reward can be eliminated — the policy can be optimized directly on the preference pairs:
DPO increases the difference between preferred and rejected log probabilities relative to the reference. It does not guarantee that the preferred answer’s absolute probability increases on every update. In the derivation, β is linked to KL regularization; it also affects gradients and tuning, rather than acting as a simple quality switch. Standard offline DPO needs neither a separate reward model nor an online sampling loop. Original DPO paper.
In DPO, reward is implicit in the policy/reference log ratio. Its difference-based loss can decrease even when both answers become less probable, provided the rejected answer decreases more. Preference-data bias, response length and overfitting require separate evaluation; longer answers do not necessarily win. Iterative data collection or alternative objectives may help, but there is no general rule that a single offline DPO stage is unusable.
Both families are alive in industry: DPO (and its relatives IPO, KTO, SimPO) is cheap and robust; online RL (PPO/GRPO) scales better with fresh data targeted at the policy's own mistakes — and reasoning training with verifiable rewards (math/code) is fundamentally online RL.
Research Constitutional AI, RLAIF and the open problems
RLAIF and Constitutional AI. Human labeling is expensive and noisy — the route to scale is for the judge to be a model as well. In Anthropic's Constitutional AI approach, the critique and the preference judgment are guided by an explicit list of principles (a “constitution”): the model criticizes and revises its own answer in the light of the principles (the SFT phase), and then the preference data is produced from AI judgments (the RL phase). The text of the principles makes public and auditable what would otherwise remain implicit taste in human labeling.
Calibration and side effects. Preference training can reinforce unwanted style, overconfidence or sycophancy, depending on data and method. Crucially, a token probability of 70% is not a 70% probability that a statement is true. Calibration of answer correctness needs a separately defined estimate and evaluation set: roughly 70% of answers assigned 70% confidence should be correct. Experiments find useful self-evaluation in certain models and task formats; this is not a universal property of base models or questions.
Open questions. A reward model approximates the real objective; intensive optimization can amplify its errors. KL penalties can limit this without eliminating reward hacking. Alignment-faking research documented strategic compliance in specific, constructed training scenarios. This does not establish the same behavior in every current model or ordinary conversation. Behavioral evaluation and internal analysis are complementary research tools, neither a general safety proof.
Five questions; four correct answers to pass. If you fall short you can retake it right away, as many times as you like — the questions and the options are shuffled every round.
Inference: running the finished model
Training gave us the weights — but using the model is an engineering world of its own. Prefill processes the prompt: positions can be computed in parallel, so sufficiently long prompts are often compute-bound. Decoding then proceeds token by token. At small batch sizes, reading weights and the KV cache often makes memory bandwidth the bottleneck. This is not universal: batch size, context length, architecture, and hardware change the balance. The KV cache reuses keys and values for earlier tokens. These differences motivate measuring time to first token and generation speed separately. Detailed cost model and assumptions.
Sampling: from a distribution to a token
The model's output is a distribution — but a concrete token has to be chosen. The greedy choice (always the most likely) is deterministic, but often produces flat, repetitive text. In practice we sample from the distribution, and the sharpness of the sampling is governed by the temperature parameter: the logits are divided by T before the softmax. T<1 sharpens the distribution toward the peak (more conservative, more prone to repetition), T>1 flattens it (more creative, more prone to error). As a complement, top-p (nucleus) filtering cuts off the long, noisy tail of the distribution: we sample only from the smallest set of tokens whose combined probability reaches p.
Drag the slider: at low T the distribution concentrates on the best candidate, at high T it stages out. The formula P(i) ∝ exp(zi/T) runs live, on fixed example logits.
The KV cache: so we do not recompute everything
For an unchanged model and prefix, earlier K/V vectors in a causal decoder do not depend on newly appended tokens. The KV cache stores them per layer. Each new position still requires Q, K and V projections, attention and the MLP; its K/V vectors are appended, and its query attends to permitted previous and current keys. Dense attention for one new position is linear in context length, without rerunning the entire prefix. Cache storage also grows linearly and can exceed weight storage at long contexts.
Mathematics Quantization and speculative decoding
Quantization. Lower bit widths can reduce weight and KV-cache storage and memory traffic. A simple integer scheme uses w ≈ s·q, sometimes with a separate zero point; scales are often stored per group. GPTQ and AWQ mitigate low-bit weight errors. Four bits can be a useful compromise, but quality loss must be measured for the model and task, especially at two bits. FP8 is floating point, not an 8-bit integer. Reduced storage alone does not guarantee a proportional speedup.
Speculative decoding. A fast draft model proposes k tokens; the target model can check their conditional distributions in parallel. Exact sampling accepts a token x drawn from draft distribution q with probability min(1, p(x)/q(x)). On rejection, sample from normalized max(0, p − q), not simply from p. Discard proposals after the first rejection; if all are accepted, the target can supply an additional token. This preserves the target distribution. Speedup depends on acceptance rate, drafting cost and verification cost; it is neither free nor guaranteed. Leviathan et al., section 2.3.
Serving. In a production system many requests run together: continuous batching groups decoding steps across requests (sharing the GPU on the weight reads), PagedAttention (vLLM) manages memory by breaking the KV cache into pages, and a prefix cache shares common prompt prefixes (the system prompt!). Because prefill and decoding have different profiles, large systems often split the two onto separate machine pools.
Research Sampling research: why top-p, and what goes wrong?
Decoding effects depend on the task. In open-ended generation, unrestricted sampling can select unlikely, erroneous continuations; greedy decoding or beam search can produce repetitive output. These are observed failure modes rather than laws governing every run. The mismatch between teacher-forced training and self-generated prefixes is discussed as exposure bias. Top-p, min-p and typical sampling use different filtering rules; none guarantees correctness.
Maximum model probability and task quality are different objectives. Diversity may help creative, open-ended text, while greedy or search-based decoding can suit other tasks. Temperature controls concentration of the conditional distribution; it does not directly measure creativity or truth. Compare decoding strategies on the intended task.
Five questions; four correct answers to pass. If you fall short you can retake it right away, as many times as you like — the questions and the options are shuffled every round.
Mechanistic interpretability: what happens inside?
8.1 · The reverse-engineering program
The previous chapters told us how the model computes — but not what. The weights are learned, not designed: nobody prescribed what any given neuron should mean. The research program of mechanistic interpretability (mech interp) is to reverse-engineer the trained network as though it were an unknown machine: to identify its internal variables (features) and the algorithms that connect them (circuits). The stakes are not merely scientific curiosity: if we can see what the model computes, we can audit its behavior from the inside as well as from the outside by testing — we can investigate possible internal differences between honest answers and strategic compliance (chapter 6), and look for causes of errors. This is a research goal: current methods do not provide a general, reliable lie detector or safety guarantee.
8.2 · The residual stream view and the logit lens
The natural coordinate system for reverse engineering is the residual stream from 4.2. Every component — the embedding, the attention heads, the MLPs — writes into the same shared vector space, additively; and output logits are linear maps of the vector after final normalization. Direct attribution fixes the normalization scale observed in that run; removing a component and rerunning the model can have a different effect. Two immediate, practical tools fall out of this. Direct logit attribution: since every component's contribution is a term of a sum, it can be computed component by component how much each pushed the “Paris” logit up. And the logit lens: the intermediate state of the stream can be passed through the unembedding after any layer — so we can watch, layer by layer, what the model currently “thinks” the continuation is. The typical picture: early layers still work in the surface world of tokens, the semantics come together in the middle layers (often along with the correct answer), and the late layers sharpen the phrasing.
An important limitation: the raw logit lens easily misleads. The representations of early layers do not yet “live” in the space of the unembedding — applying the matrix WU to them directly often yields uninterpretable noise or a systematically distorted prediction, and how far it remains usable varies strongly by model family. This is why the tuned lens variant was developed: an affine transformation is trained per layer to fit the intermediate state into the output space before the unembedding is applied. This gives a substantially smoother and more reliable layer trajectory. The logit lens is therefore best treated as a fast, cheap probe rather than a measuring instrument — and every conclusion drawn from it should be confirmed by causal intervention (8.6).
8.3 · Induction heads: the first circuit we understood
The finest textbook example that “circuit” is not a metaphor. The phenomenon: in many transformers a simple but broadly useful algorithm can be observed — “if the pattern [A][B] occurred earlier and I now see [A] again, predict [B]”. It is implemented by a circuit of two heads spanning two layers: a previous-token head in an earlier layer copies onto every position the identity of the token preceding it; building on that, the query of an induction head in a later layer uses the current [A] token to find the position whose “previous token was A” — that is, the earlier [B] — and forwards the content of the token it found toward the output. The composition of two learned heads = a search-and-copy algorithm.
Five questions; four correct answers to pass. If you fall short you can retake it right away, as many times as you like — the questions and the options are shuffled every round.
8.4 · Superposition: more concepts than dimensions
Why is it not enough to look at neurons? According to the superposition hypothesis, a network can store more features than it has representational dimensions if they are encoded in overlapping directions. This can work when features are sparse — only a few are active at any time — since a high-dimensional space has room for exponentially many nearly orthogonal directions. In simple toy models this can be studied in detail: there, optimization does pack features into slightly overlapping directions. Extending the result to real language models needs separate evidence; the methods of the next section (8.5) try to provide exactly that. The price: interference noise (simultaneously active features “talk into” one another a little) — and the fact that individual neurons become polysemantic, since a neuron sees the projection of many feature directions.
8.5 · Sparse autoencoders: a dictionary for thoughts
If features hide in overlapping directions, we need a tool that disentangles them. That is the sparse autoencoder (SAE): an auxiliary network trained to decompose the model's activations into a much wider but sparse code — every activation should be expressible as the sum of a few “dictionary elements”. The learned dictionary elements are surprisingly often monosemantic: they correspond to individual human concepts — the word “often” carries weight here; the known limitations of the method (reconstruction residual, feature splitting, seed dependence) are covered in this chapter's research block. From large models, with dictionaries of a million entries, features have been extracted such as “Golden Gate Bridge”, “security vulnerability in code”, “flattery”, “inner conflict” — and the features work causally too: artificially amplifying their activation shifts behavior predictably (the famous demo, “Golden Gate Claude”, steered every conversation to the bridge).
8.6 · Methodology: how do we know we are right?
The methodological backbone of mech interp is causal intervention: seeing a correlation is not enough (“this head gives a large weight to X”), one has to show that the component causes the behavior. The toolkit:
- Ablation: switch the component off (zero it out or replace it with the mean) — what breaks? If nothing, the component is not critical to the behavior under study (or it is redundantly backed up).
- Activation patching: run the model on two inputs (for example “The city of the Colosseum:” / “The city of the Eiffel Tower:”) and transplant the activation of a single component from one run into the other. If the answer switches from Rome to Paris, you have found where the information travels. Scanning systematically maps out the flow of information.
- Attribution graphs / circuit tracing: the latest generation of tools (2025) locally approximates the model with an interpretable “replacement model” and draws, for a concrete prompt, the feature→feature influence graph: which feature activated which along the path to the answer. This is how it first became visible that a model plans the rhyming word ahead while writing verse, or that mental addition runs parallel approximate and exact-digit paths.
Research The research frontier and the open problems
Faithfulness. A model's written chain of thought is not a reliable window onto the actual computation: models have been shown to rationalize after the fact — computing the answer by a different route than the one they describe, and even concealing the true influencing factor (a hint hidden in the prompt, say). This is one of interpretability's main motivations: an audit of the internal computation cannot be replaced by the model's self-report. Attribution graphs catch these “unfaithful CoT” cases experimentally.
The limits of the dictionary hypothesis. For all the power of SAEs, it remains open whether features are the model's “true” variables or merely a useful approximation: practical reconstructions usually retain some error (the residual is the “dark matter”), features keep splitting as the dictionary grows (feature splitting), and the dictionaries of SAEs trained with different seeds only partly overlap. Alternative and complementary directions: crosscoders (a shared dictionary across layers or models), transcoders (replacing MLPs with an interpretable sparse module), and research into nonlinear, multidimensional representations (circles, lattices).
Scalability and automation. Analyzing a frontier model's millions of features and circuits by hand is hopeless — so the field is moving toward automated interpretation: models explaining other models' features, generating and scoring verification experiments. The long-term goal is a kind of “microscope pipeline”: given a behavior (deception, susceptibility to jailbreaks, goal pursuit), produce a causal circuit-level diagnosis at the press of a button. The field is still far from that — today's tools explain a fraction of the computation — but the pace of progress over the past few years is striking: from two-layer toy models in 2021 to circuit-level case studies of concrete behaviors in frontier models by 2025.
Five questions; four correct answers to pass. If you fall short you can retake it right away, as many times as you like — the questions and the options are shuffled every round.
Capabilities and limits: what can we expect from the machinery?
A few characteristic behaviors follow necessarily from the machinery of the previous chapters — good and bad alike. This chapter ties the two together: it derives both the capabilities and the failure modes from the mechanism.
In-context learning: learning without a weight update
One of the strangest capabilities: from a few examples given in the prompt, the model “learns” a new task — even though not one of its weights changes. The explanation is architectural: attention can move information from the examples to the current question at runtime, and induction-style circuits (8.3) implement exactly pattern matching and continuation. It is important, however, that the induction head is not the complete explanation of in-context learning: Olsson et al. demonstrated a strong co-occurrence between the formation of these heads and the abrupt appearance of the ability, but on the current picture copying heads, MLP layers carrying task and format representations, and considerably more complex attention patterns all take part in ICL as well. The induction circuit is the best understood component, not the only one. The context is therefore a kind of fast, temporary memory in which the model tunes its learned general algorithms to the task at hand. In a research reading: under certain conditions the transformer's forward pass implements implicit optimization steps over the examples in the context (the “mesa-optimization” literature).
Step-by-step thinking
A forward pass is a computation of fixed depth: N layers, exactly that many. Whatever cannot be computed in that many steps, the model cannot produce correctly in a single token — but the generated tokens can themselves be turned into a computational scratchpad. The chain of thought (writing out the reasoning) is exactly this: the model externalizes its intermediate results as tokens and then conditions on them at the next step — “deepening” the computation along the sequence dimension. Modern reasoning models make a system of this: they learn through RL (5.6, chapter 6) to use thinking tokens efficiently — to undo an error, to try a branch, to check.
Why does it hallucinate?
Hallucination — a falsehood delivered with confidence — is not a mysterious malfunction but the shadow of the training objective. Several of its sources can be derived from the mechanism:
- The loss rewards continuation, not truth. Cross-entropy rewards plausible text; a well-sounding false claim has a low loss if its surface pattern is common. On top of that, “I don't know” scores worse than a guess on most benchmarks and in most preference data — so post-training can inadvertently teach confident guessing (the exam-taker's strategy: leaving it blank is a guaranteed loss of points).
- Knowledge is compressed into a distribution. Rarely seen facts are weak, noisy associations (chapters 3 and 4.4); after one bad token from sampling (chapter 7) the model conditions on its own mistake and coherently builds on it.
- Recall and phrasing come apart. Interpretability case studies (8.6) find a separate “I know this entity” signal in the network: if it switches on mistakenly, the inhibition on the answer-generating circuits is released and the model fills in the details — the structure is there, the content is not.
Mitigation can combine training for uncertainty and abstention, retrieval from external sources (RAG), tool use and verification. The mere presence of a citation does not establish that it exists or supports the claim. There is no established general guarantee eliminating hallucinations across open-ended tasks. Probabilistic modeling alone does not prove impossibility, however: constrained, verifiable tasks can admit strong correctness guarantees through formal restrictions or verification.
Further characteristic limits
| Phenomenon | Mechanical cause |
|---|---|
| The reversal curse — “A's parent is B” does not automatically give “B's child is A” | The autoregressive loss teaches directed associations: the fact was encoded in the order “A → B”; recall in the reverse direction requires separate learning. |
| Token-level blindness — stumbling over letter counting and anagrams | The model sees tokens, not characters (chapter 2); the internal structure of a token is learned only indirectly. |
| “Lost in the middle” — recall is weaker from the middle of a long context | Because of positional patterns and the distribution of the training data, attention weights favor the beginning and the end of the context; long-context training corrects this only partly. |
| Arithmetic fragility | Calculation runs on learned circuits (parallel approximate and refining paths, 8.6), not on a symbolic algorithm — at large operands the generalization runs out. Tool use (running code) is the clean solution. |
| Prompt sensitivity | The output is a conditional distribution: every surface feature of the prompt — order, formatting, tone — is a legitimate conditioning signal, and the model does use it. |
The last five questions; four correct answers to pass. This stage ties the others together: every question traces a capability or a failure mode back to the mechanism.
Glossary
- Attention
- The mechanism by which a token position reads information from other positions with learned relevance weights (query–key matching, value transfer). 4.3.
- Autoregressive
- Generation proceeding token by token, where every new token is conditioned on the entire text so far. Chapter 1.
- Backpropagation
- The efficient, graph-based execution of the chain rule: the gradient of every parameter is produced in a single backward pass. 5.3.
- BPE (byte-pair encoding)
- A vocabulary-building algorithm: the repeated merging of the most frequent adjacent pairs of symbols. Chapter 2.
- Chain of thought (CoT)
- Writing out the intermediate reasoning as tokens, which deepens the computation along the sequence dimension. Chapter 9.
- Circuit
- An identifiable cooperation of network components (heads, neurons, features) that implements an algorithm describable in human terms. Chapter 8.
- Cross-entropy loss
- The negative logarithm of the model's probability for the correct token; the objective of training. 5.1.
- DPO (direct preference optimization)
- An alignment method that optimizes directly from preference pairs, without a separate reward model or RL loop. Chapter 6.
- Embedding
- The assignment of a learned vector to a token; the starting point of the geometric representation of meaning. Chapter 3.
- Feature
- A unit of the model's internal representation — on the working hypothesis, to a good approximation a direction in activation space. Chapter 8.
- Gradient
- The vector of partial derivatives of the loss with respect to the parameters; it shows how much a small change to each parameter would improve things. Chapter 5.
- GQA (grouped-query attention)
- Several query heads share fewer key/value heads — shrinking the KV cache. 4.8.
- Induction head
- An attention head that finds and continues the repetition of a pattern seen earlier in the context; one of the foundations of in-context learning. 8.3.
- Inference
- Running the trained model (prefill plus token-by-token decoding). Chapter 7.
- KV cache
- Storing the key/value vectors of earlier tokens so that they need not be recomputed during decoding. Chapter 7.
- KL divergence
- A measure of the difference between two distributions; in RLHF, the brake on drift away from the reference model. Chapter 6.
- Logit
- The raw pre-softmax score for a given vocabulary entry. 4.7.
- Logit lens
- A diagnostic: passing an intermediate state of the residual stream through the unembedding — the forming prediction becomes visible layer by layer. 8.2.
- LayerNorm / RMSNorm
- Standardizing the scale of the vectors at each layer, for the stability of training. 4.5.
- MLP (feed-forward layer)
- A per-position up- and down-projection with a nonlinearity; most of the parameters and stored associations live here. 4.4.
- MoE (mixture of experts)
- Many expert MLPs, of which only a few activate per token — large capacity, small per-token computation. 4.8.
- Perplexity
- The exponential of the loss: how many ways the model is “undecided” per token on average. 5.1.
- Polysemantic neuron
- A neuron that activates for several unrelated concepts — a symptom of superposition. 8.4.
- Pretraining
- Next-token training on a giant corpus, which gives the raw capabilities. Chapter 5.
- Residual stream
- The per-position vector that receives additive updates through the layers — the communication backbone of the architecture. 4.2.
- RLHF
- Reinforcement learning from human feedback: policy optimization guided by a reward model learned from human preferences. Chapter 6.
- RoPE (rotary position embedding)
- Positional encoding by rotating the query/key vectors in proportion to position; attention thereby senses relative distance. 4.6.
- SAE (sparse autoencoder)
- An auxiliary network that decomposes activations into a wide, sparse, often monosemantic dictionary of features. 8.5.
- Softmax
- The transformation of a vector of scores into a probability distribution, by exponentiation and normalization. 4.7.
- Superposition
- Storing more features than there are dimensions — in overlapping, nearly orthogonal directions, exploiting sparsity. 8.4.
- SFT (supervised fine-tuning)
- Supervised fine-tuning on example dialogues; teaching the form of assistant behavior. Chapter 6.
- Temperature
- A sampling parameter: the divisor of the logits, sharpening or flattening the distribution. Chapter 7.
- Token
- The atomic unit of text for the model — typically a word piece (a BPE fragment). Chapter 2.
- Transformer
- An architecture of attention and MLP blocks built on a residual stream, trainable in parallel. Chapter 4.
- Unembedding
- The matrix that projects the final stream vector onto the logits of the vocabulary. 4.7.
Further reading — the canon, in reading order
Scientific review · 19 September 2026 (v1.1.1). The review covered the 12 learning stages, 60 quiz questions, worked examples and 14 interactive labs. Corrections and primary sources are recorded in the review notes. Toy illustrations are not measurements from a real LLM; this review is not independent academic peer review.
The list is deliberately short: the foundational texts of the field, roughly in the order of this material's chapters. The foundational texts are freely available; linked titles lead directly to the original sources.
- Vaswani et al. (2017): Attention Is All You Need — the original transformer paper. (Chapter 4)
- Radford et al. (2019): Language Models are Unsupervised Multitask Learners (GPT-2) and Brown et al. (2020): Language Models are Few-Shot Learners (GPT-3) — the discovery of scaling and in-context learning. (Chapters 1 and 9)
- Sennrich et al. (2016): Neural Machine Translation of Rare Words with Subword Units — the introduction of BPE into NLP. (Chapter 2)
- SentencePiece: official documentation — BPE and unigram implementations and tokenizer training. (Chapter 2)
- Rumelhart, Hinton & Williams (1986): Learning representations by back-propagating errors — the classic of backprop; as a modern complement, Karpathy: Neural Networks: Zero to Hero, a video series from a hand-built autograd to a GPT. (Chapter 5)
- Kingma & Ba (2015): Adam and Loshchilov & Hutter (2019): Decoupled Weight Decay Regularization (AdamW). (5.4)
- Kaplan et al. (2020): Scaling Laws for Neural Language Models and Hoffmann et al. (2022): Training Compute-Optimal Large Language Models (Chinchilla). (5.6)
- Su et al. (2021): RoFormer — RoPE. (4.6)
- Ouyang et al. (2022): Training language models to follow instructions with human feedback (InstructGPT) — the RLHF recipe. (Chapter 6)
- Bai et al. (2022): Constitutional AI: Harmlessness from AI Feedback. (Chapter 6)
- Rafailov et al. (2023): Direct Preference Optimization. (Chapter 6)
- Dao et al. (2022): FlashAttention and Kwon et al. (2023): Efficient Memory Management for LLM Serving with PagedAttention (vLLM). (Chapter 7)
- Leviathan et al. (2023): Fast Inference from Transformers via Speculative Decoding. (Chapter 7)
- Elhage et al. (2021): A Mathematical Framework for Transformer Circuits — the residual stream / QK–OV formalism. (8.2)
- Olsson et al. (2022): In-context Learning and Induction Heads. (8.3)
- Elhage et al. (2022): Toy Models of Superposition. (8.4)
- Li et al. (2023): Emergent World Representations: Exploring a Sequence Model Trained on a Synthetic Task and Gurnee & Tegmark (2024): Language Models Represent Space and Time — studies of internal “world” representations. (Chapter 1)
- Olshausen & Field (1996): Emergence of simple-cell receptive field properties by learning a sparse code for natural images — the classic of sparse coding, the conceptual ancestor of SAEs. (8.5)
- Bricken et al. (2023): Towards Monosemanticity and Templeton et al. (2024): Scaling Monosemanticity — SAEs at small and frontier scale. (8.5)
- Lindsey et al. (2025): On the Biology of a Large Language Model — attribution graphs, case studies (planning, mental arithmetic, unfaithful CoT). (8.6)
- Schaeffer et al. (2023): Are Emergent Abilities of Large Language Models a Mirage? — the emergence debate. (5.6)
- Berglund et al. (2023): The Reversal Curse. (Chapter 9)
- Greenblatt et al. (2024): Alignment Faking in Large Language Models. (Chapter 6)
This material was written for teaching. Its subject matter reflects the state of the field as of July 2026; the last update to this edition was 27 September 2026 — the difference between the two is set out in the changelog. The numerical examples (sizes, ratios) illustrate typical orders of magnitude, not the specifications of particular products. The figures are schematic. Corrections and suggestions are welcome: csaplar.d@gmail.com. ◆
Changelog
This material covers a fast-moving field. The list below is in reverse chronological order, so you can see how current the version you are reading is.
- v1.2.0 — 27 September 2026
- Shared AI-tananyagok design, dark mode by default. Scientific clarifications after an external review: the guide’s scope (autoregressive transformers), generation loop consistent with caching, the scope of world modeling and superposition (as a hypothesis, separating toy and real models), the induction-figure caption; language fixes; direct links in the bibliography.
- 19 September 2026 — Labels
- The navigation, links and example labels now consistently use “Interactive experiments”.
- v1.1.3 — 19 September 2026
- Order-independent quiz explanations; corrected family-relation, SAE, Adam, memory and emergence questions; more beginner-friendly wording. Fixed language-specific builds with new regression coverage. Saved progress is unchanged.
- v1.1.2 — 19 September 2026
- Quizzes: length-based clues to the correct answer were substantially reduced; the explanations once again say why the right answer is right and why the wrong one is tempting. The learning components are built separately per language, so the English edition carries no Hungarian text, and vice versa. New share card. Your progress is kept.
- v1.1.1 — 19 September 2026
- Scientific review: corrected attention/MLP formulas, bfloat16, scaling laws, speculative decoding, KV caching and calibration; revised 28 quiz items. Replaced the scaling sketch with computed curves. See the review notes for corrections and primary sources. Existing progress is preserved.
- v1.1 — 19 September 2026
- Fourteen new interactive workshops; worked foundations; a twelve-week practice path with local checklists; free reading and guided learning modes; distinct depth and progress labels; scientific corrections and balanced introductory quiz choices. Both editions remain self-contained and work offline.
- v1.0 — 30 August 2026
- First public release. The twelve chapters were turned into a level-based learning system: each chapter closes with a five-question quiz, a 4/5 pass mark and unlimited retries; chapters you have not yet completed stay locked. Progress lives in your browser's local storage — nothing is sent to a server. Light and dark modes designed separately.
- English edition, translated from the Hungarian original.