How a language model picks the next word
A language model produces a probability for each next token and samples one. See what temperature and top-p change, what they don't, and why fluent text isn't computed truth.
You will be able to
- Describe text generation as repeated sampling from a next-token distribution
- Predict the effect of temperature and top-p on that distribution
- Explain why low temperature means repeatable, not correct
One token at a time
ReadA large language modelLarge language modelA neural network trained on text to give a probability for the next token, used repeatedly to write text. does one thing, over and over. Given the text so far, it gives each possible next tokenTokenThe unit a language model reads and writes; often a word fragment, not a whole word. a probability. One gets picked, added to the text, and the process repeats.
A token is often a word fragment, not a whole word. Prompts and answers are counted in tokens, which is why they set both the cost of a call and how much fits.
Nothing in this loop looks anything up. The probabilities come from patterns in the training text.
Does a colder model know more?
PredictPredict first
You turn the temperature down to near zero and ask the same question several times. What happens?
Reveal
It gives the same answer, but no more likely to be right. Near zero, the model always takes its top token. If that token is wrong, it's now wrong every time. Temperature changes how the model picks. It doesn't change what the model knows.
Move the dials
ExploreThese probabilities are a fixed illustration of the next token after "The p-value tells you how". No model is called.
DExplorable · Temperature and top-p
What do the sampling settings on a language model actually change?
With these settings, how likely is the single most likely token?
Below 1 sharpens the distribution; above 1 flattens it.
Keep the likeliest tokens until they hold this much probability.
“The p-value tells you how extreme”
- With these settings
- Default (temperature 1, no cut-off)
Twelve sampled tokens: extreme · surprising · surprising · unusual · rare · rare · unusual · surprising · rare · extreme · surprising · likely
Commit your prediction above to see the result
Make your prediction to see the result.
Press turn the temperature up and find "sandwich" gaining real probability. Then lower the temperature until the same token comes up every time. Then set top-p to about 0.6 and count how many tokens survive.
The arithmetic
ReadThe model scores each token with a number . TemperatureSampling temperatureRescales a language model's next-token scores; low values sharpen the distribution, high values flatten it. divides those scores before they become probabilities:
With two tokens scored 2 and 1:
Top-pTop-p samplingKeeps only the most likely next tokens until they hold share p of the probability, then samples among those. then keeps only the likeliest tokens until they hold share of the probability, and samples among those. Top-k keeps a fixed number.
When should it be repeatable?
ReflectThink about three jobs for a language model:
- Pulling a field out of each support ticket.
- Suggesting names for a new product.
- Writing a first draft of a report's summary.
Which ones want a repeatable answer, and which want variety? For which would a surprising answer be a bug, and for which a benefit?
Fluent isn't true
ReadModels write what is likely, which is often true, and sometimes merely plausible. A hallucinationHallucinationFluent model output that is not supported by any source, including invented numbers, citations and column names. is fluent output with nothing behind it: a citation that doesn't exist, a column you don't have, a number nobody computed.
The model isn't lying, and it doesn't warn you, because likely text and true text feel the same to it.
The next lesson shows how CodeSplash keeps its numbers out of the model's hands.
In CodeSplash
In the appCodeSplash doesn't set temperature or top-p: it sends no sampling settings to any provider, so each model uses its own defaults. What you choose is the provider and model, in Settings.
You'll meet these dials in provider consoles and local servers. This code reproduces the explorable's sampling, so you can try other scores.
import numpy as np
z = np.array([3.2, 2.7, 2.3, 1.9, 1.5, 1.1, 0.5, 0.1, -0.4, -1.0, -2.6, -3.4])
T, top_p = 0.7, 0.9
p = np.exp(z / T)
p /= p.sum()
order = np.argsort(-p)
keep = order[: np.searchsorted(np.cumsum(p[order]), top_p) + 1]
q = np.zeros_like(p)
q[keep] = p[keep]
q /= q.sum()
rng = np.random.default_rng(1)
print(len(keep), "tokens kept; top probability", round(q.max(), 3))
print(np.bincount(rng.choice(len(z), 200, p=q), minlength=len(z)))z <- c(3.2, 2.7, 2.3, 1.9, 1.5, 1.1, 0.5, 0.1, -0.4, -1.0, -2.6, -3.4)
temperature <- 0.7
top_p <- 0.9
p <- exp(z / temperature)
p <- p / sum(p)
ord <- order(-p)
n_keep <- which(cumsum(p[ord]) >= top_p)[1]
q <- numeric(length(p))
q[ord[seq_len(n_keep)]] <- p[ord[seq_len(n_keep)]]
q <- q / sum(q)
set.seed(1)
cat(n_keep, "tokens kept; top probability", round(max(q), 3), "\n")
tabulate(sample(length(z), 200, replace = TRUE, prob = q), length(z))Check your understanding
CheckpointCheck yourself
Two tokens have scores 2 and 1. At temperature 2, what's the probability of the first? Give three decimals.
Show the answer
Answer: 0.622
Divide the scores by 2 to get 1 and 0.5. Then e¹ / (e¹ + e⁰·⁵) = 2.718 / (2.718 + 1.649) = 0.622. Higher temperature flattens the distribution: it was 0.731 at temperature 1.
Check yourself
Top-p is set to 0.9. What does it do?
Show the answer
Answer: a
It trims the unlikely tail. It says nothing about whether the tokens it keeps are right.
Check yourself
A model gives you a well-formatted citation for a paper. What should you do?
Show the answer
Answer: a
A confident follow-up comes from the same process as the citation. Check it against the source.
Recap
Recap- A language model repeatedly samples the next token from a probability distribution.
- Temperature sharpens or flattens the distribution, and top-p trims its tail.
- Low temperature gives repeatability, not correctness.
- Fluent text can have nothing behind it, so check claims against a source.
Concepts in this lesson