Key takeaways
- A language model does one thing: given the text so far (the context), it produces a probability distribution over the next token.
- Pick a token, append it, and predict again. Repeat that loop and you get a whole passage. This is autoregressive generation.
- Text is first cut into tokens by a tokenizer, then turned into lists of numbers (vectors). From start to finish, the model only ever deals with numbers.
- Training means doing "guess the next word" exercises over a huge amount of text, pushing up the probability of the right answer so the loss goes down. That's pretraining. A second stage, post-training, turns the model from an autocomplete engine into an assistant.
The language model you use every day
You've seen this a thousand times: you type "The weather is so" on your phone, and the keyboard is already suggesting the next word: nice, good, cold, hot. How did it guess?
It's doing just one thing: based on what you've typed so far, it estimates which word is most likely to come next.
Today's large language models (LLMs), the ones that write essays, write code and chat with you, are doing exactly the same thing under the hood: predicting the next word. They're just extremely, extremely good at it.
This episode starts from that one sentence and takes a quick tour of the whole picture. Each later episode then takes one piece apart in detail.
Not one answer, but a table of probabilities
Give the model the start of a sentence, "The cat sat", and ask it to guess the next word.
The model doesn't give just one answer. It gives a table: every possible next word, each paired with a probability.
| Next word |
Probability |
| on |
62% |
| down |
14% |
| there |
8% |
| by |
5% |
| in |
3% |
"On" is the favorite at about 62%; "down" and "there" are possible too, just less likely. Three things to remember about this table:
- It doesn't stop at five rows. Every word the model knows is in it, tens of thousands of them, and almost all of them have a probability close to zero.
- All the probabilities add up to exactly 1, or 100%. In math, a table like this is called a probability distribution.
- It is completely determined by the text that came before. Change the start to "The dog lay", and the whole table reshuffles, with "down" taking the lead.
So you can think of a language model as a function: the input is the text so far, and the output is a probability distribution over the next word. In the notation you'll see again and again in this course, the model gives
p(xt∣x<t)
which reads: the probability that position t holds the token xt, given all the tokens before it, x<t.
One word at a time: autoregressive generation
Once we have a function like this, how do we get it to write a whole passage? The method is surprisingly simple:
- Hand "The cat sat" to the model and get a probability distribution for the next word.
- Pick one word from the table, say the most likely one, "on", and stick it onto the end.
- Hand the longer sentence, "The cat sat on", back to the model and get a new distribution. This time the favorite is "the".
- One more round. This time we don't take the top choice, "mat". We take "warm" instead.
Why not always take the top word? In practice, models often draw a word at random according to the probabilities, so that what they write doesn't come out the same every time.
Round after round (predict, pick a word, append it, predict again), a complete sentence grows. Feeding what you just generated back in as the next input like this is called autoregressive generation.
Intuition: When you watch a chatbot's reply pop out word by word, you're watching this "predict, pick, append" loop go round.
What the model actually outputs at each step is a list of scores over the vocabulary, called logits. How you choose the next word from them is called the sampling strategy: always the top one, a random draw, or something cleverer. There's a lot to it, and episode 11 is all about that.
What counts as a "word"? Tokens
We keep saying "the next word". What exactly counts as a word?
Language models don't work with letters or whole words directly. They work with little chunks of text called tokens.
- In the sentence "Language models are unbelievable", common words like "models" and "are" get one token each, while a rarer, longer word like "unbelievable" is split into three pieces: un, believ and able. That's 6 tokens in total.
- Every token has a fixed ID number in the vocabulary. What the model actually sees is this sequence of numbers. (The IDs shown in the video are just for illustration; every model has its own vocabulary.)
- Other languages work the same way. In Chinese, a token can be a single character or a common word: 我喜欢大语言模型 ("I like large language models") might be cut into 我 / 喜欢 / 大 / 语言 / 模型.
- Exactly how text gets split depends on each model's own tokenizer.
All the tokens together make up the vocabulary. The model's "dictionary" is fixed: it can only choose from these tokens.
| Model |
Vocabulary size |
| GPT-2 (early) |
50,257 |
| Llama 3 (later) |
128,256 |
So the probability table from before, to be precise, gives a probability to every single token in the vocabulary.
Where the vocabulary comes from: BPE
The most common way to build a vocabulary is BPE, byte pair encoding. The idea is simple: chunks that often show up side by side get merged into a new token.
Here's a tiny example. Our corpus has four words:
| Word |
Count |
| low |
5 |
| lower |
2 |
| newest |
6 |
| widest |
3 |
To start, every word is split into single letters. Then we repeat "count, then merge":
- Count which two neighboring pieces appear together most often. E and S: six times in "newest", three in "widest", nine in total. That's the most, so we merge
e + s into es and write down our first rule.
- Count again. Now
es and t sit side by side nine times, so they merge into est.
- Next come
l and o: 5 + 2 = 7 times across "low" and "lower". They merge into lo.
- Then
lo and w merge into low. Look: "low", a common word, has become a single, complete token.
A real tokenizer does exactly this, counting and merging tens of thousands of times over a mountain of text, until the vocabulary reaches the size it wants.
The result: common words become whole tokens, while rare words get split into a few common pieces. No matter how strange a word is, the model can always spell it out from these pieces.
To the model, everything is numbers
One last key point: the model can really only do math. Everything it handles is numbers. Here's the whole pipeline:
- Text → IDs: the tokenizer cuts the text into tokens, and each token is replaced by its ID in the vocabulary.
- IDs → vectors: each ID is turned into a long list of numbers called a vector (this step is called the embedding). Think of the vector as that token's coordinates in a high-dimensional space.
- Vectors → scores: the vectors go through one giant mathematical function, the Transformer, which ends by computing a score (a logit) for every token in the vocabulary. Inside, a Transformer is a stack of layers, each one "attention plus a feed-forward network"; episodes 6–10 take it apart.
- Scores → probabilities: an operation called softmax turns those scores into probabilities that add up to 1. And that's the probability table we started with.
Every step of this pipeline is something this series will take apart. For today, just remember the overall shape.
How the model learns: training
An endless supply of exercises
So how does the model learn to guess so well? Feed it a mountain of text, and have it do "guess the next word" exercises over and over again.
The sentence "I like to eat apples" gives us several exercises:
| Context (input) |
Correct next word |
| I |
like |
| I like |
to |
| I like to |
eat |
| I like to eat |
apples |
Books, articles, web pages, code: any piece of text can be cut up like this. That's an endless supply of exercises, and the answers come for free. No one has to label anything by hand.
Measuring "how wrong" with the loss
Zoom in on one exercise: the model sees "I like to eat", and the right answer is "apples".
At the start of training, the model is basically guessing blindly. It gives "apples" only a 5% chance, and thinks "pizza" and "rice" are more likely.
We measure how wrong it is with a single number, the loss. The most common loss is the negative log of the probability of the right answer:
L=−logp
where p is the probability the model gave the right answer. As p gets close to 1, the loss goes to 0; the smaller p, the bigger the loss. Right now "apples" is at 5%, so the loss is about −log0.05≈3.
Training means nudging the model's internal parameters, little by little, so the right answer's probability goes up and the loss goes down. Once "apples" reaches 62%, for example, the loss is down to −log0.62≈0.48.
Intuition: −logp comes down especially hard on confident mistakes. As p heads toward 0 the loss shoots up, while pushing a p that's already near 1 even higher barely saves anything.
Every position in a piece of text is one exercise. Add up their losses and you get the loss for the whole text:
L(x)=−t=1∑Tlogp(xt∣x<t)
This is the "negative log likelihood of the next token", which is exactly the cross-entropy loss used to train language models. Where the name cross-entropy comes from is a story for episode 5.
How exactly do we nudge the parameters? That takes derivatives, gradients and backpropagation, the subject of episodes 2 through 4.
Pretraining
Then repeat this over trillions of tokens. Llama 3, for example, was trained on more than 15 trillion tokens. This stage is called pretraining.
Why "guess the next word" is so powerful
It's just guessing the next word. That sounds so simple, so why would it make a model so smart? Look at three exercises:
| Prompt |
Answer |
Needs |
| The capital of France is |
Paris |
knowledge |
| 3 + 5 = |
8 |
arithmetic |
| He left his keys at home, so he's locked |
out |
reasoning |
For the third, the model has to understand cause and effect: the keys are inside, he's outside, so he's locked out.
So to predict the next word well across a huge amount of text, the model is forced to learn grammar, learn facts, and even learn some reasoning. A simple goal plus enormous scale leads to surprising abilities.
From autocomplete to assistant
But a model that has only been pretrained isn't a helpful assistant yet. What it has learned is to continue text, not to answer.
Ask it "How do I make scrambled eggs?", and it might carry on with "How do I make pancakes? How do I make French toast?…", because lots of web pages (an FAQ list, say) look exactly like that. Pretraining teaches the model language, but not what it takes to be a helpful assistant that follows instructions and matches human preferences.
To turn it into an assistant that actually answers your questions, you need a second stage: post-training. Using demonstrations written by people, plus human preferences, the model learns to talk the way an assistant should. Post-training methods like RLHF, PPO, GRPO and DPO are covered in episodes 18 through 20.
A map of the series
| Part |
Episodes |
About |
| Foundations |
1–5 |
How neural networks learn |
| Transformer |
6–10 |
What's inside the model |
| Inference |
11–14 |
Generating fast and well |
| Scaling |
15–17 |
Making models bigger |
| Post-training |
18–20 |
From autocomplete to assistant |
| Beyond |
21–23 |
RNNs, SSMs, multimodal, stats |
- Foundations: what a neural network is, and how it learns through gradients and backpropagation.
- Transformer: opening the black box: attention, position encodings, normalization, and how many parameters and how much compute a model really needs.
- Inference: how to pick words from the probability table, how caching and speculative decoding make generation faster, plus GPUs and FlashAttention.
- Scaling: scaling laws, numerical precision, and how to split one model across hundreds or even thousands of GPUs for training.
- Post-training: from policy gradients to PPO, RLHF, GRPO and DPO, and how a model goes from autocomplete to assistant.
- Beyond: RNNs from before the Transformer, state space models from after it, multimodal models that can see images, and a bonus episode on statistics.
Recap
A language model takes the context and produces a probability distribution over the next token; append the chosen tokens one by one and you can write a whole passage (autoregression). Text is first cut into tokens and then turned into vectors, so the model only ever deals with numbers. Training means pushing up the probability of the right answer, again and again, over a huge amount of text, so that the loss −logp gets smaller. Next time, we start from the smallest building block of all: a single neuron.