What Is the Temperature Parameter? top_p and Values by Task
Temperature controls how random the model's word choice is: lower is more conservative and stable, higher is more varied. top_p is a different kind of limit: it samples only from the smallest set of candidate words whose cumulative probability reaches p. The practical takeaway is to tune only one of the two. Turn it down for coding, translation, and information extraction, and turn it up a bit for creative writing.
This article covers what the temperature parameter does, how it differs from top_p, and how to run a small experiment to find the right value for your task. Value ranges and defaults differ between providers, so check the official documentation.
What does the temperature parameter actually adjust?
Each time a large language model generates a word (more precisely, a token; see what a token is in an LLM), it first scores every candidate in its vocabulary and converts the scores into probabilities. For example, the candidates for the next word might look like this:
| Candidate | Probability (example) |
|---|---|
| 的 | 40% |
| Yes | 30% |
| 在 | 20% |
| Other | 10% |
(The candidates are Chinese function words, kept as-is to match the original example; any language works the same way.)
The model doesn't always pick the most likely candidate. It draws one at random according to those probabilities. Temperature reshapes this table before the draw:
- Lower temperature: high-probability candidates stand out more, draws concentrate on them, and repeated answers to the same question look more alike.
- Higher temperature: the distribution flattens, unlikely candidates get picked more often, and answers become more varied and more likely to drift off course.
- Close to 0: essentially always picks the top candidate. Most services still don't guarantee fully reproducible output, so don't treat it as a "deterministic switch."
The numbers in the table are only an illustration of how the shape changes. They are not real model output.
What is top_p, and how is it different from temperature?
top_p is also called nucleus sampling. Sort the candidates by probability from high to low, add them up from the top, and stop once the total reaches p. The draw happens only among those candidates, and the rest are excluded.
Using the example above, if top_p is 0.7, then "的" and "是" add up to exactly 70%, so the draw happens only between those two words. "在" and "Other" are excluded.
A simple way to remember the difference:
- Temperature changes how steep the whole table is. All candidates are still in play.
- top_p cuts off the tail. The relative probabilities of the remaining candidates stay the same.
Should you tune both parameters together?
Change only one at a time. If you change both, it's hard to tell which one caused a problem, and their effects can stack. Many providers' documentation also recommends changing just one of them. Follow the official documentation of the service you use.
Two more things to watch for:
- Value ranges are not uniform. Some services allow a higher maximum temperature than others, and values outside the range either raise an error or get clamped. Check the official documentation.
- Some reasoning models fix or ignore these parameters. If you pass them, they may have no effect, or the API may even return a parameter error. If tuning does nothing after you switch models, check that model's documentation first.
How to think about values by scenario
This gives you a direction, not a "right answer," because the best value depends on the model and the prompt.
| Scenario | Leaning | Why |
|---|---|---|
| Code generation, bug fixing | Low | Syntax and logic need to be stable; randomness mostly produces errors |
| Information extraction, classification, JSON output | Low | Output format must be consistent; see also making an LLM output stable JSON |
| Translation, summarization | Low to medium | Stay faithful to the source, but allow a little variation in wording |
| Everyday Q&A, customer support | Medium | Balance accuracy and natural tone |
| Copywriting, brainstorming, naming | High | You want diversity, and you can generate several times and pick the best |
For the specific numbers behind "low," "medium," and "high," start with the default value in the service's documentation as a baseline, then test in both directions.
How do you find the right value for your task? (step by step)
- Fix one real prompt and input. Don't use a toy example.
- Send 5 requests with the default values and note how the outputs differ. If the differences are small and you're happy with the results, you don't need to tune anything.
- Change only temperature (or only top_p), move a small step in one direction, and send 5 requests again.
- Compare: for code tasks, check whether it passes; for format tasks, check whether parsing succeeds; for creative tasks, check whether the output is fresher.
- Record the final value together with the model name. If you switch models, test again.
To see where these two parameters go in a request, read what Chat Completions is. An example request body (field names depend on the documentation of the service you use):
{
"model": "your model name",
"messages": [{"role": "user", "content": "Translate the following sentence into English: ..."}],
"temperature": 0.2
}
Many tools and services use the OpenAI-compatible format, so these fields mostly work across services. The reason is covered in what "OpenAI-compatible API" means.
What works better than parameter tuning?
Temperature is not a cure-all. Wrong or made-up answers are usually caused by an unclear prompt or missing context, and changing temperature won't fix that. The usual order of priority is: first write a clear prompt, then add the necessary context, and only then fine-tune sampling parameters.
If you feel that "the same parameters gave different output over the past few days," the provider may have updated the model. You can check cdnping's model watch for listing, delisting, and change records for each provider's models before deciding whether the parameters are to blame.
No API environment yet? How to start experimenting
Tuning requires an environment where you can send requests repeatedly. If your usual tool lets you fill in an "endpoint URL + API key," you can swap the URL for another compatible service and try it out. For the concept, see what a base URL is.
One option is AGI Plan (AGI Plan is a commercial partner of cdnping). It is a monthly-subscription gateway that aggregates large language models, paid in RMB with no overseas credit card required. Tools such as Claude Code and Codex connect by changing a single endpoint URL, and the same quota can be switched between multiple capability groups. It is not an officially authorized channel, and some models are marked as available outside mainland China only, so check the AGI Plan models page for specifics. For connection steps, see Getting started with AGI Plan.
Summary and next steps
- Temperature controls overall randomness and top_p controls the candidate range. Tune only one first.
- Turn it down for stability and up for diversity, but let the official documentation and your own testing decide the values.
- Before tuning, confirm that the model supports these parameters.
Next step: pick a prompt you actually use and run one round of the 5 steps above. If request speed is also bothering you, use the online speed test on the cdnping homepage to see connection status for various AI endpoints. For background on latency, see AI API latency from mainland China.