P

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 the most probable ones whose cumulative probability reaches a threshold P.

Full definition

Top P (or nucleus sampling) is a sampling method used during text generation by language models. Rather than considering the entire vocabulary at each generation step, the model only retains the most probable tokens whose cumulative probabilities reach the defined threshold P. For example, with a Top P of 0.9, the model selects the most probable tokens until their sum of probabilities reaches 90%, then randomly chooses from this subset.

This mechanism offers a major advantage over Top K: it dynamically adapts to context. If the model is very confident about the next word (e.g., after "the Eiffel"), the nucleus will be small and contain only a few candidates like "Tower" or "of". Conversely, in a more open context (e.g., the beginning of a story), the nucleus naturally expands to include more creative possibilities.

Top P is set between 0 and 1. A value close to 0 makes the model almost deterministic by keeping only the most probable token(s). A value of 1 disables filtering and considers the entire vocabulary. Common values range between 0.7 and 0.95 depending on use: lower for factual tasks, higher for creative tasks.

It is important to note that Top P interacts with temperature. Temperature modifies the probability distribution before Top P filtering. In practice, it is recommended to adjust one or the other, rather than both simultaneously, to maintain predictable control over model behavior.

Etymology

The term "Top P" comes from English, where P represents the cumulative probability threshold. The method was formalized under the name "nucleus sampling" in the research paper "The Curious Case of Neural Text Degeneration" by Holtzman et al. in 2019. The word "nucleus" refers to the subset of selected tokens, considered the core of the probability distribution.

Concrete examples

Creative writing with high diversity

Write an original story about a robot that discovers emotions. [Top P = 0.95, Temperature = 0.8]

Factual response requiring precision

Explain how photosynthesis works in simple terms. [Top P = 0.4, Temperature = 0.3]

Code generation with a balance between creativity and reliability

Write a Python function that sorts a list of dictionaries by key. [Top P = 0.7, Temperature = 0.5]

Practical usage

In prompt engineering, Top P is typically set via the model API parameters. For tasks requiring precision and coherence (summaries, data extraction, code), use a Top P between 0.3 and 0.6. For creative tasks (brainstorming, writing, idea generation), increase it to between 0.8 and 0.95 to get more varied and surprising responses.

Related concepts

TemperatureTop KNucleus SamplingStochastic Sampling

FAQ

What is the difference between Top P and temperature?
Temperature modifies the shape of the probability distribution (it "flattens" or "accentuates" differences between tokens), while Top P truncates the distribution by keeping only a subset of tokens. Temperature acts before filtering, Top P acts after. In practice, it is advisable to adjust only one of the two parameters at a time for more predictable control.
What is the difference between Top P and Top K?
Top K selects a fixed number of candidate tokens (e.g., the top 50 most probable), regardless of context. Top P, on the other hand, dynamically adapts this number based on the model's confidence. If the model is confident, the nucleus will be small; if it hesitates, the nucleus will be large. Top P is generally considered more effective because it adjusts to context.
What default Top P value should I use?
A value of 0.9 is a good starting point for most uses. It offers a balance between diversity and coherence. Go down to 0.3–0.5 for tasks requiring high precision (translation, data extraction), and go up to 0.95–1.0 for highly creative tasks where you seek originality.

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.