← Tools

LLM Sampling Playground

A language model scores every possible next token; a sampler turns those scores into one choice. Edit the scores, change temperature, top-k and top-p, and watch each step of the pipeline. All the maths is exact and runs in your browser; no model is called.

The cat sat on the ▁▁▁

A spread of plausible endings with a long tail of odd ones. The ten candidates stand in for a real vocabulary of 100,000+ tokens.

Raw logits

Drag a bar or type a value. Logits are unbounded scores: negative just means “less likely than a logit of 0”, not “impossible”.

mat
floor
sofa
bed
roof
windowsill
keyboard
fence
moon
banana
-80+14

Shifting every logit by the same amount changes nothing downstream: softmax only sees differences.

Sampler settings

Temperature0.80
Top-k6
Top-p (nucleus)0.90

Drops tokens below min-p × the top token’s probability, so the cut adapts to how confident the model is.

Order

Hugging Face transformers and vLLM scale by temperature first. llama.cpp’s default chain truncates first. With a high temperature the two give different survivors.

The pipeline, step by step

Tokens are sorted by logit. Temperature never changes that order, so every truncation step cuts from the bottom. Struck-through red rows are what that step removed; faded rows were removed earlier.

1

Raw logits

The model's unnormalised scores. Any real number, including negatives.

mat
5.20
floor
3.90
sofa
3.30
bed
2.90
roof
2.20
windowsill
1.70
keyboard
1.00
fence
0.40
moon
-1.60
banana
-3.20
2

Divide by temperature (T = 0.80)

T < 1 stretches the gaps between logits, so softmax will be peakier.

mat
6.50
floor
4.88
sofa
4.12
bed
3.62
roof
2.75
windowsill
2.13
keyboard
1.25
fence
0.50
moon
-2.00
banana
-4.00
3

Softmax

Exponentiate and normalise: every token gets a probability in (0, 1), summing to 1.

mat
0.719
floor
0.142
sofa
0.067
bed
0.041
roof
0.017
windowsill
0.0091
keyboard
0.0038
fence
0.0018
moon
0.0001
banana
2.0e-5
4

Top-k (k = 6)

Removed 4 tokens holding 0.6% of the probability.

mat
0.719
floor
0.142
sofa
0.067
bed
0.041
roof
0.017
windowsill
0.0091
keyboard
0.0038
fence
0.0018
moon
0.0001
banana
2.0e-5
5

Top-p (p = 0.90)

Removed 3 tokens holding 6.7% of the probability.

mat
0.723
floor
0.142
sofa
0.067
bed
0.041
roof
0.017
windowsill
0.0091
keyboard
–
fence
–
moon
–
banana
–
6

Renormalise

Survivors are rescaled to sum to 1. This is the distribution we sample from.

mat
0.775
floor
0.153
sofa
0.072
bed
–
roof
–
windowsill
–
keyboard
–
fence
–
moon
–
banana
–

Logprobs

A logprob is the natural log of a softmax probability. Because softmax is exp-then-normalise, the log undoes the exp: ln pi = zi − ln Σj ezj. Here ln Σ ez = 5.687, so each model logprob is just its logit minus 5.687.

TokenLogit zModel p (T = 1)ln pSampling qln q
mat5.200.615-0.4870.775-0.255
floor3.900.168-1.7870.153-1.880
sofa3.300.092-2.3870.072-2.630
bed2.900.062-2.7870−∞
roof2.200.031-3.4870−∞
windowsill1.700.019-3.9870−∞
keyboard1.000.0092-4.6870−∞
fence0.400.0051-5.2870−∞
moon-1.600.0007-7.2870−∞
banana-3.200.0001-8.8870−∞
Why APIs return logprobs, not probabilities. They fall straight out of the logits (one subtraction), they stay readable when probabilities are tiny (p = 10−12 is just −27.6), and a sequence’s probability is a product of many small numbers that underflows to zero in floating point, while its log is a plain sum. A logprob near 0 means confident; very negative means surprising.
Logprobs and perplexity. Perplexity of a text is exp(−mean logprob) over its tokens: the effective number of equally likely choices the model was weighing at each step. Summed logprobs favour short answers (every token adds a negative term), so to compare answers of different lengths use the mean, or a length penalty.

Sample

Sampling lays the final distribution end to end on [0, 1], draws a uniform random number u, and picks the token whose segment contains it.

mat
floor
sofa
01

No samples yet. Changing any setting clears the tally.

Empirical frequency (bars) vs theory (black ticks), 0 samples
0%42%84%matfloorsofa
3 of 10
Tokens in play
0.674 nats
Entropy of q
1.96
Effective choices exp(H)
–
Perplexity of samples under the model
expected 2.28

Perplexity here scores the sampled tokens with the model’s own T = 1 logprobs. Low temperature picks the tokens the model rates highest, so its output has low perplexity; greedy output has the lowest of all, which is also why it tends to be bland and repetitive over long texts.

Greedy vs self-consistency

Greedy decoding makes each token locally as likely as possible, but it commits to one path and cannot recover from an early wrong turn. Self-consistency samples several whole reasoning chains at T > 0, throws the reasoning away and majority-votes the final answers. It wins when the right answer has the most total probability spread over many paths, even though no single path to it is the likeliest.

Toy problem

3 boxes hold 6 eggs each, plus 6 loose eggs. How many eggs?

OpeningPthen → 24→ 18
3 × 6 = 18 …0.400.250.75
6 loose + 3 boxes of 6 …0.350.800.20
Count: 6, 12, 18, 24 …0.250.840.16

Greedy takes the likeliest opening, then the likeliest answer: 18, wrong, every time. That single path is also the most probable complete path (0.30). But adding up all paths, P(24) = 0.59: the right answer is reachable by more routes.

P(majority vote is right) vs chains
0%50%100%111213141sampled chains ngreedy: 0%

Exact: P(more than n/2 of n chains say 24) = 71.2% at n = 9.

Questions this answers

Dividing every logit by T is a uniform rescale. Why does it change the shape?
Softmax turns differences into ratios through an exponential: pi/pj = e(zi−zj)/T. Dividing by T scales the gap inside the exponent, which is not the same as scaling the ratio. T < 1 stretches gaps (peakier), T > 1 shrinks them (flatter), T → 0 is argmax, T → ∞ is uniform.
Is temperature applied before or after softmax?
Before, on the logits: the division sits inside the exponential, pi = ezi/T / Σ ezj/T. There is no separate “divide the probabilities” step.
Top-p before or after temperature?
Top-p needs probabilities, so it runs after softmax. Whether temperature comes before it depends on the implementation; flip the Order switch with T = 2 and watch top-p keep more tokens when temperature runs first, because the flattened distribution needs more tokens to reach p.
What does a negative logit mean?
Nothing special. Logits are any real number, and only their differences matter (try “Add 2 to every logit”). A logit of −3 just means e3 ≈ 20× less likely than a token at 0. Its probability is small but never zero, unless a truncation step removes it.
How do logprobs relate to perplexity?
Perplexity is exp of the average negative logprob per token. A model that assigns every observed token probability 1/4 has perplexity 4, as if choosing among four equally likely options each step.
Why do top-k and top-p both exist?
Top-k keeps a fixed count whatever the shape, which is too many for “Once upon a …” and too few for “My favourite colour is …”. Top-p keeps a fixed share of the mass, so it adapts. Min-p adapts too, by comparing each token with the top one.