Skip to the workspace
Pebellslearn by building
Tokens, Context and Sampling  ›  Lesson

Temperature, Top-k and Top-p

Sampling Parameters 12 minSampling parameters decide how the next token is picked from the model's probabilities: top-k and top-p trim the candidate list, and temperature sets how adventurous the pick is.

ai engineer / Temperature, Top-k and Top-p

Explore each scene at your own pace.

01 · The rule

The rule

Temperature, Top-k and Top-p

Ask the Pedal Point assistant the same question twice and you may get two differently worded answers. Ask it to pull an order number from an email and you want the same answer every time. The difference comes from a few settings that control how the model picks each token.

The rule

Top-k and top-p pick the shortlist; temperature picks how boldly.

At every step the model produces probabilities for the next token. Say the text so far is "Your order has", and the top candidates are:

token probability
shipped 0.60
arrived 0.25
been 0.10
melted 0.05

Sampling means choosing from these. Three dials shape the choice:

  • Top-k keeps only the k most likely tokens. With k = 2 the shortlist is shipped and arrived.
  • Top-p (also called nucleus sampling) keeps the smallest group of top tokens whose probabilities add up to at least p. With p = 0.9: shipped 0.60, plus arrived makes 0.85, not enough, plus been makes 0.95. Three tokens.
  • Temperature reshapes the probabilities before picking. Low temperature sharpens them, so the favourite wins almost every time; at or near zero the model behaves close to always taking the top token. High temperature flattens them, so unlikely tokens get picked more often.

Providers expose different subsets of these dials, and names and ranges vary, so check the documentation of the one you use. Also, a very low temperature makes outputs much more consistent but is not always a guarantee of identical results on every system.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.