short · 3 min
Top-p sampling
Samples only from the smallest set of tokens that covers p of the probability. Top-k is covered here too.
updated 23 Sept 2026
Picture a model about to write the word after "The". Almost anything could come next: "dog", "people", "house", "cat", hundreds of fine options. Now picture it writing the word after "The car". Far fewer words fit, and nearly all the probability sits on one or two of them.
Any rule for trimming the model's choices has to work for both.
Why trim at all
Picking tokens at random by weight (sampling) has a weak spot: the tail. Thousands of tokens each have a tiny chance, and together they add up. Pick from the tail once and the text can go somewhere it can't recover from. So we want to drop the tail before picking, but keep some randomness.
First try: keep the top k
The simple rule is top-k. Keep the k most likely tokens, rescale their chances so they add up to 1 again, and pick from those. GPT-2 did this.
Let's try k = 6 on our two moments. After "The", six is too few. In one worked example it cut perfectly good words like "people" and "house". After "The car", six is too many. It let in "down" and "a", which barely fit, and rescaling made them more likely than they were before.
One fixed number can't be right for both.
Keep enough to cover p
Top-p (also called nucleus sampling) cuts on probability instead of count. Sort the tokens from most to least likely, add up their chances as you go, and stop once the total reaches p. Keep that set, rescale, pick.
Say p = 0.9:
| Token | Probability | Running total |
|---|---|---|
| yes | 0.60 | 0.60 |
| maybe | 0.30 | 0.90 ← stop |
| no | 0.06 | 0.96 |
| sure | 0.02 | 0.98 |
| banana | 0.01 | 0.99 |
Only "yes" and "maybe" survive. After rescaling, "yes" gets two thirds of the chance and "maybe" one third.
Back to our two moments. With p = 0.92, the same worked example kept 9 words after "The" and only 3 after "The car". The set grows when the model is unsure and shrinks when it's confident, which is what we wanted. Across real text the kept set runs anywhere from one token to about a thousand.
When top-p was introduced in 2019, text sampled with p = 0.95 came closest to human writing on the authors' measures, and rarely got stuck repeating. Usual values sit between 0.9 and just under 1. You can also stack the two rules, say k = 50 to rule out very low-ranked tokens, and p = 0.95 on top.
Where it goes wrong
Set p (or k) too low and the output repeats the way always taking the top token does.
It's also easy to mix up with temperature. Temperature reshapes the whole list and leaves every token in play. Top-p leaves the shape of the top alone and cuts the tail off. Lots of setups use both.
And it may not hold up when temperature is high. A 2024 paper argues that top-p struggles there, producing incoherent or repetitive text, and offers min-p instead: keep tokens whose chance is at least some fraction of the top token's. Min-p is in Hugging Face Transformers and vLLM, if you run open models hot.
On the newest closed models you may not get to choose. As of 2026-09, Claude models released after Opus 4.6 accept top-p only at 0.99 or above, and reject any top-k. The provider decides how much tail to keep.
Further reading
- The Curious Case of Neural Text Degeneration, Holtzman et al., 2020. The paper that introduced top-p. Its first figure, with beam search looping and pure sampling drifting, is worth seeing.
- How to generate text, Patrick von Platen (Hugging Face), 2020, updated 2023. Where the "The" and "The car" example comes from, with code you can run.
- Turning Up the Heat: Min-p Sampling, Nguyen et al., ICLR 2025. Read the abstract if you like high temperatures on open models.
- Create a Message, Anthropic API reference, 2026. What Claude's top-p and top-k accept now.