Nucleus Sampling › Nucleus Sampling vs. Top-k: When to Use Which

Michael BrenndoerferUpdated July 30, 20252 min read
Language AI HandbookAll chapters
Part 28 of 67GPT Architecture
Chapter 204Nucleus Sampling › Nucleus Sampling vs. Top-k: When to Use Which
Reading section 9 of 14 · Nucleus Sampling vs. Top-k: When to Use Which · 2 minSection 9 of 14 · 2 min

Book contents

Section 9 of 14 · 2 min read

Nucleus Sampling › Nucleus Sampling vs. Top-k: When to Use WhichOne section · Sections in this chapter9 / 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

Both methods truncate the vocabulary, but they do so with different philosophies:

Comparison of top-k and nucleus sampling approaches.
AspectTop-kNucleus (Top-p)
Truncation criterionFixed countProbability mass
Adapts to contextNoYes
Parameter intuition"Keep this many options""Keep this much probability"
Risk of over-truncationYes (when few tokens dominate)No
Risk of under-truncationYes (when probability spreads)No
Compute costSlightly lower (no cumsum)Slightly higher

In practice, nucleus sampling is preferred for creative text generation because of its adaptive behavior. Top-k remains useful when you want explicit control over the number of options or when slight computational savings matter at scale.

Some systems use both: first apply top-k as a coarse filter (e.g., k=100), then apply nucleus sampling within that set. This combines the efficiency of top-k with the adaptivity of top-p. The top-k pass quickly eliminates the very bottom of the vocabulary without needing a cumulative sum, and the subsequent nucleus pass makes the fine-grained probability-based cutoff within the reduced candidate set. For very large vocabularies (100,000+ tokens), this two-pass approach can reduce computation noticeably.

Another consideration is reproducibility. Top-k is deterministic in its selection set: the same distribution always produces the same kk candidates. Nucleus sampling is also deterministic in selection but can include different numbers of tokens across calls with the same pp, since the nucleus size is data-dependent. This flexibility is usually an advantage, but for debugging purposes, it helps to know that the nucleus size will vary across steps.