Sampling and trustworthy answers
easy 6 minA decoder turns the model’s scores into a choice.
Video lesson · Chapter 1 of 4
A lottery for the next word
Theory
Read or listen first. About 2 min.
What it is
A decoder turns the model’s scores into a choice. Temperature changes the shape of the distribution, while top-p truncates the low-probability tail. Sampling after truncation requires normalizing the surviving mass. Both settings influence variation across runs.
Why it exists
For a slogan, varied continuations may be desirable. For an invoice total, even the most likely continuation needs evidence. Evaluate variation and factual accuracy independently when deciding which settings belong in a product.
Which words survive a nucleus cutoff?
Inside this chapter
Chapter sources- Hallucination vs Creativity
- Inventing a plausible detail may be useful in a short story and harmful in an invoice summary. Define the task’s evidence requirement first. For an extraction task, permit an explicit missing field and verify every reported amount against the document. Deterministic decoding can repeat a fabricated amount consistently.
Generating a lighthouse made of glass and generating an invoice total use exactly the same machinery. Whether the output counts as creativity or hallucination depends on whether the task requires it to match a source. For extraction, give the model an explicit way to say a field is missing, and check every number against the document in code. Setting temperature to zero only makes the output repeatable, so a fabricated total will be fabricated identically every time.
Example. Accept “a lighthouse made of glass” in a fiction exercise. In an inventory report, require a source record for the same claim.
Another example. An invoice extractor that must always fill
totalwill invent one for receipts where the total is torn off. Allowingtotal: nulland verifying non-null totals against the parsed line items removes the pressure to guess.Decide the evidence rule before the sampler
Temperature does not decide whether invention is acceptable. The task does. - Temperature & Top_P
- Temperature rescales logits before sampling. Lower values sharpen the distribution. Nucleus sampling sorts candidates by probability and retains the smallest prefix whose cumulative mass reaches the threshold. With masses 0.6, 0.3 and 0.1, a threshold of 0.85 retains the first two, then sampling renormalizes their probabilities.
Temperature acts first: logits are divided by T before softmax, so T below 1 widens the gaps and concentrates mass on the leader, while T above 1 flattens the distribution. Top-p then sorts tokens by probability, keeps the smallest group that reaches the cutoff, and rescales that group. The two interact: a low temperature can make one token reach the cutoff alone. Tune one at a time on your own task and look at both quality and diversity.
Example. With a cutoff of 0.6, the 0.6-probability token alone qualifies. At 0.61 the 0.3 token must join it.
Which tokens survive each top-p cutoff
Cutoff p Cumulative mass checked Kept Renormalised draw 0.60 A = 0.60 A A 1 0.61 A + B = 0.90 A, B A 2/3 · B 1/3 0.85 A + B = 0.90 A, B A 2/3 · B 1/3 0.95 A + B + C = 1.00 A, B, C A 0.6 · B 0.3 · C 0.1 Masses A 0.6, B 0.3, C 0.1. Keep the smallest top set whose mass reaches p, then rescale it to sum to one. - Calibration and abstention
- Sampling temperature reshapes token probabilities during decoding. Choosing to defer an answer is a separate product decision based on measured correctness, evidence quality and the cost of mistakes. Evaluate the risk at several levels of answer coverage.
Abstention is a product decision: answer when the expected benefit beats the cost of being wrong, otherwise defer to a human or say you do not know. To set the threshold, score each answer with some confidence signal (agreement across samples, retrieval support, a verifier), sort by that signal and plot error rate against coverage. Pick the point that matches the cost of mistakes in your domain, and recheck it when the model or prompt changes, because confidence signals drift.
Example. If answering ninety of a hundred questions yields nine errors, the error rate among answered questions is ten percent. Report coverage with that rate so a policy cannot improve simply by declining everything.
Another example. A medical billing assistant may answer 70 percent of coding questions automatically with a low error rate and route the rest to staff. A recipe chatbot can accept much higher coverage with more errors.
Report coverage beside the error rate
A policy that declines everything has zero errors and zero coverage, so neither number is meaningful alone. - Greedy and top-k decoding
- Greedy decoding always takes the highest-probability token. Top-k sampling keeps only the k most likely tokens, rescales them and draws from them. Greedy is repeatable but can loop or produce bland text; top-k uses a fixed count, whereas top-p adapts the count to how spread out the distribution is.
Top-k's weakness is that k is fixed. When the model is sure, k = 50 still lets in 49 poor candidates; when it is unsure, k = 5 may cut off reasonable ones. Top-p handles both cases by keeping more tokens when the distribution is flat and fewer when it is peaked.
Example. With A 0.6, B 0.3, C 0.1, greedy always emits A. Top-k with k = 2 draws A two times in three and B one time in three.
Three ways to turn the same distribution into a token
Same scores, different decoding rules, different behaviour. - Seeds and run-to-run variation
- A random seed fixes the sequence of random draws, which can make sampling repeatable on one fixed software and hardware stack. Batching, parallel arithmetic order and serving changes can still alter results, and hosted services may not promise identical outputs for the same seed.
Floating-point addition is not perfectly associative, so summing the same numbers in a different order can change the last bits, which can flip a close decision between two tokens. Tests should therefore check properties of the output (fields present, amounts match the source) rather than exact strings.
Example. A regression test pins seed 42 on a local runtime and gets the same summary twice. The same request against a shared endpoint differs once traffic changes the batch it runs in.
What a fixed seed holds
- The sequence of random draws
- Repeatable runs on an unchanged local stack
- Easier debugging of one failure
What can still change
- Floating-point order across batch sizes
- Runtime, driver or model revisions
- Provider-side serving changes
How it works
Step through the idea, or press Play.
Token distribution. A 0.60 · B 0.30 · C 0.10
Understand it
Worked example
Which words survive a nucleus cutoff?
- 1
A 0.60 · B 0.30 · C 0.10
Hold logits and temperature fixed. These three tokens form the complete vocabulary in this worked example.
- 2
p = 0.85 → A and B
A alone covers 0.60 of the mass. Adding B reaches 0.90, so the smallest qualifying prefix contains both.
- 3
A ⅔ · B ⅓
Sampling uses the retained mass. A is more likely than B, but either can be drawn. Neither probability measures the truth of a factual claim.
Temperature changes relative odds
Follow the handoffs
- 1RankA 0.60 · B 0.30 · C 0.10
- 2Accumulate0.60 · 0.90 · 1.00
- 3Cut at p = 0.850.90 is the first to reach it: keep A, B
- 4Renormalise0.60/0.90 and 0.30/0.90
- 5DrawA two thirds · B one third
C is excluded at 0.85 and returns once the cutoff passes 0.90.
How temperature stretches a logit gap of 1
| Temperature T | Odds of the leader, e^(1/T) | Effect |
|---|---|---|
| 2 | 1.65 | flatter, more variety |
| 1 | 2.72 | the model's own distribution |
| 0.5 | 7.39 | sharper, leader dominates |
Summary
The whole topic in one minute.
A and B cover only 0.90. The decoder must include C to reach the higher threshold.
For a slogan, varied continuations may be desirable. For an invoice total, even the most likely continuation needs evidence. Evaluate variation and factual accuracy independently when deciding which settings belong in a product.