Top-p Sampling (Nucleus Sampling)

Posted on

April 16, 2026
|

By

Abhishek Singh
Interview Kickstart
|

Share via

Generative AI

[display_author_description]

Top-p sampling, also called nucleus sampling, is a text generation decoding strategy that samples the next token only from the smallest set of tokens whose cumulative probability mass exceeds a threshold p.

What is Top-p Sampling (Nucleus Sampling)?

In an autoregressive language model, each next-token step produces a probability distribution over the vocabulary. Greedy decoding always picks the most likely token, while pure sampling can pick from the full distribution and may produce low probability, low quality tokens. Top-p sampling constrains randomness by dynamically selecting a “nucleus” of tokens: sort tokens by probability, then keep adding tokens until their cumulative probability reaches p, for example p = 0.9. The model then samples from this truncated set, usually after renormalizing probabilities. Because the size of the nucleus changes per step, top-p adapts to the model’s uncertainty: when the distribution is sharp, the nucleus is small, and outputs become more deterministic. When the distribution is flat, the nucleus grows, preserving diversity without allowing extremely unlikely tokens.

Where top-p sampling is used and why it matters

Top-p sampling is widely used in generative AI applications such as chat assistants, story generation, and code completion to balance creativity and coherence. It is often paired with temperature, repetition penalties, and max token limits. Compared with top-k sampling, which keeps a fixed number of candidates, top-p tends to be more stable across contexts because it uses probability mass rather than a fixed cutoff. Choosing p too low can make outputs repetitive or overly safe, while choosing p too high can increase hallucinations and stylistic drift.

Examples

1) With p = 0.9, the model may sample among several plausible continuations for an open-ended prompt, while still avoiding rare tokens.

2) For factual Q and A, using a lower p, like 0.7 to 0.85, can reduce variability and improve consistency.

3) In creative writing, a higher p, like 0.95, can introduce more diverse phrasing while remaining readable.

FAQs

1. How is top-p different from top-k sampling?
Top-k keeps a fixed number of tokens, while top-p keeps a variable number whose probabilities sum to at least p.

2. Should I use top-p or temperature?
They control different effects. Top-p limits the candidate set, while temperature reshapes probabilities. Many systems use both.

3. Does top-p reduce hallucinations?
It can reduce extreme low probability tokens, but hallucinations can still occur. Grounding methods like RAG matter more.

4. What is a common default value for p?
Many APIs default around 0.9 to 0.95, but the best value depends on task and desired creativity.

Register for our webinar

Uplevel your career with AI/ML/GenAI

Loading_icon
Loading...
1 Enter details
2 Select webinar slot
By sharing your contact details, you agree to our privacy policy.

Select a Date

Time slots

Time Zone:

[domain_image]
Register for our webinar

Uplevel your career with AI/ML/GenAI

Loading_icon
Loading...
1 Enter details
2 Select webinar slot
By sharing your contact details, you agree to our privacy policy.

Select a Date

Time slots

Time Zone:

Contributors

Swaminathan Iyer

Director of Product at Interview Kickstart, ex-Media.net, XLRI alum, with deep experience in data-driven and ML/AI product development

[domain_shortcode_display]

IK Courses recommended

Rating icon 4.91

EdgeUp: Agentic AI + Interview Prep

Build AI agents, automate workflows, deploy AI-powered solutions, and prep for the toughest interviews.

Interview kickstart Instructors

Rishabh Misra

Principal ML Engineer/Tech Lead
Atlassian Logo
10 yrs
Rating icon 4.94

Applied Agentic AI Course

Master Agentic AI to build, optimize, and deploy intelligent AI workflows to drive efficiency and innovation.

Interview kickstart Instructors

Ahmed Elbagoury

Senior ML/Software Engineer
Google Logo
11 yrs
Rating icon 4.83

Applied Agentic AI for SWEs

Master Multi-Agent Systems, LLM Orchestration, and real-world application, with hands-on projects and FAANG+ mentorship.

Interview kickstart Instructors

Dipti Aswath

AI/ML Systems Architect
Amazon Logo
20 yrs

Ready to Enroll?

Get your enrollment process started by registering for a Pre-enrollment Webinar with one of our Founders.

Next webinar starts in

00
DAYS
:
00
HR
:
00
MINS
:
00
SEC

Register for our webinar

How to Nail your next Technical Interview

Loading_icon
Loading...
1 Enter details
2 Select slot
By sharing your contact details, you agree to our privacy policy.

Select a Date

Time slots

Time Zone:

Almost there...
Share your details for a personalised FAANG career consultation!
Your preferred slot for consultation * Required
Get your Resume reviewed * Max size: 4MB
Only the top 2% make it—get your resume FAANG-ready!

Registration completed!

🗓️ Friday, 18th April, 6 PM

Your Webinar slot

⏰ Mornings, 8-10 AM

Our Program Advisor will call you at this time

Register for our webinar

Transform Your Tech Career with AI Excellence

Transform Your Tech Career with AI Excellence

Join 25,000+ tech professionals who’ve accelerated their careers with cutting-edge AI skills

25,000+ Professionals Trained

₹23 LPA Average Hike 60% Average Hike

600+ MAANG+ Instructors

Webinar Slot Blocked

Interview Kickstart Logo

Register for our webinar

Transform your tech career

Transform your tech career

Learn about hiring processes, interview strategies. Find the best course for you.

Loading_icon
Loading...
*Invalid Phone Number

ⓘ Used to send reminder for webinar

By sharing your contact details, you agree to our privacy policy.
Choose a slot

Time Zone: Asia/Kolkata

Choose a slot

Time Zone: Asia/Kolkata

Build AI/ML Skills & Interview Readiness to Become a Top 1% Tech Pro

Hands-on AI/ML learning + interview prep to help you win

Switch to ML: Become an ML-powered Tech Pro

Explore your personalized path to AI/ML/Gen AI success

Your preferred slot for consultation * Required
Get your Resume reviewed * Max size: 4MB
Only the top 2% make it—get your resume FAANG-ready!
Registration completed!
🗓️ Friday, 18th April, 6 PM
Your Webinar slot
⏰ Mornings, 8-10 AM
Our Program Advisor will call you at this time

Transform Your Tech Career with AI Excellence

Join 25,000+ tech professionals who’ve accelerated their careers with cutting-edge AI skills

Join 25,000+ tech professionals who’ve accelerated their careers with cutting-edge AI skills

Webinar Slot Blocked

Loading_icon
Loading...
*Invalid Phone Number
By sharing your contact details, you agree to our privacy policy.
Choose a slot

Time Zone: Asia/Kolkata

Build AI/ML Skills & Interview Readiness to Become a Top 1% Tech Pro

Hands-on AI/ML learning + interview prep to help you win

Choose a slot

Time Zone: Asia/Kolkata

Build AI/ML Skills & Interview Readiness to Become a Top 1% Tech Pro

Hands-on AI/ML learning + interview prep to help you win

Switch to ML: Become an ML-powered Tech Pro

Explore your personalized path to AI/ML/Gen AI success

Registration completed!

See you there!

Webinar on Friday, 18th April | 6 PM
Webinar details have been sent to your email
⏰ Mornings, 8-10 AM
Our Program Advisor will call you at this time