Nucleus Sampling › A Worked Example

Michael BrenndoerferUpdated July 30, 20257 min read
Language AI HandbookAll chapters
Part 28 of 67GPT Architecture
Chapter 204Nucleus Sampling › A Worked Example
Reading section 5 of 14 · A Worked Example · 7 minSection 5 of 14 · 7 min

Book contents

Section 5 of 14 · 7 min read

Nucleus Sampling › A Worked ExampleOne section · Sections in this chapter5 / 14 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Abstract formulas come alive with concrete numbers. Let's trace through nucleus sampling step by step with a realistic example.

Setting Up the Problem

Suppose a language model predicts the next token after "The weather is" and produces the following probability distribution:

Token probability distribution for predicting the next word after "The weather is".
TokenProbability
nice0.35
good0.25
bad0.15
great0.10
terrible0.05
wonderful0.04
cold0.03
hot0.02
(other tokens)0.01

This distribution is already sorted by probability. The model strongly favors positive weather descriptions, with "nice" and "good" together accounting for 60% of the mass.

Finding the Nucleus

With p=0.9p = 0.9, we walk through the tokens from highest to lowest probability, keeping a running sum:

Cumulative probability accumulation with p=0.9 threshold. Tokens 1-5 form the nucleus.
StepTokenProbabilityCumulative SumIn Nucleus?
1nice0.350.35✓
2good0.250.60✓
3bad0.150.75✓
4great0.100.85✓
5terrible0.050.90✓ (threshold reached)
6wonderful0.04.✗ (excluded)
7cold0.03.✗ (excluded)

At step 5, our cumulative sum hits exactly 0.90, meeting the threshold. We stop here. The nucleus is {"nice", "good", "bad", "great", "terrible"}, containing 5 tokens.

Notice what happened: we didn't need to decide in advance how many tokens to include. The distribution itself determined that 5 tokens were needed to capture 90% of the probability mass. If we had used top-k=5, we would get the same result here, but only by coincidence. Change the distribution slightly and top-k=5 would fail; nucleus sampling at p=0.9p = 0.9 would adapt.

Notice also what got excluded: "wonderful", "cold", and "hot" are all perfectly reasonable weather descriptions in English. The model just assigned them less probability in this context, so they fall below the threshold. This is the right call. The model's distribution reflects its training, and if it thinks "wonderful" is less likely than "terrible" here, we should respect that judgment.

Renormalizing for Sampling

The five tokens in our nucleus have probabilities summing to 0.90. To sample from a valid probability distribution, we divide each by this sum:

Renormalization divides each probability by the nucleus sum (0.90) to create a valid distribution.
TokenOriginal P(x)P(x)CalculationRenormalized P′(x)P'(x)
nice0.350.35÷0.900.35 \div 0.900.389
good0.250.25÷0.900.25 \div 0.900.278
bad0.150.15÷0.900.15 \div 0.900.167
great0.100.10÷0.900.10 \div 0.900.111
terrible0.050.05÷0.900.05 \div 0.900.056

You can verify: 0.389+0.278+0.167+0.111+0.056=1.0010.389 + 0.278 + 0.167 + 0.111 + 0.056 = 1.001 (the small discrepancy is rounding). We now have a valid probability distribution over just five tokens.

Out[3]:
Visualization
Cumulative probability curve reaching 0.9 threshold at token 5.
Cumulative probability as we add tokens in order of decreasing probability. The horizontal line marks p=0.9, and the vertical line shows where we stop (after 5 tokens).
Bar chart comparing original versus renormalized probabilities.
Original probabilities (light bars) compared with renormalized probabilities (dark bars) for the nucleus tokens.

The left panel shows how cumulative probability grows as we add tokens. The curve rises steeply at first (the top tokens contribute most of the mass) then flattens. We stop when we cross p=0.9p = 0.9. The right panel shows how renormalization scales up each probability proportionally, preserving relative rankings while creating a valid distribution.

Comparing to Top-k

In this particular case, top-k=5 would include the same tokens. But consider what happens if the model were 90% confident in a single token. Say "Paris" has 0.90 probability and everything else shares the remaining 0.10.

With nucleus sampling at p=0.9p = 0.9, we'd include just "Paris" (0.90 meets the threshold immediately). With top-k=5, we'd include "Paris" plus four tokens that collectively have only 10% probability. Those four tokens are noise that nucleus sampling correctly excludes.

This is the adaptive behavior that makes nucleus sampling effective: it contracts when the model is confident and expands when the model is uncertain.

How Different Values of p Change the Result

Let's revisit our weather example to see how the nucleus changes as we vary pp. With p=0.6p = 0.6, we only need "nice" and "good" (cumulative sum: 0.60). With p=0.75p = 0.75, we need to add "bad" as well. With p=0.85p = 0.85, we add "great". With p=0.9p = 0.9, we add "terrible". With p=0.94p = 0.94, we bring in "wonderful" as well.

Each threshold draws the line at a different point in the distribution. The choice of pp is ultimately a statement about how cautious you want to be: how much of the distribution's mass must you capture before you feel comfortable sampling? A pp of 0.9 says "I need the 90% core before sampling." A pp of 0.99 says "I need nearly everything." The first is conservative and produces coherent text; the second approaches pure sampling and produces more varied but potentially less coherent text.