Top-p sampling
In short
Top-p sampling, also called nucleus sampling, is how a language model chooses the next piece of a reply. It keeps the likeliest options whose chances add up to a set share, then picks among that shortlist.
- Also called
- Nucleus sampling, top_p
- Topic
- How AI models work
- Level
- Some background helps
- First described
- Ari Holtzman, Jan Buys, Li Du, Maxwell Forbes and Yejin Choi named the method nucleus sampling in 2019, in "The Curious Case of Neural Text Degeneration"1.
At each step a language model assigns every next option a probability, then has to choose1. Always taking the single most likely option, or searching for the highest-probability continuation, often produces bland text that repeats itself1. Drawing from the whole list is no better. Holtzman and colleagues call the long low-probability end an unreliable tail, and pure sampling from it comes out incoherent1.
Top-p sampling, which they named nucleus sampling, keeps the smallest set of options whose probabilities add up to at least p, then samples inside that set1. The set is rebuilt at every step. It grows when many options are plausible and shrinks when the model is sure. They say the nucleus usually holds somewhere between one option and about a thousand1.
Temperature reshapes the whole distribution, and they argue it does not by itself cut that tail enough1. Top-k sampling, used by Fan, Lewis and Dauphin in 2018, always keeps a fixed count. Their story model kept the 10 most likely next words2. A fixed count is clumsy: too small and the text goes generic, too large and unlikely words are boosted when the remaining probabilities are rescaled1.
They had GPT-2 Large write 5,000 passages of up to 200 tokens from held-out WebText prefixes1. At p = 0.95, nucleus sampling scored perplexity 13.13, near human text at 12.38, and repeated 0.36% of the time against 0.28% for humans1. Beam search with 16 beams repeated 28.94% of the time1. On HUSE, their combined human and statistical score, nucleus was highest at 0.97. That figure used a smoothed stand-in, because plain truncation gave the method probability zero on most tokens1.
APIs expose the same control as top_p. OpenAI's completions API calls it nucleus sampling: 0.1 keeps tokens in the top 10% of probability mass, and the docs say to change top_p or temperature, not both3. Gemini's topP is the maximum cumulative probability of tokens to consider. The docs say Gemini uses nucleus sampling, sometimes with a fixed top-k, and that some models disallow top-k5. Anthropic's Messages API still defines top_p as nucleus sampling, but models released after Claude Opus 4.6 reject any value below 0.994.
Example
On OpenAI's completions API, top_p of 0.1 means the model may choose only among the tokens that together hold the top 10% of the probability, and must leave the rest out3.
Why it matters
It is the usual control for how wide the next-word shortlist is. A low p stays on likely phrasing. A p near 1 lets more of the tail through13.
Questions people ask
What is top-p sampling?
It is nucleus sampling. At each step the model keeps the smallest set of next-token options whose probabilities add up to at least p, then draws from that set. The set gets bigger when the model is unsure and smaller when it is confident1.
Is top-p the same as temperature?
No. Temperature reshapes every probability before the draw. Top-p throws away the low-probability tail and samples only inside what is left1. OpenAI's completions API says to change one or the other, not both3.
What does a top_p of 0.1 mean?
On OpenAI's completions API, 0.1 means only the tokens that make up the top 10% of probability mass can be chosen3. Holtzman and colleagues' main published comparison used a much higher cutoff, p = 0.951.
Can I still set top_p on Claude?
Anthropic's Messages API still describes top_p as nucleus sampling. Models released after Claude Opus 4.6 do not support setting it: a value of 0.99 or more is kept for compatibility, and anything lower returns an error4.
Related terms
Tools that use it
- ChatGPTOpenAI's general chatbot, the most used AI app in the UK and worldwide.
- ClaudeAnthropic's chatbot, strong on writing and on pointing out holes in an argument, with a tight free allowance.
- Google GeminiGoogle's chatbot, built into Android, Gmail and Docs, with a generous free plan.
Sources
- Holtzman et al. (2019), The Curious Case of Neural Text Degeneration, arXiv:1904.09751
- Fan, Lewis and Dauphin (2018), Hierarchical Neural Story Generation, arXiv:1805.04833
- OpenAI API reference, Create completion (temperature and top_p)
- Anthropic, Messages API reference (top_p)
- Google, Gemini API generateContent, GenerationConfig topP