How language models work
From next-word prediction to the full Transformer — tokens, embeddings, attention, the MLP, training — then what turns it into a chat assistant.
20 stops34 min
The lessons, in order
-
What a language model actually does
One job: guess the next word. Probabilities over every word, sample, append, repeat.
1min1:28
-
Tokens and embeddings
Text becomes tokens, tokens become vectors. Similar words cluster; directions carry meaning.
1min1:27
-
The problem attention solves
“River bank” and “bank deposit” start from the identical vector. Meaning must come from neighbors.
1min1:19
-
The only math you need
Dot product = similarity meter. Matrix × vector = a stack of learned questions. The model IS the matrices.
2min2:07
-
Neural networks
multiply, add, bend
A neuron multiplies, adds and bends. Stack layers of them into a neural network: one function, set by its weights and biases.
2min2:27
-
Attention: words talking to each other
Queries, Keys, Values. Scores → softmax → weighted sum. Watch softmax(QKᵀ/√d)·V happen.
2min2:15
-
Multi-head attention + the MLP
Many heads track many relationships; each token thinks alone in the MLP, where facts live.
2min1:50
-
The whole Transformer, assembled
Embed → (attend + think) × 96 → unembed → probabilities. The box, opened; the loop, closed.
2min1:31
-
Mixture of Experts (MoE)
Replace the one big MLP with many experts plus a router picking the top 2. 47B stored, ~13B paid.
2min1:37
-
Tokenization and BPE
Byte-Pair Encoding merges its way to a vocabulary — and why models can't count the r's in strawberry.
2min1:54
-
Supervised learning
examples with answers
A line learns house prices from labeled examples, then faces houses it never saw. Text, it turns out, labels itself.
2min2:24
-
How the matrices get filled in
Weights start as noise. Guess, measure surprise, nudge downhill — trillions of times.
2min1:50
-
How the model knows word order
Attention is order-blind. Position fingerprints and RoPE put the sequence back.
Also rests on Sine and cosine Calculus
2min1:41
-
No peeking: causal masking
Mask the upper triangle so the future never leaks — and one sentence trains every position at once.
1min1:14
-
The KV cache
what context costs
Keys and values get cached; every new token reads a longer shelf. The n² cost, and why long chats slow down.
1min1:17
-
Pretraining
the internet as flashcards
Every prefix is a question, the next word is the answer. The result: an autocomplete, not an assistant.
2min1:34
-
From autocomplete to assistant (RLHF)
Fine-tuning teaches the format; human preferences train a reward model that shapes behavior.
2min1:35
-
Temperature
how models choose
Greedy loops and drones. Temperature sharpens or flattens; top-p cuts the weird tail.
2min1:31
-
Scaling laws
why bigger got better
Loss vs compute is a straight line on log scales; abilities switch on in jumps.
1min1:20
-
Opening the black box
Heads with jobs, single-concept neurons, superposition — the interpretability frontier.
2min1:46