AI & LLMs

Top-p Sampling

also: nucleus sampling

Sampling from the smallest set of tokens whose probabilities sum to p — adaptive randomness control.

Top-p (nucleus) sampling keeps the most-probable tokens until their cumulative probability reaches p (say 0.9), then samples within that set. Unlike top-k (a fixed count), the nucleus adapts: confident distributions shrink it, flat ones widen it. Combined with temperature, these knobs trade determinism against diversity — low for code and extraction, higher for ideation.

Worked example: top-p=0.9 keeps the smallest set of tokens whose probabilities sum to 0.9, then renormalizes and samples — on a confident step that might be 2 tokens, on an uncertain one 50. Gotcha: it adapts the cutoff to the distribution (unlike a fixed top-k), but very low top-p turns output repetitive and very high lets in noise; ~0.9 with moderate temperature is a common default.