All posts

Top-k and top-p sampling

/AI/4 min read

Both cut off the long tail of unlikely tokens before sampling, and each fails where the other works. On a confident step, top-p at 0.9 keeps 4 tokens where top-k keeps 40; on a flat one it keeps 15,996 where top-k keeps 40 — and leaves 62.5% of the probability out.

A language model's vocabulary has tens of thousands of tokens, and at every step every one of them gets some probability. Almost all of those probabilities are tiny. But there are so many of them that, together, they add up — and sampling from the full distribution means occasionally drawing one.

That is the long tail, and it is where incoherent text comes from: not from the model's top choices, but from the thousands of near-zero options that, added up, are drawn surprisingly often.

Top-k and top-p are two ways of cutting the tail off before sampling. They differ in what they hold fixed.

Top-k: keep a fixed number

Keep the k most likely tokens, set the rest to zero, renormalise, sample. With k = 40, there are always exactly 40 candidates.

Top-p: keep a fixed share of probability

Sort tokens by probability and keep the smallest set whose probabilities add up to at least p. With p = 0.9, keep whatever the top tokens are that together cover 90% of the mass — maybe 3 tokens, maybe 3,000. Also called nucleus sampling.

The same rule on three different steps

The shape of the distribution changes from step to step. After "The capital of France is", nearly all the probability sits on one token. After "My favourite word is", it is spread over thousands.

Model three steps over a 50,000-token vocabulary, from very confident to very open:

steptop tokentop-p 0.9 keepstop-k 40 covers
confident67.1%4 tokens99.3% of the mass
typical43.8%27 tokens92.2%
open-ended8.8%15,996 tokens37.5%

Read the rows.

On the confident step, top-k is too loose. It keeps 40 candidates when four cover 90% of the probability. The other 36 are the tail it was supposed to remove; together they hold a few per cent of the mass — enough to be drawn now and then. Top-p adapts and keeps just the four.

On the open-ended step, top-p is too loose. Keeping 90% of a flat distribution means keeping almost sixteen thousand tokens, most of them individually absurd. Top-k caps the candidates at 40, and those 40 hold only 37.5% of the probability — the model has many reasonable options, and top-k picks among the best of them.

Neither rule is right on both. That is why they are usually used together.

EACH CUT FAILS WHERE THE OTHER WORKS confident step top-p: 4 top-k: 40 open-ended step top-k: 40 top-p: 15,996 top-p adapts to confidence; top-k caps the damage when there is none used together, the stricter of the two applies on every step
A fixed count is wrong on confident steps; a fixed share is wrong on flat ones.

Using both, with temperature

Most serving stacks apply the filters in a fixed order:

  1. Temperature scales the logits, sharpening or flattening the distribution.
  2. Top-k keeps at most k candidates.
  3. Top-p keeps the smallest set covering p of what remains.
  4. Renormalise and sample.

With both set, the stricter one wins on each step. On the confident step, top-p leaves 4 tokens. On the open-ended step, top-k leaves 40. The tail is cut either way.

The order with temperature matters. Raising the temperature flattens the distribution before the filters see it, so a higher temperature makes top-p keep more tokens. That is often what people want — more variety among plausible options — but it means the two settings are not independent dials.

Min-p, briefly

A newer alternative keeps every token whose probability is at least some fraction of the top token's. With min-p = 0.1, the cut-off is a tenth of whatever the top token has:

stepcut-offmin-p 0.1 keeps
confident6.71%2 tokens
typical4.38%4 tokens
open-ended0.88%10 tokens

It adapts in both directions: tight on confident steps like top-p, bounded on flat steps like top-k. The price is that it is stricter than either — on the open-ended step its 10 tokens hold only 25.7% of the probability — so it is usually paired with a somewhat higher temperature.

Typical settings

tasktemperaturetop-ktop-p
code, extraction, facts0 – 0.2——
general chat0.7400.9
creative writing0.9 – 1.050–1000.95

At temperature 0 the filters do nothing: greedy decoding already takes only the top token.

What to take away

Top-k keeps a fixed number of candidates; top-p keeps a fixed share of probability. On a confident step top-k is too generous — 40 tokens where 4 hold 90% of the mass. On a flat step top-p is too generous — 15,996 tokens to reach 90%.

Use both, so the stricter one applies on each step, and remember that temperature reshapes the distribution before either filter sees it.