How an LLM picks words

Step inside the last stage of a large language model: scores, softmax, temperature, top-k and top-p, one word at a time. About this simulator

About this simulator and how it works

Free demo: level 1 (4 jobs). Levels 2 to 4 (top-k and top-p, whole sentences, penalties and beam search, tokens) are in the full version.

Chat AIs write one token at a time. For each one the model gives every possible next token a score, turns the scores into probabilities and rolls the dice. This simulator lets you turn the dials real apps use (temperature, top-k, top-p and a frequency penalty), watch the probabilities change and write sentences pick by pick.

How it works

  • The grey text is the prompt. Each bar is a word (strictly, a token) the model could write next, with its chance of being picked. 🎲 Pick a word draws a random number between 0 and 1 and takes the word it lands on in the strip below the bars. 🎲 Roll ×20 does 20 picks from the same point without writing anything, so you can see how often each word wins.
  • Every job is a short list of tasks: some ask you to set the dials to a goal (they tick themselves when you get there), others are questions. Stars: one for finishing, one for every answer right first time, one for no hints.

The maths (exactly what real systems do)

  • Softmax with temperature: chance of word i = ezᵢ/T ÷ Σ ezⱼ/T, where z are the scores (logits) and T the temperature. T = 0 is treated as greedy decoding: the highest score always wins. Press Show the maths to see every number.
  • Top-k keeps the k highest-probability words and sets the rest to zero. Top-p (nucleus sampling, Holtzman and others, 2019) keeps the smallest set of top words whose probabilities add up to at least p. The survivors are then renormalised so they add up to 100%. The order is temperature, then top-k, then top-p, as in Hugging Face Transformers.
  • Frequency penalty (as documented by OpenAI): each word's score is lowered by the penalty × the number of times it has already appeared in the text the model has written. The prompt itself is not counted here.
  • Entropy = −Σ p log₂ p, in bits. 2entropy is the "effective number of choices"; perplexity applies the same idea across a whole text.
  • Whole text is the product of the model's own probabilities (at T = 1, no filters) for every word written, shown with its natural log.

What is illustrative

The scores themselves are not from a real model: they are hand-written numbers chosen to behave the way real models do (a confident top answer with plausible rivals, a long tail of nonsense, loops that greedy decoding falls into). A real model scores its entire vocabulary, often 50,000 to 200,000 tokens, using a neural network that reads the whole prompt; here each point in a sentence has a short list of candidates. The way of splitting words into tokens in level 4 is an example; every model's tokenizer differs. Everything that happens to the scores after that, the probabilities, filters, sampling and penalties, is calculated exactly as described above.