Sub topic 10
Sampling Parameters and Output Variability
This chapter assumes you have read the earlier chapters on prompt anatomy, clear instructions, and output formats. You should be able to read short Python snippets. Here you will learn why the same prompt can produce different replies, and which settings control that.
Why the same prompt gives different answers
A language model does not pick a single answer. For each next token (a word or word piece), it computes a probability for every token in its vocabulary. Then a sampling step chooses one. If the choice involves randomness, two runs of the same prompt can diverge, and once one token differs, every later token can change too.
Sampling parameters are settings sent with your request that control this choice. They do not change what the model knows. They change how adventurous the choice is.
The main parameters
| Parameter | What it does | Low value | High value |
|---|---|---|---|
| temperature | Sharpens or flattens the probability distribution | Predictable, repeats the most likely wording | Varied, more surprising wording |
| top-p (nucleus sampling) | Keeps only the smallest set of tokens whose probabilities add up to p | Few candidates, safer | Many candidates, more variety |
| max tokens | Caps the reply length | Reply may be cut off | Longer replies allowed |
| stop sequences | Strings that end the reply when produced | Not applicable | Not applicable |
Temperature is the one you will use most. At 0, the model nearly always takes the most likely token. Values around 0.7 to 1.0 give more variety. The valid range and defaults differ between providers, so check your provider's documentation.
Top-p and temperature both change randomness. A common rule is to adjust one and leave the other at its default.
Choosing values for the task
Match the setting to what you need from the reply.
- Extraction, classification, and structured output: use low temperature. You want the same input to give the same label, and valid JSON every time.
- Summaries and question answering over supplied text: use low to moderate temperature.
- Brainstorming, naming, and creative writing: use higher temperature, then pick from several drafts.
def settings_for(task):
presets = {
"classify": {"temperature": 0.0, "max_tokens": 20},
"summarize": {"temperature": 0.3, "max_tokens": 300},
"brainstorm": {"temperature": 0.9, "max_tokens": 400},
}
return presets[task]
print(settings_for("classify"))
print(settings_for("brainstorm"))The exact parameter names depend on the API you call, but the idea is the same: keep a named preset per task, so the choice is deliberate and easy to change.
Measuring variability
Because output varies, one good run proves little. Run the same prompt several times and count the distinct replies. This reuses the evaluation idea from the testing chapter.
from collections import Counter
def variability(call_model, prompt, runs=5):
replies = [call_model(prompt).strip().lower() for _ in range(runs)]
return Counter(replies)
# Example with a fake model that is sometimes inconsistent
import itertools
fake = itertools.cycle(["positive", "positive", "Positive", "neutral", "positive"])
print(variability(lambda p: next(fake), "Classify: Setup was fine."))The output shows one label appearing four times and another once. If a classification prompt at temperature 0 still splits like this, the problem is usually the prompt, not the sampling. Tighten the instructions, add few-shot examples, or restrict the allowed labels.
Common mistakes
- Raising temperature to fix a wrong answer. Randomness does not add knowledge. It usually adds more errors.
- Changing temperature and the prompt together, which hides which change helped.
- Setting max tokens too low, which cuts JSON off mid-object and breaks parsing.
- Judging a prompt from one sample at a high temperature.
Exercise
A prompt extracts an order number from emails and must return the same value for the same email. Which temperature range would you choose, and why would you still validate the reply in code?
Show answer
Choose a low temperature, at or near 0, because extraction needs the most likely token each time and not varied wording. You still validate because temperature 0 does not guarantee identical or well-formed output. The reply could contain extra text or a malformed value, so code should check its format before using it.
Exercise
You run a brainstorming prompt five times at temperature 0.9 and like only one reply. A teammate suggests lowering the temperature to 0 to "get that good reply every time". Is that a good plan?
Show answer
No. Lowering the temperature does not recover that specific reply. It makes the model favor its single most likely wording, which may be bland. For brainstorming, keep a higher temperature and generate several drafts, then choose the best one.