What is Temperature in an LLM? Temperature Explained

Blog / What is Temperature in an LLM? Temperature Explained
What actually is temperature in a large language model? And why does the exact same prompt give a different answer each time you run it?

Same Prompt, Different Answers

Ask a model the same question twice and the replies usually differ.Sometimes you want this variety for brainstorming or creative writing. Other times you want strict consistency for extracting data into a JSON format. You need a way to control this randomness.To understand how to control this, we first need to see how a model generates text.

How the Model Picks the Next Word

For every single step, the model calculates a raw score for every word in its vocabulary. This raw unnormalized score is called a logit.A logit is the model's score for how strongly it favours one token as the next word. It is not a probability yet, so it does not have to fall between 0 and 1.A mathematical function called softmax turns these scores into probabilities that add up to one. Then one token is sampled from those probabilities, added to the text, and the whole process repeats.
The prompt The sky is goes into the model, which gives logits of 3.0 for blue, 1.3 for clear and 0.2 for grey, softmax turns them into 80, 15 and 5 percent, and the sampled word is added to the text before the loop repeats
Let's look at a concrete prompt like "The sky is". The model might score "blue" at 80%, "clear" at 15% and "grey" at 5%.Most of the time it picks "blue", but occasionally it samples "clear" or "grey".So how can we shift these probabilities? This is where temperature comes in.

Low vs High Temperature

Temperature divides the logits before the softmax exponential step. The formula simply divides each logit by the temperature value, and then applies the softmax function.A low temperature, like 0.5, mathematically stretches the gap between the numbers. This sharpens the distribution. Our 80% probability for "blue" might jump to 95%, making it completely dominate the options.
At temperature 0.5 each logit is divided by 0.5, so 3.0, 1.3 and 0.2 become 6.00, 2.60 and 0.40, and the probabilities sharpen to 95 percent for blue, 4 percent for clear and 0.6 percent for grey
A high temperature, like 2.0, halves the logits and shrinks the distance between them. This flattens the distribution.Now "blue" might drop to 50%, giving unlikely tokens like "grey" a legitimate chance to be chosen.

Temperature 0 and Why It Still Varies

Setting the temperature to exactly 0 means the model bypasses probability sampling entirely. It is forced to always pick the highest-probability token, which is an operation called greedy decoding.However, a temperature of 0 is still not guaranteed to give identical output every single run.Production servers group concurrent user requests into changing batch sizes. Graphics processing units alter their internal parallel math strategies based on this batch size.Because floating-point addition is non-associative, these operation order differences slightly alter the final logits. If two top tokens are in a near tie, a tiny rounding difference can flip the winner and change the entire rest of the text.
Two runs of the same prompt in a batch of 4 and a batch of 16 go through the GPU and give near-tied logits for blue and clear, so run 1 picks blue and run 2 picks clear, because floating-point addition is not associative

Top-k and Top-p

There are two other ways to control the next word.Top-k simply keeps the k most likely tokens and discards the rest.Top-p, also called nucleus sampling, keeps the smallest set of tokens whose probabilities cumulatively add up to p. This means the pool size dynamically adapts. It expands when the model is unsure, and shrinks when the model is highly confident.Temperature reshapes the entire probability distribution. Top-k and top-p cut it off abruptly.Because they interact unpredictably, the standard advice is to change temperature or top-p, but never both.

Which Value to Use

Use a low temperature around 0 to 0.3 for code generation, JSON output and data extraction.Use a middle value for general chat. Use a higher value for brainstorming and creative writing.

Moving Away From Temperature

Newer models are taking this dial away.OpenAI's latest models tell you to leave temperature and top-p out of your requests. Anthropic's current API version returns an error if you send temperature, top-p or top-k to any Claude model. Google's newest Gemini models ignore temperature, and Google says later models will return an error for it.Instead, you control these models with the prompt, the system instructions and the reasoning effort setting.

Recap

TermWhat It Means
LogitsRaw, unnormalized scores for every word in the vocabulary
SoftmaxThe function that turns logits into probabilities
TemperatureDivides the logits before softmax to reshape probabilities
Greedy decodingTemperature 0: always pick the most likely token
Top-kKeeps exactly the k most likely tokens
Top-pKeeps the smallest set of top tokens whose probabilities add up to p

About TechPrep

Never walk into a technical interview unprepared again. TechPrep empowers software engineers to stop guessing and start getting offers. We provide the exact questions asked by tech companies across Data Structures & Algorithms, System Design, Low-Level Design & AI coding rounds. Don't leave your career up to chance. Join thousands of engineers who have successfully navigated the tech hiring maze and landed roles at top tech companies.