Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
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.

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.

The shortcut

Facts cold, ideas warm. Extraction, classification and support answers want low temperature. Brainstorming and creative writing can run warmer. For top-p, walk down the list adding probabilities until you reach p.

Top-p = 0.7 on the table aboveStep 1 of 4
  1. Start with the favourite

    shipped: running total 0.60. Below 0.7, keep going.

The trap is changing several dials at once. Adjust one, test, then move on; many teams leave top-p alone and tune only temperature.

Drills

Answer fast — trust the rule.

quick drill

Using the table, which tokens survive top-k = 3?

quick drill

Using the table, which tokens survive top-p = 0.5?

quick drill

Which setting suits extracting order numbers from emails?

quick drill

What does raising the temperature do?

Remember

  • Top-k and top-p pick the shortlist; temperature picks how boldly.
  • Facts cold, ideas warm.
  • Top-p: add probabilities from the top until you reach p.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainTwo features, two settings

Pedal Point uses the model for two jobs: pulling the order number and product name out of customer emails, and brainstorming names for a new line of bike bags. Recommend a sampling setting for each job and explain your reasoning using what temperature and top-p do to the model's choices.