Temperature
Learn how temperature reshapes next-token probabilities and how to choose and test it for consistent or varied outputs.
The same Large Language Model prompt can produce different responses across multiple runs. One setting that affects this variation is temperature. It changes how the model samples the next token, but it does not add knowledge or retrain the model.
Temperature is often described as a creativity control. That description is helpful for beginners, but the precise effect is mathematical: it reshapes the probability distribution used during token selection.
What Is Temperature?
Temperature is an inference parameter applied to the model's output scores, called logits, before converting those scores into probabilities. A lower positive value sharpens the distribution, while a higher value flattens it.
- Lower temperature concentrates probability on already likely tokens.
- Higher temperature gives less likely tokens a greater chance of selection.
- Temperature does not change the model's trained weights.
- Supported ranges and exact zero-temperature behavior vary by provider and model.
How Temperature Changes Probabilities
For each possible next token, the model produces a logit. The sampling system divides the logits by temperature and applies softmax to create probabilities.
P(token_i) = softmax(logit_i / T)
T: temperature
T < 1: sharper distribution
T = 1: original distribution
T > 1: flatter distributionSuppose three candidate tokens initially have probabilities of approximately 70%, 20%, and 10%. Lowering temperature makes the 70% candidate more dominant. Raising temperature brings the probabilities closer together, increasing the chance that another candidate is selected.
Temperature does not decide whether an answer is factual or creative by itself. It changes token selection at every generation step, and small differences can lead to very different later sentences.
Low Temperature
A lower temperature generally produces more focused and repeatable wording because highly likely tokens dominate the distribution. It is often a sensible starting point for tasks with narrow expected outputs.
- Structured extraction and classification.
- Code generation and technical explanations.
- Customer-support drafts based on approved sources.
- Business reports and documentation.
- Tasks evaluated against a specific format or rubric.
Low temperature does not guarantee accuracy. It can make the model repeat the same confident error more consistently. Factual reliability still needs trusted context, tools, validation, and review.
High Temperature
A higher temperature flattens the distribution and increases variation. This can help explore alternatives, but excessive values may reduce coherence, instruction adherence, or factual stability.
- Brainstorming many possible approaches.
- Story concepts and fictional dialogue.
- Marketing headline variations.
- Creative analogies and examples.
- Exploring alternative names, themes, or designs.
Higher temperature does not guarantee useful creativity. A clear task, audience, constraints, and selection process usually matter more than simply increasing randomness.
What Happens at Temperature Zero?
Many APIs treat temperature zero as deterministic or near-greedy generation, meaning the most likely permitted token is selected. The mathematical formula cannot literally divide by zero, so this behavior is implemented as a special case.
Even then, identical output is not universally guaranteed. Provider infrastructure, model updates, numerical differences, tool results, hidden context, batching, and other settings can change a response. Check the selected API's reproducibility documentation.
Python Probability Simulation
This small example shows how different temperatures reshape fixed logits. It does not call an LLM.
import math
tokens = ["clear", "helpful", "unexpected"]
logits = [2.0, 1.0, 0.2]
def probabilities(values, temperature):
if temperature <= 0:
raise ValueError("Use a positive temperature for this formula")
scaled = [value / temperature for value in values]
largest = max(scaled)
exponents = [math.exp(value - largest) for value in scaled]
total = sum(exponents)
return [value / total for value in exponents]
for temperature in (0.3, 1.0, 1.8):
probs = probabilities(logits, temperature)
result = dict(zip(tokens, [round(p, 3) for p in probs]))
print(temperature, result)Subtracting the largest scaled logit improves numerical stability without changing the final softmax probabilities.
API Configuration Example
The following object is conceptual because model APIs differ in supported fields and ranges.
{
"task": "extract_invoice_fields",
"temperature": 0.2,
"output_format": "validated_json"
}Strict extraction should not rely on temperature alone. Define a schema, validate every field, handle missing values, and reject malformed results.
Temperature and Other Sampling Controls
Top-k
Top-k restricts sampling to the k highest-probability candidate tokens before selection. A small k narrows the choice set.
Top-p
Top-p, also called nucleus sampling, keeps the smallest candidate set whose cumulative probability reaches a threshold. Its candidate count adapts to the shape of the distribution.
Seeds and Penalties
Some platforms support a random seed or repetition-related penalties. A seed can improve reproducibility under supported conditions, while penalties alter token scores based on earlier output. Exact semantics are provider-specific.
Temperature, top-k, top-p, penalties, and model behavior interact. Avoid changing every control at once because it becomes difficult to identify why quality changed.
Temperature and Prompt Engineering
The prompt describes what the model should do. Temperature changes the distribution used while generating the answer. They complement one another but solve different problems.
Prompt:
Explain Python loops for beginners using one runnable example.
Lower temperature:
Often favors stable, conventional explanations.
Higher temperature:
May vary analogies, examples, wording, and structure more widely.If an output repeatedly ignores a requirement, first improve the instruction, context, or output validation. Temperature is not a substitute for a well-defined task.
Choosing a Temperature
- Start from the model provider's documented default or task guidance.
- Create a representative evaluation set before tuning the value.
- Use lower values as a starting point for narrow, structured tasks.
- Test moderate or higher values when useful diversity is a goal.
- Generate several samples per prompt to measure variation.
- Compare quality, instruction adherence, safety, latency, and user preference.
- Record the model version, prompt, temperature, and all other sampling settings.
Evaluation Example
| Task | Quality to Measure | Likely Starting Direction |
|---|---|---|
| Invoice extraction | Schema validity and field accuracy | Lower |
| Code generation | Tests, correctness, and security | Lower |
| Tutoring | Accuracy, clarity, and learner value | Low to moderate |
| Brainstorming | Diversity and usefulness of ideas | Moderate to higher |
| Story concepts | Originality, coherence, and style | Moderate to higher |
These are starting directions, not universal settings. A specific model may behave differently, and some modern model APIs expose limited or no temperature control.
Advantages
- Adjusts variation without retraining the model.
- Helps one model support both focused and exploratory workflows.
- Provides a simple setting for controlled experiments.
- Can improve user experience when matched to a task's need for diversity.
Limitations
- Low temperature does not guarantee truth or valid structured output.
- High temperature does not guarantee originality or quality.
- It cannot add missing knowledge or fix poor source context.
- Effects differ across models, versions, and API implementations.
- Other sampling settings can hide or amplify its effect.
- A single output cannot reveal how variable a setting really is.
- Some APIs may restrict or omit temperature controls.
Best Practices
- Treat temperature as a measured inference setting, not a creativity guarantee.
- Tune it after clarifying the prompt and success criteria.
- Change one sampling control at a time during experiments.
- Run repeated samples and include difficult edge cases.
- Validate facts, code, and structured output independently.
- Pin and log settings for production requests where appropriate.
- Retest when prompts, models, providers, or decoding settings change.
Real-World Applications
- Focused extraction and classification.
- Programming and technical assistants.
- Educational explanations.
- Customer-support response drafts.
- Marketing variation and brainstorming.
- Story, dialogue, and creative-content generation.
- Multi-candidate generation followed by ranking or human selection.