Generative AI / Basic: generation and evidence foundations / Lesson 7
Sampling, Temperature, and Output Limits
Understand how decoding choices alter a candidate distribution and why output limits need explicit handling.
On this page
Lesson goals
- Distinguish a model score from a generated token.
- Explain temperature using a small distribution.
- Handle output limits and evaluate decoding choices.
From scores to a token
At a generation step, a model produces scores for possible next tokens. A decoding rule turns those scores into a selected token, then generation continues using the updated context. Greedy selection chooses a highest-scoring candidate; sampling instead draws from a distribution. Exact available controls and their interactions depend on the serving implementation.
Neither a high model score nor deterministic decoding establishes factual correctness. A wrong candidate can receive the highest score. Evaluate answers against the actual task and evidence, and keep application authorization and validation independent of the decoding setting.
Normalize a tiny example
For two illustrative scores, exponentiating and normalizing gives a probability distribution that sums to one. These values are not output from a real language model. The small numbers keep the arithmetic visible; production implementations typically use numerically stable calculations for large score ranges.
import math
logits = [2.0, 1.0]
weights = [math.exp(x) for x in logits]
print([round(w / sum(weights), 3) for w in weights])
Scores 2 and 1 give approximately 0.731 and 0.269 after normalization.
Change distribution sharpness
A positive temperature divides each score before normalization. A lower value makes the higher score relatively more likely; a higher value makes this two-candidate distribution more even. Temperature only affects a sampling path that applies it. It does not fix missing evidence or guarantee diversity, correctness, or a particular answer.
import math
logits = [2.0, 1.0]
for temperature in (0.5, 2.0):
weights = [math.exp(x / temperature) for x in logits]
print(temperature, [round(w / sum(weights), 3) for w in weights])
For this fixture, 0.5 gives approximately 0.881 and 0.119; 2.0 gives approximately 0.622 and 0.378.
Recognize a limited output
A generation limit bounds work and response size, but a response can stop before completing its intended structure or explanation. Check the serving systemโs completion status and validate any structured output before use. Define a bounded retry or a clear incomplete-result response; do not treat a cut-off JSON object as a valid answer.
Choose settings with evidence
Compare decoding settings on a representative task set with a fixed evaluation procedure. Record model and configuration, repeated-run behavior when sampling is enabled, task quality, latency, and failures. Inspect regressions and incomplete outputs. A setting that works for a creative draft may be inappropriate for a constrained extraction task, even with the same model.
Common errors and fixes
A low temperature is claimed to guarantee a correct answer.
- Why it happens
- Predictable selection was confused with factual support.
- How to fix it
- Verify claims against appropriate evidence and test task accuracy.
- Prevent it next time
- Evaluate correctness separately from repeatability.
An incomplete structured response is sent downstream.
- Why it happens
- A length limit ended generation before validation.
- How to fix it
- Reject the incomplete candidate and use a bounded retry or fallback.
- Prevent it next time
- Check completion status and schema before use.
Changing temperature has no observable effect.
- Why it happens
- The active decoding path may not use sampling or the setting.
- How to fix it
- Inspect the documented configuration and test the selected path.
- Prevent it next time
- Record effective decoding settings with evaluations.
2 practice exercises
1. For scores 2 and 1, does temperature 0.5 or 2.0 make the two probabilities more similar?
Show worked solution
Temperature 2.0 makes them more similar, about 0.622 and 0.378. Temperature 0.5 sharpens the distribution to about 0.881 and 0.119.
2. A response stops halfway through a JSON object. What should the application do?
Show worked solution
Treat the candidate as incomplete, avoid downstream execution, and apply the documented bounded retry or fallback. Validate the resulting object and its factual support before use.
Key takeaways
- Decoding selects tokens from model scores using a defined rule.
- Temperature changes a sampling distribution, not truthfulness.
- Output limits require completion and validation handling.