Glossary

Top-p sampling

A sampling method, also called nucleus sampling, that limits a language model's next-token choice to the smallest set of tokens whose combined probability reaches p.

Top-p sampling, also called nucleus sampling, is a way of choosing the next token in which a language model only considers the most likely tokens whose probabilities add up to a threshold p, then samples from that group. With p set to 0.9, the model ranks its candidates, keeps adding them until their combined probability reaches 90%, discards everything else and picks from what is left. The size of that group changes at every step. When the model is confident, one or two tokens may cover 90% and the choice is nearly fixed. When many continuations are plausible, the group grows and the text can vary. Top-p cuts off the long tail of unlikely tokens that produces odd words and derailed sentences, while keeping variety where variety makes sense. A top-p of 1.0 turns the cut-off off.

How top-p works, step by step

At every step a transformer produces a probability for every token in its vocabulary. Most of that probability sits on a handful of tokens, and the rest is spread thinly across tens of thousands of others. Top-p sampling sorts the tokens from most to least likely, walks down the list adding up probabilities, and stops as soon as the running total reaches p. That set is the nucleus. Every token outside it gets a probability of zero, the probabilities inside it are rescaled so they add up to one again, and the model samples a token from the result.

Worked numbers

Suppose a model is continuing “After the meeting I ordered a” and its six most likely next tokens have these probabilities: “ coffee” 40%, “ taxi” 25%, “ sandwich” 15%, “ new” 10%, “ refund” 6% and “ giraffe” 4%. The table shows what is left to sample from at different values of p, after rescaling.

Token

Original

Top-p 0.5

Top-p 0.75

Top-p 0.9

Top-p 1.0

“ coffee”

40%

61.5%

50%

44.4%

40%

“ taxi”

25%

38.5%

31.3%

27.8%

25%

“ sandwich”

15%

removed

18.8%

16.7%

15%

“ new”

10%

removed

removed

11.1%

10%

“ refund”

6%

removed

removed

removed

6%

“ giraffe”

4%

removed

removed

removed

4%

At 0.5, the first two tokens already reach 65%, so only they remain. At 0.9, the first four reach exactly 90%, and the two least plausible continuations are gone. At 1.0 nothing is removed, and “ giraffe” will turn up in about one run in 25. Over a long answer with hundreds of such choices, that tail is where most of the strange phrasing comes from. The percentages are illustrative and rounded.

Why the nucleus changes size

The adaptive size is the point of the method. For “The capital of France is”, the token “ Paris” might carry 95% on its own, so the nucleus at p = 0.9 is a single token and the output is effectively fixed. For the opening of a short story, the top hundred tokens might each carry under 2%, so the nucleus is large and the model has room to vary. The same setting gives tight output where the model is sure and loose output where it is not, which a fixed cut-off cannot do.

Top-p compared with top-k, min-p and greedy decoding

Top-k sampling keeps a fixed number of tokens, such as the 40 most likely, regardless of how probability is spread. That is too many when the model is confident, letting implausible tokens in, and can be too few when it is not. Top-p was proposed in 2019 research on why generated text degenerates, partly to fix this. Min-p, offered by some open-source inference servers, keeps tokens whose probability is at least a fixed fraction of the top token’s, which behaves similarly and handles very flat distributions a little differently. Greedy decoding always takes the single most likely token, which is predictable but tends to repeat itself in long text. Temperature is the other common control: it reshapes the whole distribution, sharpening or flattening it, while top-p truncates it.

The name top-k also appears in search, where it means something different. In retrieval, top-k is the number of passages or results returned for a query, which is a separate setting from top-k sampling during generation even though the idea of keeping the k best candidates is the same.

Order of operations matters when settings are combined. In most implementations, temperature is applied to the logits first, then the top-p cut-off is taken from the resulting probabilities. A high temperature flattens the distribution, so the nucleus at a given p grows. That is why the common advice is to adjust temperature or top-p and leave the other at its default. Many APIs default top-p to 1.0, which means no truncation, and some reasoning models fix sampling settings and ignore the parameter.

Choosing a top-p value

If your outputs are already good, leave top-p at its default. If the model occasionally produces a bizarre word or veers off topic at a temperature you otherwise like, lowering top-p to around 0.9 or 0.95 removes the tail without making the text flat. For tasks with one correct answer, temperature near 0 already does the work and top-p has little left to change.

Task

Typical top-p

Notes

Extraction, classification, structured output

1.0 (with temperature near 0)

Top-p has little effect once temperature is low

Question answering over documents

0.9 to 1.0

Keep temperature low; top-p trims rare slips

Chat and assistants

0.9 to 0.95

Natural variety without unlikely tokens

Creative writing and brainstorming

0.95 to 1.0

Pair with a higher temperature for range

For structured output, constrained decoding removes tokens that would break the schema before sampling, so top-p then works on what is left. Values inside fields still vary with sampling, which is why extraction usually runs at a low temperature. Logprobs show the probabilities behind each chosen token and make it easy to see how often the nucleus was a single token and how often it was wide.

When evaluating a model, record the sampling settings with every run. A golden dataset scored at one top-p value says little about behaviour at another, and LLM evaluation results that change between runs are often sampling noise rather than a real difference between prompts. Running each case several times at the production settings gives a fairer picture than a single run with sampling turned off.

Top-p in long outputs and agents

Top-p works one token at a time, so its effect builds up over long outputs. A large language model writing a 2,000-token report makes 2,000 sampling decisions, and even a small chance of an odd token at each step adds up. That is one reason long-form generation usually pairs a moderate temperature with a top-p of 0.9 to 0.95. Repetition is the opposite failure: with sampling turned down too far, models can loop on a phrase. Frequency and presence penalties, offered by several APIs, lower the probability of tokens that have already appeared and are applied before the top-p cut-off, so the two settings work together.

In agents, different steps want different settings. A step that picks a tool and fills its arguments should be close to deterministic, while a step that writes to a user can sample more freely. Chain of thought reasoning sometimes samples several reasoning paths on purpose and keeps the most common answer, and that only works if top-p leaves room for the paths to differ. Clear instructions in the system prompt and a few worked cases through few-shot prompting narrow the model’s distribution before sampling begins, which often makes top-p tuning unnecessary. For graders such as LLM-as-a-judge, keep sampling tight so scores are comparable between runs.

What top-p does not change

Top-p only removes unlikely tokens. If the most likely continuation is wrong because the prompt is missing a fact, carries an outdated one or buries the right passage in the middle of a long context, the nucleus will contain the wrong answer and top-p will keep it. Most hallucination in production comes from that kind of context problem, so tuning sampling helps at the margins while fixing retrieval and memory moves the error rate. Top-p also has no effect on cost or length; max tokens and the size of the prompt decide those.

A worked example: drafting follow-up emails

Consider a sales copilot that drafts follow-up emails after calls. At temperature 1.0 and top-p 1.0, the drafts read naturally, but roughly one in twenty contains an odd phrase or an invented detail, such as a discount nobody offered. The team lowers top-p to 0.9 and the odd phrasing mostly disappears while the drafts still vary from one prospect to the next.

The invented discounts remain. Looking at the prompts, the team finds the copilot receives only the latest call transcript, so when a prospect mentions “the pricing we discussed”, the model fills the gap with a plausible guess, which is a highly likely token sequence and survives any top-p setting. The fix is to give the model the prospect’s history from earlier calls, including what was offered. After that, the drafts are both varied and accurate. The figures in this example are illustrative.

Where Exabase fits

Exabase leaves sampling settings to you; top-p, temperature and the rest stay in your own model calls. Exabase supplies what the model samples from. Exabase Memory extracts facts, preferences and events from conversations, resolves contradictions as they change and keeps a current picture of each user or account, which is the prospect history the copilot above was missing; see agent memory. Its retrieval has its own way of trimming a long tail: the memory search endpoint can expand a query into several retrieval passes, rerank up to 500 candidates, and drop the lowest-scoring share with a rerank threshold, where 0.05 drops the bottom 5%. The retrieving memories guide covers those settings.

Deep Search has a precision setting from 0 to 1 that plays a similar role for documents and media: low values cast a wide net and high values return only the closest matches. Cutting weak candidates from retrieval does for context what top-p does for tokens, and it is set out in RAG accuracy. For more on the retrieval side, see similarity isn’t relevance, top-k is the wrong default, the reranking entry and the best reranking models comparison. The top-k retrieval and precision and recall entries explain the trade-off in search terms.

Frequently asked questions

What is top-p sampling in simple terms?

It limits the model’s choice of next token to the most likely options whose probabilities add up to p, such as 90%, and picks randomly among those. Unlikely tokens outside that group are never chosen.

What is a good top-p value?

For most uses, 0.9 to 1.0. Leave it at the default of 1.0 if outputs are fine, and lower it towards 0.9 if you see occasional odd words at a temperature you otherwise like.

What is the difference between top-p and top-k?

Top-k keeps a fixed number of the most likely tokens. Top-p keeps however many tokens it takes to reach a probability total, so the group shrinks when the model is confident and grows when it is not.

What is the difference between top-p and temperature?

Temperature reshapes the whole probability distribution, making it sharper or flatter. Top-p truncates it, removing the least likely tokens. Most guidance suggests adjusting one of them, not both.

Does top-p 1.0 mean no top-p?

Yes. At 1.0 the nucleus includes every token, so nothing is removed and sampling is controlled by temperature alone.

Does lowering top-p reduce hallucinations?

Slightly. It removes rare, odd tokens, but most hallucinations are likely continuations built on missing or outdated context, which top-p keeps. Better context does more.

Does Exabase set top-p?

No. You control sampling in your own model calls. Exabase provides the context through Memory and Deep Search, with its own retrieval settings for trimming weak candidates.

Related pages: temperature, logprobs, token, transformer, hallucination, structured output, constrained decoding, reranking, RAG accuracy

Cut your token spend and give your agent precise context.

Get started in minutes.

Cut your token spend and give your agent precise context.

Get started in minutes.