Top-k Sampling › Choosing the Right Value of k
Language AI HandbookAll chapters Top-k Sampling › Choosing the Right Value of kOne section · Sections in this chapter6 / 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
The choice of significantly affects generation quality, and there is no universally optimal value. The best depends on the model, the task, the context, and even the individual prompt. Understanding the principles behind selection helps you make informed decisions rather than guessing.
The basic tension is between quality and diversity. Lower values restrict sampling to the model's most confident predictions, creating text that is safe and coherent but potentially monotonous. Higher values allow the model to explore less certain territory, creating more varied text but risking occasional incoherence. This is not a tradeoff you can resolve once and forget; it lives at the heart of every generation decision.
One useful way to think about selection is in terms of coverage: how much of the probability mass does the top- set hold? For a factual prompt where the model is highly confident, the top 5 tokens might hold 90% of the probability mass. Choosing in this case would include 45 tokens that collectively hold only 10% of the mass. You are sampling mostly from the same small set of dominant tokens, with occasional low-probability diversions that may not add value. For a creative writing prompt where probability is spread more evenly, the top 5 tokens might hold only 30% of the mass, and would be needed to capture a meaningful range of plausible continuations.
Think of coverage-based reasoning as calibrating to the distribution's shape. When the distribution is sharp (high confidence), a small captures most of the mass. When the distribution is flat (low confidence), you need a larger to stay in the region where the model finds things plausible. This is exactly the intuition behind nucleus (top-p) sampling, which adaptively selects based on cumulative probability. But even for fixed- sampling, understanding coverage helps you choose wisely for a given application.

Guidelines for Selecting k
Consider these factors when choosing :
-
Task type: Creative writing benefits from higher (50-100) for variety. Factual responses work better with lower (10-30) for precision.
-
Context confidence: When the model is highly confident (one token dominates), even large won't introduce much diversity since top tokens have most of the mass anyway.
-
Generation length: Longer generations may need lower to prevent drift. Errors accumulate over many steps, and each improbable token can push the generation off course.
-
User expectations: Interactive applications often use =40-50 as a reasonable default that balances fluency with variety.
def analyze_top_k_coverage(probs, k_values):
"""Analyze what fraction of probability mass top-k captures."""
sorted_probs, _ = torch.sort(probs, descending=True)
coverage = {}
for k in k_values:
coverage[k] = sorted_probs[:k].sum().item()
return coverageTop-k Probability Coverage by Context Type: ============================================================ Prompt: 'The capital of France is' k= 5: 24.3% of probability mass k= 10: 35.9% of probability mass k= 20: 45.7% of probability mass k= 50: 54.9% of probability mass k=100: 62.4% of probability mass Prompt: 'Once upon a time in a land far' k= 5: 68.3% of probability mass k= 10: 79.8% of probability mass k= 20: 86.2% of probability mass k= 50: 92.8% of probability mass k=100: 95.4% of probability mass Prompt: '2 + 2 =' k= 5: 40.8% of probability mass k= 10: 52.5% of probability mass k= 20: 62.0% of probability mass k= 50: 70.5% of probability mass k=100: 76.0% of probability mass
The coverage analysis reveals an important pattern: when the model is confident, a small captures most of the probability mass. "2 + 2 =" concentrates probability in very few tokens, so might capture 99%+ of the mass. Open-ended prompts like "Once upon a time" spread probability more evenly, requiring larger to maintain diverse sampling. Looking at coverage percentages is one of the most practical guides to choosing for a specific application.

This visualization makes the intuition concrete. For confident predictions like "2 + 2 =", the curve shoots up almost vertically, reaching 99% coverage with just a handful of tokens. Open-ended prompts show a gentler slope, requiring larger to achieve the same coverage. This explains why a fixed works differently across contexts: the same value can mean very different things depending on how the probability mass is distributed. A value of might cover 99.9% of the mass for a factual prompt and only 60% for a creative one.