Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Controlling the Output  ›  Lesson

Top-K and Top-P — Trimming the Choices

Top-P 14 minTop-k keeps only the k most likely next tokens and top-p keeps the smallest set whose probabilities add up to p, then the model samples from what is left.

Top-K and Top-P — Trimming the Choices

Even at a moderate temperature, the reply-drafting prompt now and then produced a bizarre word — "Your brake cables are now fully marzipan." The word was very unlikely, but not impossible, so once in thousands of tokens it got picked. Temperature reshapes the odds; top-k and top-p go further and throw the long tail of unlikely tokens away.

The rule

Top-k keeps a fixed number; top-p keeps enough to reach a share.

Both settings shrink the pool of candidate tokens before one is sampled. The kept tokens' probabilities are then rescaled so they add up to 1 again.

Take this next-token distribution after "Please check the":

brake 0.50   chain 0.25   bell 0.15   tyre 0.06   spoke 0.04
  • Top-k keeps the k most likely tokens. With k = 2, only brake and chain survive, rescaled to about 0.67 and 0.33.
  • Top-p (also called nucleus sampling) keeps the smallest set of top tokens whose probabilities add up to at least p. With p = 0.9: brake 0.50, plus chain = 0.75, plus bell = 0.90. Three tokens survive; tyre and spoke are dropped.

The difference shows up when the model's confidence changes. Suppose after "Please ring the" the distribution is bell 0.92, with everything else tiny. Top-p 0.9 keeps only bell, because it reaches 0.9 alone. Top-k 2 still keeps two tokens, dragging in an unlikely second choice. When the model is unsure, top-p keeps more tokens; when it is sure, fewer. It adapts; top-k does not.

The shortcut

Picture the tokens as a ranked list and draw a cut line:

  • Top-k — cut after position k, always.
  • Top-p — walk down, adding probabilities, and cut as soon as the running total reaches p.
Applying top-p = 0.8 to the "check the" listStep 1 of 4
  1. Rank

    brake 0.50, chain 0.25, bell 0.15, tyre 0.06, spoke 0.04.

A practical habit: adjust either temperature or top-p, not both at once, so you can tell which change had which effect. Many tools default top-p to 1, which keeps every token and leaves the job to temperature.

Drills

Answer fast — trust the rule.

quick drill

With top-k = 3, how many candidate tokens are kept?

quick drill

Using the list brake 0.50, chain 0.25, bell 0.15, tyre 0.06, spoke 0.04, which tokens does top-p = 0.7 keep?

quick drill

The model gives one token a probability of 0.95. What does top-p = 0.9 keep?

quick drill

Why is top-p often preferred over top-k?

Remember

  • Top-k keeps a fixed number; top-p keeps enough to reach a share.
  • Shortcut: rank, add up, cut at the line, then sample from what is left.
  • Trap: tuning temperature and top-p together and not knowing which one mattered.

Practice — level up

0/1 solved

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

ExplainK or P?

Explain the difference between top-k = 3 and top-p = 0.9 using a next-token distribution where one token has 0.92 probability, and say which one adapts better to how confident the model is.