
Dan Patiño
AI Strategy & Innovation at Coderhouse
Artificial Intelligence
LLM temperature is a setting that controls how random a large language model’s output is. A low temperature makes the model stick to its most likely next words, so answers are focused and repeatable. A high temperature spreads the choice across more options, so answers become more varied, more creative, and more likely to drift. Temperature usually travels with related controls such as top-p (nucleus sampling) and top-k, which limit which words the model is allowed to pick from.
Why it matters now: teams are moving from chatting with AI to building products and agents on top of model APIs. In that setting, the same prompt can give a different answer every time, and that variability affects testing, data extraction, customer-facing copy, and costs. Knowing what these sampling parameters do is one of the quickest ways to make an AI feature behave predictably.
How temperature works
A language model writes one token at a time. At each step it scores every possible next token and turns those scores into probabilities. Temperature rescales the scores before that conversion:
Temperature near 0: the probability of the top choice is pushed toward 100%, so the model almost always picks it. Output is close to deterministic.
Temperature around the default: the model follows its learned distribution, with some natural variety.
Higher temperature: the distribution flattens, so less likely tokens get picked more often. This adds variety but also raises the chance of odd word choices or off-topic turns.
Accepted ranges and defaults differ by provider. Anthropic’s Messages API reference documents temperature alongside top-p and top-k, and Google’s Gemini text generation guide shows how to set it in a generation config. Always check the current docs for the model you use, because some reasoning-focused models restrict or ignore these parameters.
Top-p and top-k: the other sampling controls
Top-k
Top-k keeps only the k most likely tokens at each step and samples among them. With k set to 40, the model ignores everything outside the top 40 candidates.
Top-p (nucleus sampling)
Top-p keeps the smallest set of tokens whose combined probability reaches p, for example 0.9, and samples from that set. The set grows when the model is unsure and shrinks when it is confident. The technique comes from the research paper “The Curious Case of Neural Text Degeneration”, which showed that always picking the single most likely word tends to produce repetitive text, while sampling from the “nucleus” gives more natural results.
Using them together
Most providers recommend adjusting temperature or top-p, not both at once, because their effects overlap and become hard to reason about. Change one setting, measure the outcome, and keep the rest at their defaults.
Which settings to use for which job
Data extraction, classification, and structured output: use a low temperature. You want the same input to produce the same fields every time, especially when the result feeds a database or a JSON schema.
Code generation and SQL: low to moderate. Creativity rarely helps a query, but a little variety can help when you generate several candidates and test them.
Customer support and internal assistants: low to moderate, so answers stay consistent with policy and source documents.
Brainstorming, naming, and marketing copy: moderate to high, where variety is the point and a human picks the best options.
Evaluation runs: keep settings fixed and record them, so changes in scores reflect your prompt or model changes rather than sampling noise.
Temperature and hallucinations
A common myth is that temperature 0 eliminates hallucinations. It does not. A low temperature makes the model consistently choose its most likely answer, and if that answer is wrong, it will be consistently wrong. Hallucinations come mostly from missing or misleading context and from how the model was trained; we cover the causes in what AI hallucinations are. Grounding the model with retrieval, asking for structured outputs, and validating results do more for accuracy than sampling settings alone.
Why temperature 0 is not always perfectly repeatable
Even at the lowest temperature, two identical requests can occasionally differ. Model providers run requests on shared hardware with batching and parallel math, where tiny numerical differences can tip a close choice between two tokens, and models get updated over time. If you need strict repeatability, pin the model version, log every parameter, and design your tests to check meaning and structure rather than exact wording. Techniques like using an LLM as a judge help compare outputs that vary slightly in phrasing.
A simple tuning workflow
Start with the provider’s defaults and a clear prompt.
Write 10 to 20 representative test inputs with the outputs you expect.
Run them at two or three temperature values and compare accuracy, format errors, and variety.
Pick the lowest temperature that still gives acceptable variety for the task.
Record the setting in code and configuration, not just in someone’s memory, and revisit it when you change models.
FAQ
Does a higher temperature make the model smarter or more creative?
It makes outputs more varied, not more capable. The model does not gain knowledge at a higher temperature; it simply samples from a wider range of options it already considered possible.
Does temperature affect cost or speed?
Not directly. Cost depends mainly on input and output tokens. A high temperature can lead to longer or rambling answers, though, so set a maximum output length as well.
Should I expose temperature to end users?
Usually not. Most products are better off choosing a sensible setting per feature, such as low for answers and higher for idea generation, rather than asking users to tune a technical parameter.
Want to build AI features that behave predictably in production? Explore these Coderhouse courses:
AI Engineering Course — learn to work with model APIs, prompts, evaluation, and deployment.
Data Analytics Course — analyze and validate the data your AI systems produce.
I'm Dan Patiño, head of AI Strategy & Innovation at Coderhouse. My day-to-day work involves merging the tactical management of e-commerce (CRO, Email Marketing and SEO) with the development of disruptive solutions. I specialize in building internal AI-powered apps to automate tasks and boost innovation within the team. I firmly believe that technology is strategy's best ally. To dive deeper into my professional journey, I'll be waiting for you on my LinkedIn profile.