Temperature, top-p and other sampling parameters
What do temperature, top-p and other sampling parameters do, and how do you set them?
TL;DR
At each step the model outputs probabilities for every possible next token, and sampling parameters decide how to pick one. Temperature reshapes the distribution: low values (around 0–0.3) make the most likely tokens dominate, so output is focused and repeatable; high values (around 0.8–1.2) flatten it, so output is more varied and creative but riskier. Top-p (nucleus sampling) keeps only the smallest set of tokens whose probabilities add up to p (such as 0.9) and samples from those. Top-k keeps only the k most likely tokens. Use low temperature for extraction, classification, code and factual Q&A; higher for brainstorming and creative writing.
Key points
- Temperature 0 ≈ greedy decoding (always the top token). It's still not perfectly deterministic on most APIs, because of floating-point and batching effects.
- Tune temperature or top-p, not both at once; they interact.
- Max tokens caps output length (and cost). Hitting it cuts the answer mid-sentence, so check the stop reason.
- Stop sequences end generation at a marker.
- Frequency and presence penalties (on some APIs) reduce repetition.
- Some newer reasoning models fix or ignore sampling parameters; check the model's documentation.
- For reliable structure, prefer structured outputs or tool calling with a JSON schema over tuning sampling.
Example
Logits for next token: "Paris" 5.0 · "Lyon" 2.0 · "France" 1.0
temperature 0.2 → Paris ≈ 100%
temperature 1.0 → Paris ≈ 94%, Lyon ≈ 5%, France ≈ 2%
temperature 2.0 → Paris ≈ 74%, Lyon ≈ 16%, France ≈ 10%Common follow-ups
- Why does high temperature cause hallucinations? It makes unlikely tokens more likely, and one wrong token early can send the rest of the answer off track.
- How do you get diverse but good outputs? Sample several candidates at a moderate temperature and pick the best with a scorer or judge (best-of-n).
What interviewers look for
Tip
What each parameter does to the distribution, and sensible settings per task type.
- Red flag: "temperature 0 guarantees identical outputs."