Skip to content

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.

Introductory ~11 min9 steps

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
01

One token at a time

Read

A 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.

02

Does a colder model know more?

Predict

Predict first

You turn the temperature down to near zero and ask the same question several times. What happens?

It gives the same answer, and it's more likely to be rightIt gives the same answer, but no more likely to be rightIt gives a different answer every time
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.

03

Move the dials

Explore

These 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?

0.50
How sure?
1.00

Below 1 sharpens the distribution; above 1 flattens it.

1.00

Keep the likeliest tokens until they hold this much probability.

12
200

“The p-value tells you how extreme”

surprising0.364unusual0.221likely0.148extreme0.099compatible0.066rare0.045strong0.024much0.016big0.010true0.005purple0.001sandwich0.000
  • 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 first; the figure appears when you commit.

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.

04

The arithmetic

Read

The model scores each token ii with a number ziz_i. TemperatureSampling temperatureRescales a language model's next-token scores; low values sharpen the distribution, high values flatten it. TT divides those scores before they become probabilities:

pi=ezi/T∑jezj/Tp_i = \frac{e^{z_i / T}}{\sum_j e^{z_j / T}}

With two tokens scored 2 and 1:

T=1: e2e2+e1=0.731T=0.5: e4e4+e2=0.881T{=}1:\ \frac{e^{2}}{e^{2} + e^{1}} = 0.731 \qquad T{=}0.5:\ \frac{e^{4}}{e^{4} + e^{2}} = 0.881

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 pp of the probability, and samples among those. Top-k keeps a fixed number.

05

When should it be repeatable?

Reflect

Think 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?

06

Fluent isn't true

Read

Models 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.

07

In CodeSplash

In the app

CodeSplash 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.

Python
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)))
R
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))
08

Check your understanding

Checkpoint

Check 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.

09

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