Top K: Definition and Examples
Top K is a generation parameter that limits the model's choice to the K most probable tokens at each step, reducing incoherent responses while maintaining some diversity.
Full definition
Top K is a sampling parameter used during text generation by language models (LLMs). At each generation step, the model calculates a probability for each possible token in its vocabulary. The Top K parameter restricts this choice to the K tokens with the highest probabilities, excluding all others from selection.
Concretely, if Top K is set to 50, the model will only consider the 50 most probable tokens for the next word, then perform a random draw among these 50 candidates according to their respective probabilities. A Top K of 1 always selects the most probable token (deterministic generation), while a very high Top K allows the model to choose from a wide range of possibilities.
The main benefit of Top K is finding a balance between coherence and creativity. A value too low produces repetitive and predictable texts, while a value too high may introduce improbable tokens that make the text incoherent. In practice, Top K is often used in combination with other parameters like Temperature and Top P to fine-tune model behavior.
It is important to note that Top K has a limitation: it applies a fixed threshold regardless of context. In some situations, the model is very confident and only 5 tokens are truly relevant, while in other cases, 200 tokens could be valid continuations. This is why Top P (nucleus sampling) is often preferred, as it dynamically adapts to the probability distribution.
Etymology
The term 'Top K' comes from computer science and statistics, where 'top K' simply refers to the K best-ranked elements of a set. In the context of LLMs, it was popularized by the article by Fan et al. (2018) on text generation by sampling, then widely adopted by model APIs like GPT and Claude.
Concrete examples
Creative generation with a high Top K to encourage originality
Write a surrealist poem about rain. [Top_K = 100, Temperature = 0.9]
Factual response with low Top K to maximize accuracy
What is the capital of Australia? [Top_K = 10, Temperature = 0.2]
API Claude configuration for a professional chatbot
In the API parameters: { "top_k": 40, "temperature": 0.7 } for an assistant that stays coherent while varying its wording.
Practical usage
In prompt engineering, Top K is usually set via the model's API parameters rather than in the prompt itself. For factual or technical tasks, use a low Top K (10-40) combined with a low temperature. For creative tasks, increase Top K (50-100) with a higher temperature. In most cases, prefer Top P over Top K because it adapts better to context.
Related concepts
FAQ
What is the difference between Top K and Top P?
What Top K value should I use by default?
Can you combine Top K and Top P at the same time?
See also
How to use this prompt
- Copy the prompt with the button above.
- Paste it into ChatGPT, Claude or your favorite AI assistant.
- Replace the bracketed variables with your details, then refine the result.
About Prompt Guide
Prompt Guide is a free library of 2500+ ready-to-use prompts for ChatGPT, Claude and other AIs, with guides to learn prompting and tools to build and optimize your own prompts.
More definitions
Top P: Definition and Examples
Top P, also known as nucleus sampling, is a generation parameter that controls the diversity of AI responses by limiting token selection to those with cumulative probability reaching a threshold P.
Transformer: Definition and Examples
Neural network architecture introduced in 2017 by Google, based on the attention mechanism, which forms the basis of all major language models.
Tree Of Thought: Definition and Examples
Tree of Thought (ToT) is an advanced prompting technique that allows a language model to explore multiple reasoning paths simultaneously, under
Trustworthy AI: Definition and Examples
Trustworthy AI refers to artificial intelligence designed to be reliable, ethical, transparent, and respectful of fundamental rights.
Vector Database: Definition and Examples
A vector database is a specialized database for storing, indexing, and searching numerical vectors (embeddings), enabling...
Video Understanding: Definition and Examples
Ability of an AI model to analyze, interpret, and extract relevant information from video content, combining visual, temporal, and often audio understanding.
Get new prompts every week
Join our newsletter.