Top-k Sampling › Choosing the Right Value of k

Michael BrenndoerferUpdated July 29, 20256 min read
Language AI HandbookAll chapters
Part 28 of 67GPT Architecture
Chapter 203Top-k Sampling › Choosing the Right Value of k
Reading section 6 of 13 · Choosing the Right Value of k · 6 minSection 6 of 13 · 6 min

Book contents

Section 6 of 13 · 6 min read

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 kk significantly affects generation quality, and there is no universally optimal value. The best kk depends on the model, the task, the context, and even the individual prompt. Understanding the principles behind kk selection helps you make informed decisions rather than guessing.

The basic tension is between quality and diversity. Lower kk values restrict sampling to the model's most confident predictions, creating text that is safe and coherent but potentially monotonous. Higher kk 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 kk selection is in terms of coverage: how much of the probability mass does the top-kk set hold? For a factual prompt where the model is highly confident, the top 5 tokens might hold 90% of the probability mass. Choosing k=50k=50 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 k=50k=50 would be needed to capture a meaningful range of plausible continuations.

Think of coverage-based reasoning as calibrating kk to the distribution's shape. When the distribution is sharp (high confidence), a small kk captures most of the mass. When the distribution is flat (low confidence), you need a larger kk to stay in the region where the model finds things plausible. This is exactly the intuition behind nucleus (top-p) sampling, which adaptively selects kk based on cumulative probability. But even for fixed-kk sampling, understanding coverage helps you choose kk wisely for a given application.

Out[14]:
Visualization
Line plot showing diversity increasing and coherence decreasing as k increases, with optimal zone highlighted.
Trade-off between diversity and coherence as k varies. Lower k values produce more focused text but risk repetition. Higher k values increase diversity but may introduce occasional off-topic tokens. The shaded region represents typical values used in practice (k=30-100).

Guidelines for Selecting k

Consider these factors when choosing kk:

  • Task type: Creative writing benefits from higher kk (50-100) for variety. Factual responses work better with lower kk (10-30) for precision.

  • Context confidence: When the model is highly confident (one token dominates), even large kk won't introduce much diversity since top tokens have most of the mass anyway.

  • Generation length: Longer generations may need lower kk to prevent drift. Errors accumulate over many steps, and each improbable token can push the generation off course.

  • User expectations: Interactive applications often use kk=40-50 as a reasonable default that balances fluency with variety.

In[15]:
Code
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 coverage
Out[16]:
Console
Top-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 kk captures most of the probability mass. "2 + 2 =" concentrates probability in very few tokens, so k=10k=10 might capture 99%+ of the mass. Open-ended prompts like "Once upon a time" spread probability more evenly, requiring larger kk to maintain diverse sampling. Looking at coverage percentages is one of the most practical guides to choosing kk for a specific application.

Out[17]:
Visualization
Line plot showing cumulative probability curves for three prompts, showing different coverage rates.
Cumulative probability coverage as k increases for three different contexts. Confident predictions (blue) reach near-complete coverage with tiny k values. Open-ended contexts (green) require larger k to capture the same probability mass. The dashed lines show the 90% and 99% coverage thresholds.

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 kk to achieve the same coverage. This explains why a fixed kk works differently across contexts: the same kk value can mean very different things depending on how the probability mass is distributed. A value of k=50k=50 might cover 99.9% of the mass for a factual prompt and only 60% for a creative one.