What Are Top-k and Top-p in LLMs? How Do They Control AI Output?
Top-k and Top-p are sampling techniques that control which candidate tokens an LLM can choose from during generation. Understanding them is important for controlling creativity, diversity, and consistency in AI applications.
What Are Top-k and Top-p?
When an LLM generates text, it predicts probabilities for possible next tokens.
For example:
"The sky is..."
The model may assign probabilities to:
blue → 70%
cloudy → 15%
clear → 10%
green → 1%
others → 4%
Top-k and Top-p control which of these candidate tokens the model can consider.
What Is Top-k?
Top-k sampling limits the model to the k most probable tokens.
For example, if:
Top-k = 3
the model considers only the three highest-probability candidates.
This reduces the possibility of selecting unlikely tokens.
Lower k → More focused
Higher k → More diverse
What Is Top-p?
Top-p (Nucleus Sampling) selects the smallest group of tokens whose cumulative probability reaches a specified threshold.
For example:
Top-p = 0.90
The model considers enough of the highest-probability tokens to cover approximately 90% of the probability mass.
Unlike Top-k, the number of candidate tokens can change from one generation step to another.
Top-k vs Top-p
Top-k | Top-p |
|---|---|
Uses a fixed number of candidate tokens | Uses a probability threshold |
Example: k = 20 | Example: p = 0.9 |
Candidate count stays fixed | Candidate count can vary |
Controls candidate pool size | Controls probability mass |
Why Are They Important?
They can help control the balance between:
Predictability ↔ Diversity
For example:
Customer support → More controlled output
Creative writing → More diverse output
Brainstorming → More variety
Interview Tip
Don't confuse Top-k/Top-p with Temperature.
Temperature changes the probability distribution used for sampling.
Top-k/Top-p restrict the set of candidate tokens considered for sampling.
They can be used together, depending on the model/provider.
Key Takeaway
Top-k → Select from the k most probable tokens.
Top-p → Select from the smallest set of tokens whose cumulative probability reaches p.
These sampling techniques help AI Engineers control how focused, diverse, and creative LLM-generated responses can be.
Related articles
More in LLM
What Hardware Do You Need to Run a Local LLM?
Local LLM hardware requirements come down to memory. Learn the parameters × bytes rule, a worked 7B example, how quantization helps, and whether you need a GPU.
6 min readNov 30, 2026What Is a Reasoning Model? How "Thinking" AI Works
A reasoning model is an LLM that writes out intermediate thinking before it answers. Learn how it works, when it helps, and why it is slower and uses more tokens.
6 min readNov 28, 2026Small Language Models vs LLMs
What a small language model (SLM) is, how it differs from a large language model, and how to choose between them for cost, privacy and quality.
4 min readNov 24, 2026What Is a Transformer? Attention Explained Simply
A plain-English guide to the transformer, the design behind modern language models: what attention means, how it works step by step, and a tiny Python example you can run.
5 min readNov 18, 2026What Are Open-Source LLMs? Run One Locally With Ollama
What open-source LLMs are, why people run them locally, and how Ollama lets you download and use one with a few commands.
3 min readNov 9, 2026What Are Tokens and Tokenization in LLMs? Why Are They Important?
LLMs don't process text exactly like humans do. They process text as tokens. Understanding tokenization is essential for understanding context windows, API costs, model limits, and LLM performance.
2 min readAug 29, 2026
Comments
Sign in to join the discussion.


