AI engineering path · How models behave · Topic 2 of 12

Sampling: temperature, top-p and determinism

Not started

At every step the model returns a score (a logit) for every token in its vocabulary. A softmax turns those scores into probabilities, and a sampler picks one token. Sampling settings change only that last step: the model's knowledge and the prompt are untouched, but which of the likely continuations you get, and how adventurous it is, depends on them.

Why it matters: The same prompt can give different answers on every call. Knowing why tells you when to turn the randomness down, when to leave it, and why you can never rely on it being zero.

  1. 1The model gives odds, not an answer

    Each step produces a probability for every token. A sampler then picks one.

  2. 2Temperature reshapes the odds

    Low temperature sharpens the distribution towards the top choice; high temperature flattens it so unlikely tokens get picked more often.

  3. 3Top-p cuts off the tail

    Nucleus sampling keeps only the smallest set of tokens whose probabilities add up to p, then samples from those.

  4. 4Zero is not a guarantee

    Even at temperature 0, providers do not promise identical output for identical input. Design and test for variation.

1

How it works

The explanation, with examples you can run.

Temperature

Temperature divides the scores before the softmax. Below 1 it exaggerates the gaps, so the top token wins more often; above 1 it shrinks them, so less likely tokens get a real chance. As temperature approaches 0, sampling becomes greedy decoding: always take the single most likely token.

Top-p and top-k

Top-k keeps the k most likely tokens and discards the rest. Top-p (nucleus sampling, from Holtzman et al.) keeps the smallest set of tokens whose probabilities add up to p. Both exist to cut off the long tail of unlikely tokens that produce nonsense when sampled; top-p adapts to how confident the model is at each step. A common recommendation is to adjust temperature or top-p, not both at once.

Run this to see both on a made-up distribution for the word after "The capital of France is". The random generator is seeded so the counts are repeatable.

⌘/Ctrl + Enter
OutputNot run yet

Press Run to see what your code prints, in order.

Runs in this browser, in an isolated frame with no access to this page. console.log, info, warn and error are shown; code runs as a classic script, so use an async function for await.

Choosing settings

  • One right answer (classification, extraction, routing, code that must compile): low temperature, or the provider's recommended default, plus structured output.
  • Many good answers (brainstorming, varied copy, test data): higher temperature, and generate several candidates if you then pick the best.
  • Defaults are reasonable. Many teams never touch these knobs and get good results by improving the prompt instead.

At the time of writing, some providers have stopped accepting custom temperature or top-p values on their newest models, especially those that reason before answering. Check the API reference for the model you use instead of assuming every knob exists.

Determinism is a property you build, not a setting

Even at temperature 0, providers say results will not be fully deterministic: requests are batched with others on shared hardware, and floating-point arithmetic is not associative, so near-ties between tokens can resolve differently. One flipped token early on changes everything after it.

If you need repeatable behaviour:

  • Store the output the first time and reuse it (cache by a hash of model, prompt and settings) when the same input must give the same answer.
  • Constrain the output (fixed labels, a JSON schema) so variation cannot change the meaning.
  • Write tests and evaluations that check properties ("contains the order id", "label is one of five", "grader score at least 4") rather than exact strings, and run them several times.
  • Pin the model version where the provider allows it, and re-run your evaluations before switching.
2

Check yourself

3 questions. Get them all right to finish this topic.

  1. 1.You classify support tickets into five fixed labels. Which sampling setup fits best?
  2. 2.You set temperature to 0 and send the same prompt twice. The answers differ slightly. What is the most likely explanation?
  3. 3.With top-p = 0.9, which tokens can be sampled?

0 of 3 right so far. Get all three to finish this topic.