# Top-p

> The Top-p (nucleus sampling) is a parameter that limits the choice of the next token to the most probable options.

## What is top-p (nucleus sampling) in LLMs?

The parameter that makes the model choose the next token only from the smallest set whose total probability reaches 'p', for example '0.9'. Like temperature, it controls how varied the output is, and usually only one of them is changed.

- Topic: Generation Settings
- Type: concept

## Example

Wrong:

```js
const res = await llm.generate({
  prompt,
  top_p: 40,
  temperature: 1.5,
});
```

Right:

```js
const res = await llm.generate({
  prompt,
  top_p: 0.9,
});
```

Top-p is a probability between 0 and 1, and only one sampling setting is tuned while temperature stays at its default.

## Common mistakes

- Confusing top-p with top-k, which keeps a fixed number of tokens.

## Key points

- Only one of top-p or temperature is changed from its default, not both.
- Top-p is a value between '0' and '1', not a count of tokens.

## Interview questions

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

Top-k keeps a fixed number of the most likely tokens. Top-p keeps the smallest set whose probabilities add up to 'p', so the set size changes for each token.

### What top-p value should I use?

Most apps keep the default. Lower values such as '0.5' make output more focused; values near '1' allow more variety.

### Does top-p 1 turn nucleus sampling off?

Yes. A value of '1' keeps all tokens, so it does not filter anything.

## Related cards

- [Temperature](https://wonderium.dev/ai/temperature.md)
- [Token](https://wonderium.dev/ai/token.md)
- [Max Tokens](https://wonderium.dev/ai/max-tokens.md)
- [LLM API](https://wonderium.dev/ai/llm-api.md)

---

- Level: [Artificial Intelligence](https://wonderium.dev/ai.md)
- HTML version: https://wonderium.dev/ai/top-p
- Last updated: 2026-11-25
