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.
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.
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.
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
| Term | What It Means |
|---|---|
| Logits | Raw, unnormalized scores for every word in the vocabulary |
| Softmax | The function that turns logits into probabilities |
| Temperature | Divides the logits before softmax to reshape probabilities |
| Greedy decoding | Temperature 0: always pick the most likely token |
| Top-k | Keeps exactly the k most likely tokens |
| Top-p | Keeps 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.