induwara.lk
induwara.lkAI · Sampling

Top-p vs Top-k Sampling Calculator

Drag top_k and top_pand watch which of a language model's next-token candidates survive, get zeroed, and renormalize — with the exact math token by token. No API key, no cost, runs in your browser.

By Induwara AshinsanaUpdated Jul 11, 2026
Top-k & top-p sampler
Runs in your browser · no API key
Distribution preset

A moderately peaked distribution — the worked-example set.

Off — top-k keeps every token

Keep the smallest set reaching 90% cumulative mass

Filter order

llama.cpp style: cut to the top_k set first, then take the nucleus of what's left.

Most likely token
the
40%
Tokens surviving
4 / 6
after both filters
Mass kept
90%
before renormalizing
Vocab reduction
33.33%
candidates removed

Sorted distribution & cutoffs

Kept Cut by top_p Cut by top_k
the
40%
a
25%
an
15%
this
10%
top_p nucleus boundary · cumulative 90%
that
top_p
6%
some
top_p
4%

top_k then top_p keeps 4 of 6 tokens. Because both knobs cut from the sorted tail, the surviving set is simply the shorter of the two prefixes — min(top_k, nucleus length). Switch to Intersection and you'll get the identical survivors: top-k and top-p commute.

Step-by-step math

#TokenProb pCumulativeSurvivesFinal prob
1 the40%40%44.44%
2 a25%65%27.78%
3 an15%80%16.67%
4 this10%90%11.11%
5 thatcut by top_p6%96%0%
6 somecut by top_p4%100%0%
Renormalized total (4 surviving)1

Order: sort → top_k → top_p → renormalize. .

How it works

A language model produces a probability for every token in its vocabulary. Decoding parameters decide how much of that distribution to keep before drawing the next token. top_k and top_p are the two most common truncation knobs. This tool takes the probabilities as given — the post-temperature distribution, which is exactly the order real runtimes apply these filters (temperature → top_k → top_p) — and shows the cut in the reference order sort → top_k → top_p → renormalize.

  1. Sort. Order the tokens by descending probability. Both filters operate on this sorted list.
  2. top_k (Fan et al., 2018). If top_k = k > 0, keep only the k highest-probability tokens and zero the rest. k = 0 disables the filter, and a k larger than the vocabulary is a no-op.
  3. top_p / nucleus (Holtzman et al., 2019). Walk the tokens in descending order, accumulating a cumulative sum. Include tokens until the cumulative sum first reaches or exceeds top_p — the token that crosses the threshold is included, then stop. top_p = 1.0 disables the filter; the walk uses the raw (not renormalized) masses, matching llama.cpp.
  4. Renormalize. Let Sbe the sum of the kept tokens' original probabilities. Each kept token's sampling probability is p_i / S; cut tokens are 0. The survivors sum to exactly 1.

There is one insight worth internalising: because top_k and top_p both keep a prefix of the probability-sorted list, they commute. Combining them just keeps the shorter prefix — min(k, nucleus length) — so the two documented conventions (llama.cpp's “top_k then top_p” and the OpenAI-style “intersection”) always produce the same surviving set. The toggle in the tool changes only which filter is credited with cutting each token. As a credibility check, the nucleus length is computed two independent ways — an accumulate-and-break walk and an exclusive-prefix test — and the tool confirms they agree. Everything is deterministic and runs entirely in your browser.

Worked examples

All three use the peaked base distribution the=0.40, a=0.25, an=0.15, this=0.10, that=0.06, some=0.04 (which already sums to 1).

A · top-k only

top_k = 3, top_p = 1.0

  1. Sort (already sorted): the 0.40, a 0.25, an 0.15, this 0.10, that 0.06, some 0.04
  2. top_k = 3 keeps the, a, an; top_p = 1.0 cuts nothing further
  3. Renormalize over S = 0.40 + 0.25 + 0.15 = 0.80
  4. the 0.40/0.80 = 50.00%, a 0.25/0.80 = 31.25%, an 0.15/0.80 = 18.75% (3 survive)

B · top-p only (more aggressive than top-k=3)

top_k = 0, top_p = 0.60

  1. Cumulative walk: the → 0.40 (< 0.60, include) → a → 0.65 (≥ 0.60, include and stop)
  2. Nucleus = {the, a}; everything after a is zeroed
  3. Renormalize over S = 0.40 + 0.25 = 0.65
  4. the 0.40/0.65 = 61.54%, a 0.25/0.65 = 38.46% (2 survive — fewer than top_k=3)

C · combined, llama.cpp order

top_k = 4 then top_p = 0.90

  1. top_k = 4 keeps the, a, an, this; cuts that and some
  2. Nucleus over survivors: 0.40 → 0.65 → 0.80 → 0.90 (this crosses 0.90, include and stop)
  3. All 4 survive; renormalize over S = 0.90
  4. the 44.44%, a 27.78%, an 16.67%, this 11.11% (sum 100.00%)

Frequently asked questions

Sources & references

Related tools

Rate this tool
Be the first to rate

Comments & feedback

Spotted a bug or want an improvement? Tell us — our team reviews every comment, and good ideas get built. Comments are public and anonymous.

Found a bug, edge case, or want to suggest an improvement?

Email me at [email protected] — most fixes ship within 24 hours.