# Top-p Sampling

> Picking only from the top tokens whose chances add up to a set share, like 0.9.

**Field:** Artificial Intelligence  
**Category:** LLMs  
**Also known as:** Nucleus Sampling, Top-p

## Think of it like

Think of it like you only pick from the best snacks. Stop once they fill most of the plate.

## Definition

Top-p sampling limits the draw to the smallest group of likely tokens whose chances add up to p. With p set to 0.9, the model sorts tokens from most to least likely and keeps them until their chances reach 90 percent. The long tail of unlikely tokens is dropped. When the model is sure, few tokens are kept; when it is unsure, more are. It is also called nucleus sampling.

## Example

For 'The sky is', the top token 'blue' has 0.85 and 'clear' has 0.07. With top-p at 0.9, only those two are kept, and every odd word is out.

## Why it matters

It cuts off nonsense words while still allowing variety. That is why many tools turn it on by default.

## Common mistake

Changing top-p and temperature together and then not knowing which one fixed the problem. Change one at a time.

## Three principles

- **Sort by chance.** Most likely first.
- **Keep until p.** Like 90 percent in total.
- **Size adapts.** Few when sure, more when not.

## Related terms

- [Top-k Sampling](https://learn.benibix.com/term/ai/top-k-sampling)
- [LLM Temperature](https://learn.benibix.com/term/ai/temperature)
- [LLM Sampling](https://learn.benibix.com/term/ai/sampling)

## Told apart from

- [Temperature vs Top-p Sampling](https://learn.benibix.com/compare/temperature-vs-top-p-sampling): People often think temperature and top-p are two names for one setting. Temperature changes the shape of all the chances, making the top word stronger or the field flatter. Top-p does not reshape them; it cuts off the long tail of unlikely words. Most guides say to change one and leave the other alone.
- [Top-k vs Top-p Sampling](https://learn.benibix.com/compare/top-k-vs-top-p-sampling): People think top-k and top-p are the same cut, and they are not. Top-k always keeps a set number of tokens, such as 40. Top-p keeps tokens until their chances add up to a share, such as 0.9. So top-p keeps a few tokens when the model is sure, and many when it is not. Top-k keeps 40 either way.

## Taught in

- [Temperature and Sampling](https://learn.benibix.com/learn/ai-llms/temperature-and-sampling): LLMs, 3/15

![The same sorted bars of next-word chances, twice. Top-k keeps the first three bars. Top-p keeps bars from the top until their chances add up to 0.9.](https://learn.benibix.com/diagrams/llm-top-k-top-p.svg)

---

Source: https://learn.benibix.com/term/ai/top-p-sampling
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-18
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
