How a model predicts the next token
A language model reads the text so far, gives every possible next token a probability, picks one, and repeats.
Read first: Tokens & tokenization, Transformers & attention.
D1 · StoryFor young children and total beginners
Let's play a game. I say: “The cat sat on the…” and you shout the next word.
You'd probably say “mat”. Maybe “floor” or “sofa”. Probably not “moon”!
An AI language model plays this game again and again, one small piece of a word at a time. That's how it writes whole answers.
Like: The finish-the-sentence game.
D2 · PictureFor curious beginners
Text is split into tokens (words or pieces of words), and each token becomes a list of numbers.
Those numbers pass through a stack of transformer layers. Attention lets each position look back at earlier tokens to decide what matters.
The last layer gives a score to every token in the vocabulary, and softmax turns those scores into probabilities.
One token is chosen, added to the text, and the whole process repeats.
One step of text generation
- Tokenize: text to tokens
- Embed: tokens to numbers
- Transformer: attention + layers
- Scores: one per token
- Softmax: to probabilities
- Pick: and append
Like: Like the autocomplete on your phone, scaled up enormously.
Where it breaks: Phone autocomplete looks at the last few words. A large model attends to its whole context window at once, and its internal computation can represent plans that go beyond the next word.
D3 · PlayFor motivated learners
The model gives every candidate token a score; softmax turns scores into probabilities. Temperature controls how sharp or flat that distribution is before a token is drawn.
Try the tasks below. Watch how the bars change, then press Sample 20 to see what the model would actually write.
Like: Temperature is like how adventurous you feel when ordering: low means your usual favourite, high means you might try anything.
Where it breaks: Your adventurousness has reasons. Temperature just divides every score by the same number before softmax: it changes how random the choice is, not how good or creative the options are.
Try the interactive version in the map.
Things to try
- Set the temperature to 0.2. Which word wins almost every time?
- Set it to 1.8 and sample again. How often does “moon” appear now?
- Limit to the top 3 tokens. Which words can never be chosen?
D4 · MechanismFor practitioners
Each candidate token i gets a score (logit) zᵢ. With temperature T, the probability is pᵢ = exp(zᵢ / T) ÷ Σⱼ exp(zⱼ / T).
Greedy decoding always takes the highest pᵢ. Top-k keeps only the k highest-probability tokens and renormalises. Top-p (nucleus) keeps the smallest set whose probabilities add up to at least p.
Edit the scores and settings below; every column is computed live from the formula.
Try the interactive version in the map.
tokens = tokenize(prompt)
cache = None # KV cache: reuse attention work for earlier tokens
while not done(tokens):
logits, cache = model(tokens[-1:], cache) # one forward pass for the newest token
probs = softmax(logits / temperature)
probs = top_p(top_k(probs, k), p) # optional filters, then renormalise
next_token = sample(probs) # or argmax(probs) for greedy decoding
tokens.append(next_token)
return detokenize(tokens)Trade-offs
- Greedy is repeatable but can loop or sound flat; sampling is more varied but less predictable.
- Low temperature suits extraction, code and factual answers; higher temperature suits brainstorming and fiction.
- Top-p adapts to how confident the model is; top-k uses a fixed cut-off regardless of the distribution's shape.
- The KV cache makes each new token cheap to compute but uses memory that grows with context length.
D5 · FrontierFor experts
D2's loop of predict, pick, repeat is accurate, but it undersells what happens inside and hides real limits of the training objective.
Planning inside. Interpretability work found a model picking a rhyme word before writing the line that leads to it, though the method gave clear insight on only about a quarter of prompts tried (Lindsey et al., 2025).
Teacher forcing can fail. On a simple path-finding task, next-token training failed to learn the task at all, while predicting several tokens ahead helped (Bachmann and Nagarajan, 2024).
Several tokens at once. Training to predict four tokens ahead improved coding results at 13B and allowed up to three times faster inference (Gloeckle et al., 2024).
Not left to right. An 8B diffusion language model trained from scratch was competitive with similar autoregressive models, so left-to-right order is not the only option (Nie et al., 2025).
Examples as of 2026-10-30
- Every chat model: GPT, Claude, Gemini, Llama and others
- Sampling settings in model APIs: temperature and top-p
Sources
- Attention Is All You Need (2017)
- On the Biology of a Large Language Model (2025)
- Better & Faster Large Language Models via Multi-token Prediction (2024)
- Fast Inference from Transformers via Speculative Decoding (2022)
- The pitfalls of next-token prediction (Bachmann and Nagarajan) (2024-03-11)
- Large Language Diffusion Models (Nie et al.) (2025-02-14)
How it connects
- Transformers & attention computes How a model predicts the next token.
- Pretraining uses as its objective How a model predicts the next token.
More in Layer 5 · Foundation Models
- Tokens & tokenization
Text is chopped into tokens (common words or pieces of words), and each token becomes a number the model can work with. - Transformers & attention
The architecture behind modern language models: stacked layers in which every token can look back at earlier tokens and pull in what's relevant. - Pretraining
Training a model from scratch on huge amounts of text by asking it, over and over, to predict the next token and correcting its mistakes. - Post-training: from predictor to assistant
Fine-tuning and reinforcement learning that turn a raw text predictor into a helpful, safer assistant. - Open vs closed weights
Some models' weights can be downloaded and run anywhere; others can only be reached through the provider's service. - Reasoning models
Models trained to work through a problem step by step before answering, trading extra time and tokens at answer time (test-time compute) for better results on hard tasks. - Specialised and fine-tuned models
Models adapted for one job, such as coding or a small focused task, by fine-tuning, distillation or community modification of open weights.