Temperature & Top-p
Decoding hyperparameters that control LLM output determinism versus creativity by reshaping token probability sharpness (Temperature) and truncating the cumulative probability mass (Top-p)
Table of Contents
Structure Diagram
What is Temperature & Top-p?
Decoding hyperparameters that control LLM output determinism versus creativity by reshaping token probability sharpness (Temperature) and truncating the cumulative probability mass (Top-p). It is a core concept in LLM Fundamentals within AI / ML.
Core Principles
At every generation step, an LLM transforms raw vocabulary logits into a probability distribution via Softmax before sampling the next token. Temperature ($T$) divides logits by $T$ prior to Softmax: lower values sharpen the distribution toward the single highest-probability token, while higher values flatten it to encourage diversity.
Top-p (Nucleus Sampling) keeps only the smallest set of top tokens whose cumulative probability exceeds $p$ (e.g., 0.9), dynamically pruning low-probability tail tokens that cause incoherent or off-topic output.
Practical Takeaways
- Tune one lever at a time: Adjusting both Temperature and Top-p aggressively makes behavior hard to predict. Most API providers recommend fixing
top_p=1.0and tuningtemperaturealone. - Match settings to task determinism: Use
0.0–0.2for JSON extraction, code, and classification;0.3–0.5for RAG synthesis; and0.7–0.9for creative ideation. - Zero temperature is near-deterministic, not bitwise identical: GPU parallelism and MoE routing can still introduce minor variance; combine
temperature=0with schema validation for mission-critical flows.
Notable Examples
- Structured Data Extraction: Setting
temperature=0ensures consistent JSON field values across batch document runs. - Creative Writing Assistants: Pairing
temperature=0.85withtop_p=0.95preserves fluency while surfacing varied phrasing. - Automated Evals (LLM-as-a-Judge): Locking
temperature=0minimizes scoring jitter across benchmark iterations.