Top-k Sampling › How Top-k Sampling Works
Language AI HandbookAll chapters Top-k Sampling › How Top-k Sampling WorksOne section · Sections in this chapter3 / 13 ↓
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
Now that we understand the problem, let us build up the solution piece by piece. The core insight behind top-k sampling is deceptively simple: if most of the probability mass concentrates in a handful of tokens, why not just sample from those and ignore the rest?
The elegance of this solution is that it makes almost no assumptions about the shape of the distribution. It does not try to model why probability falls off or where the "natural" cutoff should be. It simply imposes a hard count-based boundary and accepts the consequences. This makes top-k both easy to understand and easy to implement, which explains why it became so widely adopted despite its simplicity.
Think of top-k sampling as a casting director who says: "I will only consider actors in the top of my ranking. Everyone else is not being called back, regardless of how close they were to the boundary." The director does not re-evaluate the top actors based on who was excluded; they simply rescale their assessments of the remaining candidates. Someone ranked 3rd out of 50,000 is still ranked 3rd out of the top , and their relative standing compared to other top- candidates is unchanged.
This analogy also highlights an important subtlety: the excluded tokens receive zero probability, not some small residual probability. This is a hard cut, not a soft fade. The benefit is that we guarantee no sampling from the tail. The cost is that tokens just outside the boundary (ranked and ) receive zero probability even if they were only slightly less probable than the token at rank . Whether this boundary effect matters in practice depends on how steeply the probability drops off near rank .
The Intuition: Focus on What Matters
Think about how you complete sentences. Given "The capital of France is", you do not mentally consider every possible word in English. You immediately focus on a small set of plausible continuations: "Paris", maybe "a", "the", or "known". Your mental probability distribution is not uniform across all words, and neither is the model's. Top-k sampling formalizes this intuition by explicitly restricting attention to the most likely candidates.
The approach works in five steps:
- Compute probabilities: The model outputs a distribution over all vocabulary tokens
- Rank by likelihood: Sort tokens from most to least probable
- Keep the top k: Select only the highest-probability tokens
- Zero out the rest: Set all other token probabilities to exactly zero
- Renormalize and sample: Rescale the remaining probabilities so they sum to 1, then sample
Top-k sampling restricts the sampling space to the most probable tokens. After zeroing out tokens outside the top-, the remaining probabilities are renormalized before sampling.
From Intuition to Mathematics
Let us formalize this process. At generation step , the model has seen context (all previous tokens) and outputs a probability for each possible next token . This distribution spans the entire vocabulary, but we only want to sample from the top performers.
First, we need to identify which tokens make the cut. We define as the set containing the tokens with highest probability. Formally, where and every token outside has probability no greater than the smallest element of . If the vocabulary has 50,000 tokens and , then contains exactly the 50 most likely tokens for this specific context.
Now we need to handle a subtle but important issue: after removing tokens from consideration, the probabilities of the remaining tokens no longer sum to 1. A probability distribution must sum to 1 for sampling to work correctly. If the top- tokens collectively hold 85% of the probability mass, the removed tokens held the other 15%, and our truncated distribution sums to only 0.85. The solution is renormalization: we divide each remaining probability by the total mass of the kept tokens, scaling everything up so the distribution is valid again.
To compute the renormalization constant, we sum the probabilities of all top- tokens:
where:
- : the set of tokens with highest probability
- : the model's original probability for token given context
- : the total probability mass held by the top- tokens, which acts as a normalizing constant
This gives us the formal definition of top-k sampling. Given the original distribution and the normalization constant, the truncated distribution assigns probability:
where:
- : a candidate token at position
- : the context (all tokens before position )
- : the original probability the model assigns to token given the context
- : the set of tokens with highest probability under
- : the normalization constant, which equals the sum of probabilities of the top- tokens
- : the renormalized probability used for sampling
Why does this formula make sense? Notice that for tokens inside , dividing by (which is less than 1) makes each probability larger than the original. This is correct: we are redistributing the probability that was held by the excluded tokens and giving it proportionally to the included tokens. For tokens outside , the formula assigns zero. The resulting distribution sums to exactly 1 because .
Why Renormalization Preserves Relative Probabilities
A necessary property of this construction is that renormalization preserves the relative likelihood of tokens within the top-. This matters because the model's ranking captures important information about which tokens are more semantically appropriate than others, and we want to honor that information.
Consider two tokens: "Paris" with original probability 0.30 and "the" with probability 0.15. In the original distribution, "Paris" is exactly twice as likely as "the".
After truncation, suppose the top- tokens have cumulative probability . The renormalized probabilities become:
- "Paris":
- "the":
Notice that . The ratio is preserved! This happens because we divide both probabilities by the same constant . Renormalization scales all probabilities equally, maintaining their relative ordering and ratios.
This property matters because it means top-k sampling respects the model's preferences among plausible tokens. We are not arbitrarily reweighting tokens; we are simply restricting which tokens can be sampled while honoring the model's ranking within that restricted set. If you trust the model's learned distribution over the top candidates, renormalized top-k sampling samples from that distribution faithfully.
The key insight is that top-k sampling changes the support of the distribution (which tokens have non-zero probability) without changing the shape of the distribution over the supported tokens. The relative probabilities among kept tokens are identical to what they would be under the original distribution, up to the constant scaling factor .
Visualizing the Truncation
The following visualization shows this process concretely. On the left, we see the original distribution over the top 20 tokens. On the right, we see the truncated distribution after keeping only tokens and renormalizing.

