Next-Token Prediction
The one deceptively simple objective — predict the next token — that teaches a model grammar, facts, and reasoning. Built from zero: probabilities over a vocabulary, softmax, cross-entropy, perplexity, and how a predictor becomes a writer.
Start here — the only job an LLM is ever given
Everything a large language model can do — answer questions, write code, translate, summarize, argue — comes out of training it to do one thing, over and over, a few trillion times:
Given the text so far, guess what comes next.
That's the whole training objective. Not "understand language." Not "be helpful." Just guess the next piece of text. This page is about why that one boring instruction is enough, and exactly how it turns into numbers a computer can improve.
A reads the text you have so far and produces a probability for every possible next token. Training means: show it real text, look at what actually came next, and nudge the model to have given that token a higher probability. Repeat until the guesses get good.
Remember from Tokenization that models don't read words — they read , chunks of text like " Paris" or "ing". The complete list of tokens a model knows is its . So the precise job is: given the tokens so far, put a probability on each token in the vocabulary.
A forecaster never says "it will rain tomorrow." They say "70% rain, 25% cloud, 5% sun" — a whole spread of possibilities with confidence attached. A language model works exactly the same way. It never picks a single next word. It hands you a forecast over every token it knows, and only later does something else decide which one to actually use.
It starts from zero and adds one idea at a time: what the model outputs → how raw scores become probabilities → how we measure being wrong → the number researchers actually report → how one sentence becomes hundreds of training examples → how a predictor becomes a writer. Flip the Depth switch at the top for the formal notation and the derivations; nothing is hidden for good, and the deeper panels open automatically once you have the prerequisites.
What the model actually outputs — a forecast, not a word
Say the text so far is The capital of France is. A trained model does not output " Paris". It outputs a number for every single token in its vocabulary — tens of thousands of numbers — and those numbers add up to 1.
is the name for that spread. Here's what the top of it looks like:
Two things to notice, because both matter later:
- The model is confident but not certain. It gives
" Paris"most of its belief, but keeps a little aside for everything else. A model that was 100% certain of everything would be a model that could never be surprised — and never learn. - Every bar is positive and they sum to exactly 1. That's not a coincidence; it's forced by the softmax you'll meet in a moment.
A language model is asked to continue the text The capital of France is. What does it produce?
Hint: Think about the weather forecaster.
Logits → probabilities: the softmax
Inside, the network doesn't naturally produce probabilities. Its last layer spits out one raw score per vocabulary token — any number at all, positive or negative, and they don't add up to anything in particular. Those raw scores are called .
You have raw scores like 2.0, 1.0, and 0.1. You need positive numbers that add to 1. Two moves do it: first make every score positive by exponentiating it (raising to that power), then divide each one by the total so the shares add to 1. That two-step recipe is the .
An election gives you raw vote counts — 74 for one candidate, 27 for another. Nobody quotes raw counts; they quote 66% and 24%. Softmax is that same "counts into shares" conversion, with one extra step first: exponentiating turns the model's scores (which can be negative) into something that behaves like a vote count, and it does so aggressively — a score that's 1 point higher gets about 2.7 times the share.
Suppose a tiny model with a three-token vocabulary produces logits [2.0, 1.0, 0.1].
- Exponentiate each one. , , . All positive now.
- Add them up. . This total is the "electorate."
- Divide each by the total. , , .
Result: [0.659, 0.242, 0.099]. They're all positive, and they sum to . The token with the highest logit got the biggest share — and notice how a gap of just 1.0 in the logits became a roughly 2.7× gap in probability.
You add 100 to every single logit before applying softmax. What happens to the resulting probabilities?
Measuring wrongness — the cross-entropy loss
Now we have a forecast. Real text tells us what actually came next. To train, we need one number saying how bad was that forecast — the — because gradient descent can only shrink a number.
The right measure is how surprised the model was by the truth. If it gave the actual next token a probability of 0.9, it basically saw it coming — barely surprised, tiny loss. If it gave the true token 0.01, it was blindsided — huge surprise, huge loss. So: take the probability the model assigned to the token that really came next, and turn "high probability" into "low penalty."
The function that does that flip is the negative logarithm, . And that's the entire loss: .
Imagine you must bet on the next token, spreading 100 chips across the candidates, and you're charged based on how few chips you put on the winner. Put 90 chips on the right token and you pay almost nothing. Put 1 chip on it and you pay dearly. Put zero chips on it and the penalty is infinite — which is precisely why softmax never lets any probability reach exactly zero. Training is you, over trillions of rounds, learning to bet better.
Each row is one position in real text. We only ever look at the probability the model gave the token that actually appeared — the other 50000 probabilities are irrelevant to the penalty except through the fact that they stole from it.
| Probability on the true token | Loss | Reading |
|---|---|---|
| 0.90 | 0.105 | Saw it coming. Almost no penalty. |
| 0.50 | 0.693 | A coin flip's worth of doubt. |
| 0.10 | 2.303 | Genuinely surprised. |
| 0.01 | 4.605 | Blindsided. Big correction incoming. |
Work one out by hand: , so the loss is . The logarithm of a number below 1 is negative, and the leading minus sign flips it to a positive penalty. That sign flip is the whole reason the minus is there.
The model gives the correct next token a probability of 0.99, but it also gave a wrong token 0.005. Does that wrong token contribute to the loss?
Hint: Look at which single number the loss formula reads.
Perplexity — the number papers actually report
Cross-entropy in nats is hard to feel. Is a loss of 3.0 good? Researchers therefore quote instead.
A perplexity of 10 means the model is about as uncertain as someone picking uniformly at random from 10 equally likely options at every step. Perplexity 2 means it's basically down to a coin flip. Perplexity 50000 means it's guessing blindly from the entire vocabulary. Lower is better, and it's on a scale you can picture.
Perplexity is just raised to the loss.
- A model reaches an average loss of nats. Perplexity — as uncertain as choosing among 10 options.
- A weaker model sits at a loss of . Perplexity — twice as many effective options, so twice as lost. Loss and perplexity always move together; perplexity just puts the number on a scale you can picture.
- An untrained model with a 50257-token vocabulary spreads its guess uniformly, so it gives the true token and its loss is . Perplexity — exactly the vocabulary size, which is the sanity check every practitioner runs on step 0 of training.
That last point is the most useful thing here: if your loss at initialization isn't close to , something is wrong before you've trained at all.
Two language models are evaluated on the same test set. Model A reports a perplexity of 18; Model B reports 42. What can you conclude?
One sentence, hundreds of lessons — teacher forcing
Here's the trick that makes pretraining so absurdly data-efficient: a single sentence isn't one training example. It's one training example per token.
Take the sentence the cat sat on the mat. Chop it after the first token: context the, answer cat. Chop after the second: context the cat, answer sat. And so on. A 6-token sentence yields 5 question-answer pairs — and crucially, the answers were already there. Nobody labels anything. The text labels itself. That's why this is called .
Hand someone a novel and ask them to make flashcards from it, and you'd get maybe a few hundred. Feed the same novel to a language model and every single token position becomes a card: "here's everything up to here — what's next?" A 100000-token book is 100000 flashcards. Multiply by the internet and you see where trillions of training signals come from.
The sentence the cat sat on the mat, with each position's context and target:
| Position | Context the model sees | Target it must predict |
|---|---|---|
| 1 | the | cat |
| 2 | the cat | sat |
| 3 | the cat sat | on |
| 4 | the cat sat on | the |
| 5 | the cat sat on the | mat |
Now the important part: the model does not process these five rows one at a time. It processes the whole sentence in a single forward pass and produces a prediction at every position simultaneously — five distributions, five losses, one average. That parallelism is exactly what the Transformer buys you, and it's why training is feasible at all.
Note also row 4: the target is the, a token the model has already seen at position 1. The correct prediction genuinely depends on context, not on the token alone.
But there's a catch. If the model sees the whole sentence at once, what stops it from peeking at position 3 while predicting position 3? Nothing — unless we forbid it.
The fix is a : before the model combines information across positions, we blank out every connection that points backward in time. Position 3 may attend to positions 1, 2, and 3; it may not attend to 4, 5, or 6.
Training this way — feeding the true prefix at every position rather than the model's own earlier guesses — is called . The name is literal: at every step the teacher hands the model the correct history, so one bad prediction can't poison the rest of the sentence.
The heatmap on the right is that mask. Each row is a position doing the predicting; each column is a position it might look at. Lit squares are allowed; dark squares are forbidden. Everything above the diagonal — the future — is dark.
The mask is also where the classic off-by-one bug lives. Inputs and labels are the same sequence shifted by one: the label for position is the input at position . Shift the wrong way and the model trivially learns to copy its own input, the loss crashes toward zero, and generation produces gibberish.
A loss that drops implausibly fast is nearly always a leak.
Why can a language model be trained on raw internet text with no human labelling at all?
Hint: Where does the correct answer for each position come from?
From predictor to writer — autoregressive generation
A model that only ever answers "what's next?" seems limited. It isn't, because you can feed its answer back in.
Ask for the next token. Pick one. Append it to the text. Now ask again — with the new, longer text as context. Loop. Each pass produces exactly one token, but the loop produces essays, proofs, and programs. This feed-the-output-back-in loop is what means.
Each individual decision is myopic — one token. But because the result of that decision becomes part of the next decision's input, long-range structure emerges anyway. The model writing the tenth paragraph can see the nine it already wrote.
How do you turn the forecast into an actual token? That choice is , and it is not part of training at all — you can change it freely on an already-trained model:
- Greedy — always take the highest-probability token. Deterministic, and reliably dull; it also gets stuck repeating itself.
- Sampling — draw randomly according to the probabilities. Varied, but occasionally picks something from the long tail that derails the text.
- Top-k — keep only the most likely tokens, renormalize, then sample. Cuts off the tail.
- Top-p (nucleus) — keep the smallest set of tokens whose probabilities sum to (say 0.9), then sample. Adapts: a narrow set when the model is confident, a wide one when it isn't.
Temperature and decoding strategy are why the same model can feel rigid in one product and freewheeling in another. Nothing about the weights changed — only how the forecast is read.
Here is the whole idea at microscopic scale. This model has no learned parameters at all — it just counts which token follows which, which is exactly what a does — and then generates by sampling from those counts.
During training the model always receives the real previous tokens as context, but during generation it receives its own previously generated tokens. What is this mismatch called, and why does it matter?
Why such a simple objective teaches so much
It's genuinely surprising that "guess the next token" produces something that can translate, summarize, and write code. The reason is that guessing well requires knowing things.
- To finish
The capital of France is ___you must have stored a fact. - To finish
She picked up the red ___you need grammar and plausibility. - To finish
def factorial(n): if n <= 1: return ___you need to have internalized how the code works. - To finish
Translate to French: The book is on the table. ___you need translation. - To finish
17 * 24 = ___you need arithmetic.
None of those abilities were asked for. Each one showed up because it lowered the loss. That's the whole reason the field bet so heavily on scale: the objective never saturates — there is always more structure to learn that would shave a little more surprise off the next token.
Predicting text well and compressing text well are the same problem — a good forecast lets you encode the truth in fewer bits, which is precisely what the cross-entropy loss measures. So a model driving its loss down is being squeezed into finding the shortest possible description of how language and the world behave. The regularities it extracts to save bits are what we experience as knowledge.
- The model does not pick a word. It outputs a distribution; a separate decoding step picks. Temperature and top-p live there, not in the model.
- Low loss is not the same as being right. The objective rewards matching the training distribution, including its errors and biases. Nothing in cross-entropy knows what is true.
- A base model does not answer questions. It continues text. Ask it something and it may reply with more questions — the answering behavior comes later, from supervised finetuning and RLHF.
- Perplexity across tokenizers is meaningless. Different vocabularies chop text into different numbers of tokens, so per-token numbers aren't comparable.
Explain to a friend who has never studied maths why training a model to guess the next word ends up teaching it facts and grammar. Then explain what the loss number means in plain words, and why a probability of zero on the true token would be infinitely bad. If you stall on the loss, reread the surprise curve section.
- A language model outputs a probability distribution over the entire vocabulary at every position — a forecast, never a single word.
- Softmax turns raw scores (logits) into that distribution: exponentiate, then divide by the total. Only the differences between logits matter.
- Cross-entropy loss is the negative log of the probability given to the token that actually came next — literally "how surprised was the model." It is exactly maximum likelihood, and its floor is the entropy of language itself.
- Perplexity is : the effective number of options the model is choosing among. An untrained model's perplexity equals its vocabulary size — the sanity check at step 0.
- Every token position is its own training example, labelled by the text itself (self-supervision), with a causal mask stopping the model from seeing the future and teacher forcing supplying the true prefix.
- Feeding predictions back in makes the predictor a writer (autoregressive generation); how you pick each token — greedy, sampling, top-k, top-p, temperature — is a decoding choice made after training.
Practice — and how to make it stick
• Retrieval practice: before scrolling back, try to say out loud what softmax does, what cross-entropy measures, and what perplexity means. Struggling to recall beats rereading.
• Spaced repetition: mark this topic complete to add it to your Review queue, so it resurfaces right before you would have forgotten it.
• Interleaving: mix these with Optimization and Probability & Statistics rather than grinding one topic — the loss you just met is exactly the number gradient descent minimizes.
Edit and run the lab below. If you get stuck or hit an error, ask Ada on the right — she can see your code and your terminal output.
Then take it further, away from the keyboard and back to it:
- By hand: a model gives the true token a probability of . What is the loss, and what perplexity would that imply if every position were like this? (Answer: , perplexity — as if choosing among four options.)
- Break it on purpose: in the bigram cell above, change
rng = np.random.default_rng(0)to a different seed and rerun a few times. Notice how much the generated sentence varies — that variability is sampling, and temperature would widen or narrow it. - Extend the bigram model: make it a trigram model that conditions on the previous two words. Does the generated text get more coherent? What happens to the number of contexts it has never seen — and why does that make smoothing necessary?
- Read like a scientist: skim the GPT-2 paper's introduction and find where it argues that a model trained only on next-token prediction picks up tasks it was never taught. Compare that claim to the list in the section above.
- Build it for real: work through Karpathy's Let's build GPT video, which starts from exactly the counting bigram model above and grows it into a Transformer.
The one thing this page left as a black box is how the model turns a context into logits — how it decides that after The capital of France is, the token " Paris" deserves a high score. That's the next lesson: Self-Attention from Scratch.