Top-P (Nucleus Sampling)
Latest update: 26/04/30
Back to › Architecture & Technical
Definition
Top-P, also called nucleus sampling, is a setting that controls how the AI selects the next word by limiting its choices to only the most probable options – letting you tune the balance between creative variety and focused, predictable output.
What Is Top-P (Nucleus Sampling)?
Every time an AI generates a token, it considers the full range of possible next words and assigns a probability to each one. Top-P is a way of deciding which part of that range to sample from.
Instead of considering all possible tokens – including highly unlikely ones that could derail the output – or only ever picking the single most likely token, Top-P draws a boundary. It takes the smallest group of top-ranked tokens whose combined probabilities add up to a set value (like 0.9 or 0.95), and samples only from that group.
“Nucleus” refers to that core group – the nucleus of high-probability options. Sampling from it keeps outputs coherent while still allowing meaningful variety.
💡 How Does It Work?
Imagine the model has 50,000 possible next tokens. For the current position in a sentence, some are highly probable (“the,” “a,” “this”), some are moderately probable (“however,” “instead”), and most are vanishingly unlikely (“porcupine,” “zeppelin,” “%”).
With Top-P set to 0.9, the model adds up probabilities from the top down until the cumulative total reaches 90%. That might be the top 20 tokens, or it might be just the top 5 – it depends on how concentrated the probability distribution is at that moment. The model then randomly selects from only those tokens.
Think of it like a restaurant with 200 items on the menu. Top-P says: “Only choose from the dishes that together account for 90% of what this restaurant actually sells.” You get variety within reason – not a random item from the back of the kitchen.
Why It Matters for Your Prompts
Top-P is often used alongside temperature, and the two work differently. Temperature scales the probability distribution – spreading it out or compressing it. Top-P cuts it off – removing low-probability options from consideration entirely regardless of how temperature has scaled things.
In most consumer AI tools, you won’t directly control Top-P – it’s set by the platform. But if you’re using an API or a tool that exposes generation parameters, adjusting Top-P gives you a different kind of control than temperature alone.
Lower Top-P (like 0.7 or 0.8) narrows the vocabulary pool significantly – outputs become more focused, repetitive, and safe. Higher Top-P (like 0.95 or 1.0) keeps a wide range of tokens in play – outputs feel more natural and varied, but occasionally veer in unexpected directions.
For most creative and conversational tasks, a Top-P around 0.9–0.95 paired with a moderate temperature is a common starting point. For highly structured outputs – structured data extraction, consistent formatting – lower values of both can help.
🌐 Real-World Example
A developer is building a story generation tool. With Top-P set high (0.98), the output is lively and surprising – but occasionally produces sentences that go off in odd directions or use unusual word choices that break immersion.
She lowers Top-P to 0.85. The outputs become more grounded – still varied, but drawing from a tighter pool of likely vocabulary for the genre. The weird outlier sentences disappear.
She didn’t change the story prompt or the temperature. Just the nucleus of tokens the model was choosing from – and that was enough to fix the problem.
Related Terms
- Temperature – The companion setting to Top-P; temperature scales the probability distribution while Top-P cuts off the tail, and the two are often adjusted together.
- Inference – Top-P is applied during inference, at the token sampling step that determines what gets generated.
- Logits – Logits are the raw scores that get converted to probabilities before Top-P filtering is applied.
- Large Language Model (LLM) – All major LLMs support Top-P as a generation parameter, though default values vary by model and platform.
- Hallucination – Very high Top-P settings can increase the chance of low-probability – and potentially incorrect – tokens making it into the output.
Frequently Asked Questions
Should I use Top-P or temperature to control output randomness?
Use both – they control different things. Temperature scales the whole probability distribution (making it flatter or steeper). Top-P then cuts off the low-probability tail after that scaling. A common pattern: set temperature to control overall creativity, then use Top-P to prevent genuinely unlikely tokens from appearing. Most platforms default to sensible values for both; adjust them together if you need finer control.
What’s the difference between Top-P and Top-K?
Top-K is a simpler version: it limits sampling to the top K tokens by rank, regardless of their probabilities. Top-P is more adaptive – the size of the candidate pool changes based on the shape of the probability distribution at each step. In a highly concentrated distribution, Top-P might choose from just 5 tokens; in a flat distribution, from 50. That adaptivity makes Top-P more popular than Top-K in modern systems.
What happens if I set Top-P to 1.0?
Setting Top-P to 1.0 means all tokens remain in consideration – no nucleus trimming. The full probability distribution is in play. This gives the model maximum latitude and is sometimes done intentionally for highly creative tasks. It also means very unlikely tokens can occasionally appear, which can produce surprising outputs – for better or worse.
Is Top-P the same as top-p in every AI system?
The parameter name varies. OpenAI calls it top_p. Anthropic calls it top_p too. Some systems call it nucleus_sampling_p or just p. The underlying mechanism is the same – sampling from the smallest set of tokens that sum to the specified probability threshold. If you see a parameter between 0 and 1 that isn’t temperature, it’s likely nucleus sampling.
References
- Holtzman, A. – The Curious Case of Neural Text Degeneration (2019, University of Washington) – The paper that introduced nucleus sampling and demonstrated why it outperforms both greedy decoding and top-K sampling.
- Hugging Face – Text Generation Strategies – Practical documentation covering temperature, Top-P, Top-K, and their interactions in text generation.
Further Reading
Author Daniel: AI prompt specialist with over 5 years of experience in generative AI, LLM optimization, and prompt chain design. Daniel has helped hundreds of creators improve output quality through structured prompting techniques. At our AI Prompting Encyclopedia, he breaks down complex prompting strategies into clear, actionable guides.

