Nucleus Sampling › A Worked Example
Language AI HandbookAll chapters 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 |
|---|---|
| nice | 0.35 |
| good | 0.25 |
| bad | 0.15 |
| great | 0.10 |
| terrible | 0.05 |
| wonderful | 0.04 |
| cold | 0.03 |
| hot | 0.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 , we walk through the tokens from highest to lowest probability, keeping a running sum:
| Step | Token | Probability | Cumulative Sum | In Nucleus? |
|---|---|---|---|---|
| 1 | nice | 0.35 | 0.35 | ✓ |
| 2 | good | 0.25 | 0.60 | ✓ |
| 3 | bad | 0.15 | 0.75 | ✓ |
| 4 | great | 0.10 | 0.85 | ✓ |
| 5 | terrible | 0.05 | 0.90 | ✓ (threshold reached) |
| 6 | wonderful | 0.04 | . | ✗ (excluded) |
| 7 | cold | 0.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 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:
| Token | Original | Calculation | Renormalized |
|---|---|---|---|
| nice | 0.35 | 0.389 | |
| good | 0.25 | 0.278 | |
| bad | 0.15 | 0.167 | |
| great | 0.10 | 0.111 | |
| terrible | 0.05 | 0.056 |
You can verify: (the small discrepancy is rounding). We now have a valid probability distribution over just five 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 . 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 , 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 . With , we only need "nice" and "good" (cumulative sum: 0.60). With , we need to add "bad" as well. With , we add "great". With , we add "terrible". With , we bring in "wonderful" as well.
Each threshold draws the line at a different point in the distribution. The choice of 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 of 0.9 says "I need the 90% core before sampling." A 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.