Temperature vs top_p in LLMs: what they do and what to set
The short version
temperatureandtop_pboth control how much randomness goes into picking each next token. They change how stable or varied the output is for the same prompt.- Temperature reshapes the probability distribution. top_p cuts off its long tail.
- Change one of them, not both. In practice, tune temperature and leave top_p at its default.
- Low for tasks with one right answer (classification, extraction, code), higher for tasks where you want options (copy, brainstorming).
- Temperature 0 makes output more repeatable. It does not make it more correct.
Sampling: the model outputs a probability distribution
An LLM writes text one token at a time. At each step it assigns a probability to every candidate token and picks one. After "The weather today is", it might give "sunny" 40%, "rainy" 25%, "cloudy" 20%, and so on down a long tail.
The procedure that picks one token from that distribution is sampling. The model produces the probabilities; temperature and top_p control how a token is chosen from them. Keeping those two jobs separate makes both parameters easier to reason about.
Temperature
temperature sharpens or flattens the distribution.
- Close to 0: the most likely token wins almost every time, and the output is nearly the same on every run.
- Around 1.0: the model's distribution is used roughly as it is, with natural variation.
- Above 1.0: the distribution flattens, and unlikely tokens get picked more often. The output gets more varied, and also more likely to wander or break down.
OpenAI-format APIs usually accept values from 0 to 2. If you leave it out, the model's own default applies.
top_p
top_p decides how many candidates are in play. Add up the candidates' probabilities from the most likely down, stop when the total reaches top_p, and sample only from that set. This is called nucleus sampling.
top_p: 0.1 keeps only the top handful of tokens. top_p: 1.0 keeps all of them. Temperature changes the shape of the distribution; top_p trims its tail.
Change one, leave the other
Both parameters act on the same randomness from different directions, so changing both at once makes the effect hard to read. OpenAI's API reference recommends altering one or the other, not both. The simplest working rule: tune temperature, and leave top_p at its default.
Starting values by task
These are starting points. The best value depends on the model and the prompt, so check real output and adjust.
| Task | Temperature | Why |
|---|---|---|
| Classification, extraction, structured data | 0 to 0.3 | The same format and the same judgment every time |
| Code and SQL | 0 to 0.3 | Fewer syntax slips |
| Summaries and translation | 0.3 to 0.7 | Faithful to the source, without stiff repetition |
| Conversation and general writing | 0.7 to 1.0 | Natural range of phrasing |
| Brainstorming, taglines, ad copy | 0.8 to 1.0 or higher | Many candidates for a person to choose from |
The rule of thumb: the more a task has a single right answer, the lower the temperature; the more you want options, the higher.
Temperature 0 does not mean accurate
Three things to know about the low end.
- Temperature 0 is nearly deterministic. Parallel computation and floating-point rounding on the serving side can still change the result slightly between identical requests.
- Temperature 0 does not make answers correct. It only changes how tokens are picked; it adds no knowledge and no evidence. A confident wrong answer is a hallucination problem, and the fixes are in the prompt and in grounding the model in sources.
- Models do not all read the parameters the same way. Defaults and real effects differ by provider and model, and some models, reasoning models in particular, ignore temperature or accept only certain values. An OpenAI-compatible API gives you the same syntax everywhere, but the behavior can still differ.
How stable the format and the role are also depends a great deal on the system prompt.
Setting it in a request
Add temperature to a normal Chat Completions request.
curl https://api.fastmetal.ai/v1/chat/completions \
-H "Authorization: Bearer $FASTMETAL_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "anthropic-claude-haiku-4-5",
"temperature": 0.2,
"messages": [
{"role": "user", "content": "Classify this review as positive, negative or neutral: Delivery was fast, but the box was a little crushed."}
]
}'
To use top_p with the OpenAI Python SDK, set it and leave temperature out:
from openai import OpenAI
client = OpenAI(
api_key="<FASTMETAL_API_KEY>",
base_url="https://api.fastmetal.ai/v1",
)
resp = client.chat.completions.create(
model="anthropic-claude-haiku-4-5",
top_p=0.9,
messages=[{"role": "user", "content": "Give me five taglines for a new single-origin coffee."}],
)
print(resp.choices[0].message.content)
The most reliable way to pick a value is to run the same prompt at two settings and compare:
for t in (0.0, 0.9):
resp = client.chat.completions.create(
model="anthropic-claude-haiku-4-5",
temperature=t,
messages=[{"role": "user", "content": "Summarize this product's features in three lines: ..."}],
)
print(f"--- temperature={t}")
print(resp.choices[0].message.content)
One FastMetal key calls many models, so you can change model in the same loop and compare model and setting together.
FAQ
Should I use temperature or top_p?
Start with temperature and leave top_p at its default. Changing both at once makes it hard to tell which one did what.
Does temperature 0 give the same answer every time?
Nearly, but not always exactly. Serving-side effects can still shift the output slightly. If you need strict reproducibility, add tests that check the output.
I switched models and the same temperature behaves differently. Why?
Each model interprets the parameters and sets defaults in its own way. The syntax is shared across an OpenAI-compatible API, and the effect can still differ. Re-tune the setting when you change models.
Summary
Temperature and top_p control how a model picks its next token. Keep them low for tasks with one right answer and higher when you want options. Setting temperature to 0 does not make answers more accurate, and the same value behaves differently from model to model. The API getting-started guide shows how to make your first request; from there, run one prompt across a few settings and a few models and compare.