Top-K: The Fixed Candidate Pool

** Prompt Engineering: Mastering AI Communication from Zero to Expert
Lesson Content
0% Complete

Top-K: The Fixed Candidate Pool

Learning Objectives:

(1) Define Top-K sampling.

(2) Explain how Top-K limits the next-token candidate pool.

(3) Distinguish between Top-K and Temperature as control mechanisms.

 

What Is Top-K?

Top-K sampling limits the model’s next-token selection to only the K most likely tokens from the full vocabulary. All tokens outside the top K are completely excluded — they have zero probability of being selected.

MENTAL MODELTop-K is like a talent show with a fixed number of contestants. If K = 50, only the 50 most likely next words are allowed on stage. The model then picks from those 50 (influenced by Temperature). Everyone else is sent home regardless of how plausible they might be.

 

Top-K: How It Works (Step by Step)

  1. Model generates probability scores for all ~50,000 tokens in vocabulary
  2. Probabilities are sorted in descending order
  3. Only the top K tokens are kept; all others are set to zero
  4. Temperature is applied to the remaining K tokens to shape the final selection
  5. One token is selected from the filtered, temperature-scaled distribution

 

Top-K Example

RankWordProbabilityTop-K=3 Status
1cat0.25✓ INCLUDED
2dog0.20✓ INCLUDED
3hat0.15✓ INCLUDED
4car0.10✗ EXCLUDED
5banana0.05✗ EXCLUDED
6+all others<0.05✗ EXCLUDED

 

Top-K vs Temperature: What’s the Difference?

AspectTemperatureTop-K
What it controlsHow the probabilities are weighted/scaledHow many candidates are available for selection
Effect of low valueStrong weighting to top token — predictableFewer candidates — more controlled
Use caseAdjust creativity levelSet a fixed ceiling on token variety
Typical values0.0–1.0 (or 2.0)10–100 (varies by model)

Course Outline