P

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

TemperatureTop P (Nucleus Sampling)SamplingLogits

FAQ

What is the difference between Top K and Top P?
Top K selects a fixed number of tokens (the K most probable), while Top P dynamically selects tokens whose cumulative probabilities reach a threshold P. Top P adapts better to the context: it restricts more when the model is confident and broadens the choice when multiple options are valid.
What Top K value should I use by default?
A value between 40 and 50 is a good starting point for most uses. For very precise responses, go down to 10-20. For free creation, go up to 80-100. Note that some APIs like Claude's use a default Top K that works well without adjustment in most cases.
Can you combine Top K and Top P at the same time?
Yes, most APIs allow combining both. In this case, the model first applies the Top K filter then the Top P filter on the remaining tokens. This allows finer control, but in practice, using one or the other is usually sufficient. Start with Top P alone, then add Top K if you need to further limit.

See also

How to use this prompt

  1. Copy the prompt with the button above.
  2. Paste it into ChatGPT, Claude or your favorite AI assistant.
  3. 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

Get new prompts every week

Join our newsletter.