EN ▾
Get API key

How to Set LLM Temperature in an API Call

Here is how to set llm temperature: add a temperature number to the request body of /v1/chat/completions. This guide gives starting values per task, code in three languages and fixes for common sampling problems.

Updated

Glass dial from cool blue to warm orange with dots spreading wider as it warms, picturing LLM temperature
per 1M input tokens
$0.15
Output tokens / 1M
$0.50
token context
100,000
trial credit
$0.50
requests per minute
300

Key points

  • Set temperature in the JSON body; the API passes it through to the model.
  • Use 0 to 0.3 for code and extraction, 0.8 to 1.1 for fiction and brainstorming.
  • Change temperature or top_p, not both at once.
  • Temperature does not change price; max_tokens caps cost.

Short answer: one field in the request body

To set llm temperature in an API call, add a temperature number to the JSON body of POST /v1/chat/completions. Low values (0 to 0.3) make the model pick the most likely next token almost every time, which suits extraction, code and factual answers. Higher values (0.8 and up) spread the choice across more tokens, which suits stories, brainstorming and dialogue.

curl https://api.uncensoredaimodel.cc/v1/chat/completions \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "uncensored",
    "temperature": 0.2,
    "messages": [{"role": "user", "content": "List three uses of a hash map."}]
  }'

The API passes temperature, top_p, stop, seed and the frequency and presence penalties straight through to the model. The rest of this article explains what each one changes and which values to start with.

What temperature actually does

A language model produces a score for every possible next token. Temperature rescales those scores before one token is chosen. At 0 the top-scoring token wins every time, so output is nearly deterministic. As the value rises, lower-ranked tokens get a real chance, and the text becomes more varied, more surprising, and more likely to drift off course.

This is not a creativity dial that makes the model smarter. Higher temperature makes answers more random, not better. On a question with one correct answer, a high value just increases the chance of a wrong one. On a task with many acceptable answers, such as naming a product, randomness gives you a wider set to pick from.

Typical OpenAI-style ranges run from 0 to 2, but values above about 1.3 often produce rambling text on most models. Test your range rather than trusting a number from a blog.

Starting values by task

TaskTemperatureNotes
Data extraction, classification0 to 0.2Combine with JSON mode
Code generation, SQL0.1 to 0.3Keep output stable between runs
Summaries, Q and A0.2 to 0.5Some variety, still grounded
Chat assistant0.5 to 0.8Natural but steady tone
Fiction, roleplay, brainstorming0.8 to 1.1Raise top_p or penalties if it loops

These are starting points from common practice, not guarantees for any particular model. Pick one, run ten prompts, read the results, and move in steps of 0.1 to 0.2.

Setting it from code

Python

from openai import OpenAI

client = OpenAI(base_url="https://api.uncensoredaimodel.cc/v1", api_key="YOUR_KEY")

resp = client.chat.completions.create(
    model="uncensored",
    messages=[{"role": "user", "content": "Write a two-line opening for a mystery story."}],
    temperature=0.9,
    top_p=0.95,
)
print(resp.choices[0].message.content)

Node.js

import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.uncensoredaimodel.cc/v1",
  apiKey: process.env.API_KEY,
});

const resp = await client.chat.completions.create({
  model: "uncensored",
  messages: [{ role: "user", content: "Extract the city name: 'I flew to Lisbon on Monday.'" }],
  temperature: 0,
});
console.log(resp.choices[0].message.content);

The same temperature field applies to streaming calls. If a streamed request ends with an unclear failure, see the guide to unknown errors from an LLM stream.

Temperature, top_p, seed and penalties together

Temperature is not the only sampling control, and they interact.

  • top_p keeps only the smallest set of tokens whose probabilities add up to p. A common advice is to change temperature or top_p, not both at once, so you know which one produced a change.
  • seed asks for repeatable sampling. With a fixed seed and identical parameters, repeated calls tend to match, although exact repeatability is not something to rely on across any provider. Use it for debugging, not as a guarantee.
  • frequency_penalty lowers the chance of tokens that already appeared often, which helps with repeated phrases.
  • presence_penalty lowers the chance of any token that appeared at all, nudging the model toward new topics.
  • stop ends generation when a string appears, which is useful for structured output.

A frequent fix for creative text that loops: lower temperature slightly (for example from 1.1 to 0.9) and add a small frequency penalty such as 0.3, rather than raising temperature further.

Compare settings with a quick experiment

The fastest way to learn what a value does is to run the same prompt at several temperatures and read the output side by side.

import os
from openai import OpenAI

client = OpenAI(base_url="https://api.uncensoredaimodel.cc/v1", api_key=os.environ["API_KEY"])
prompt = "Give me a name and one-sentence pitch for a coffee shop on a mountain."

for t in (0, 0.5, 1.0, 1.4):
    print("--- temperature", t)
    for _ in range(3):
        r = client.chat.completions.create(
            model="uncensored",
            messages=[{"role": "user", "content": prompt}],
            temperature=t,
            max_tokens=80,
        )
        print(r.choices[0].message.content.strip())

At 0 you will see the same line three times, or nearly. At 1.4 you may see unusual wording or odd structure. That is the range of behaviour you are choosing between. The experiment makes 12 calls of about 40 input and 60 output tokens each, which is 12 x (40 x $0.15 / 1M + 60 x $0.50 / 1M) = 12 x $0.000036 = $0.000432, well under a tenth of a cent.

Chat clients, apps and cost

You do not need to write code to change the value. Chat front ends that accept a custom OpenAI-compatible endpoint, such as SillyTavern, expose a temperature slider and send it in every request. Point the client at https://api.uncensoredaimodel.cc/v1, enter your key, choose the model ID uncensored, and adjust the slider.

Two habits save time. First, keep one preset per task: a low one for code and a higher one for story work, so you do not retune each time. Second, change one thing between tests. If you move temperature, top_p and a penalty together, you will not know which one fixed or broke the output. When you use the API from your own backend, store the value in configuration rather than hard-coding it, so you can adjust it without a deploy.

For long conversations, remember that sampling settings only affect the next token choice. They cannot fix a confusing system prompt or an overloaded context. If answers drift as the chat grows, trim old turns first, then revisit temperature.

Temperature does not change price. You pay $0.15 per 1M input tokens and $0.50 per 1M output tokens whatever the sampling settings; set max_tokens when you need a ceiling. The pricing page has top-up details, and the API key page gives new users a $0.50 trial credit with one click. Parameters are listed in the docs.

Pitfalls and fixes

  • Answers differ between runs when you wanted stability. Set temperature to 0 and a seed. Check that your client does not overwrite the value with its own default.
  • Output is dull and repetitive. Raise temperature in steps, or add a frequency penalty.
  • Output is incoherent. Lower temperature, or lower top_p to around 0.9.
  • JSON breaks at high temperature. Use response_format: {"type": "json_object"} and keep temperature at or below 0.3.
  • The setting seems ignored. Make sure the field is in the top-level body, not inside messages, and that your frontend actually forwards it.

If the model keeps giving unexpected answers for reasons other than sampling, read the chat troubleshooting guide for prompt and system message issues.

Start with trial credit

Email and password, key on screen right away.

Questions and answers

What temperature should I use for factual answers?

Start at 0 to 0.3. Lower values keep the model on its most likely answer, which reduces random errors.

Does temperature 0 give identical output every time?

It makes output close to deterministic, but exact repeatability is not guaranteed. Add a seed and fixed parameters for the best consistency.

Can I set temperature when streaming?

Yes. The same field works with stream set to true.

Which sampling parameters does the API accept?

temperature, top_p, stop, seed, frequency_penalty and presence_penalty are passed through.

Your key is one form away

Create an account, copy the key, change the base URL. That is the whole setup.