# Top-P Sampling

- **Canonical URL:** https://getaimath.com/dictionary/top-p-sampling/
- **Topics:** [LLM Fundamentals](https://getaimath.com/dictionary/category/llm-fundamentals/)
- **Last updated:** 2026-11-25

## What is Top-P Sampling?

**In one sentence:** Top-P sampling means the model picks from the smallest group of likely tokens whose chances add up to P, such as 90%.

Instead of a fixed number like Top-K, Top-P adapts. When the model is sure, the group might be just one or two tokens. When it is unsure, the group gets bigger. It is also called nucleus sampling.

![How ChatGPT & Claude Talk: Super-Powered Autocomplete — Simple cartoon diagram showing a sentence chopped into Lego word blocks (tokens), an AI brain weighing the odds of the next word ("mat" 75%, "couch" 18%, "roof" 5%), and snapping "mat" onto the end of the sentence.](https://getaimath.com/diagrams/llm-fundamentals.png)
*Napkin Diagram (LLM Fundamentals): Language models don’t plan whole essays at once—they play a super-smart game of "guess the very next word" over and over.*

### Quick Math / Rule of Thumb

top_p = 0.9 keeps only the smallest set of top tokens whose probabilities sum to 90%, dynamically trimming the long tail of unlikely words.

## What is an example of Top-P Sampling?

With top-p 0.9, if "Paris" alone has a 95% chance after "The capital of France is," the model only considers "Paris."

## Why does Top-P Sampling matter?

Top-P is a common, flexible way to balance creativity and good sense.

## Top-P Sampling vs. Temperature

Temperature rescales how flat or sharp all token probabilities are, while Top-P (nucleus sampling) cuts off the unlikely tail once top choices add up to probability P.

| Dimension | Top-P Sampling | [Temperature](https://getaimath.com/dictionary/temperature/) |
| --- | --- | --- |
| Mechanism | Keeps smallest top set summing to P (e.g., 0.9) | Sharpens (<1) or flattens (>1) logits |
| Effect on long tail | Hard-truncates low-probability tail tokens | Lowers tail odds but keeps rare words possible |
| Practical advice | Keep near 0.9–0.95 as a safety net | Tune one primarily (e.g., T=0.2 for code, 0.8 for writing) |

## Related Concepts

- [Top-K Sampling](https://getaimath.com/dictionary/top-k-sampling/): Top-K sampling means the model only chooses the next token from its K most likely options.
- [Repetition Penalty](https://getaimath.com/dictionary/repetition-penalty/): A repetition penalty is a setting that makes a model less likely to repeat the same words or phrases over and over.
- [Temperature](https://getaimath.com/dictionary/temperature/): Temperature is a setting that controls how random or creative a model's answers are.
- [Logits](https://getaimath.com/dictionary/logits/): Logits are the raw scores a model gives every possible next token before they are turned into probabilities.
- [Chain of Thought (CoT)](https://getaimath.com/dictionary/chain-of-thought-cot/): Chain of Thought is when an AI works through a problem step by step in writing before giving its final answer.
- [Softmax](https://getaimath.com/dictionary/softmax/): Softmax is a math function that turns a list of raw scores into probabilities that add up to 100%.

---
Source: The AI Math Dictionary (https://getaimath.com/dictionary/top-p-sampling/)
