Typical-p sampling, or locally typical sampling, keeps only the smallest set of the most locally typical tokens whose probabilities add up to typical_p or higher. Local typicality measures how similar the conditional probability of predicting a target token is to the expected probability of predicting a random token given the partial text. In plain terms, it drops tokens that are surprisingly likely or surprisingly unlikely for the context, which cuts the incoherent tails that top-p alone can keep.
What you need
- A generation call where you control the sampling parameters
- A test prompt where the model sometimes produces an odd token
- A note of the typical-p value you will try, for example 0.9
Step 1: Set typical_p below 1
Set typical_p to a value below 1, for example 0.9, so the sampler keeps only the locally typical tokens. At 1.0 the filter does nothing.
Step 2: Generate on a prompt with odd tails
Run the test prompt and read the output for tokens that feel out of place. Typical-p should remove the surprising tokens that top-p would have kept because they were probable in absolute terms.
Step 3: Compare with top-p on the same prompt
Run the same prompt with top-p instead of typical-p and compare the outputs. If typical-p reads more coherent, keep it; if it reads the same, top-p is simpler and you do not need the extra parameter.
Worked example: synthetic typical-p check
The table below is a synthetic example. It shows the expected result of using typical-p; no text was generated.
| Setting | Odd token in output? | Reads coherent? |
|---|---|---|
| No filter | Yes – surprising word | No |
| top-p 0.9 | Sometimes | Mostly |
| typical-p 0.9 | No | Yes |
| Decision | typical-p kept | Most coherent |
Expected result (UNTESTED). typical-p removes the surprising token that top-p sometimes keeps, and the output reads coherent. This check was not run live; the table shows the expected result for the synthetic example.
Limits
- Typical-p is a less common parameter than top-p, so not every API or engine exposes it; check your stack first.
- Local typicality is measured against the expected probability given the partial text, so it depends on the context, not on a fixed threshold.
- Setting typical-p too low can starve the sampler of tokens on unusual but correct text, such as code or names.
- Typical-p and top-p can be combined, but two filters on the same distribution make the behaviour harder to predict.
