← All news

News / Learn / LLM fundamentals

How do temperature and sampling change answers?

You'll be able to pick the right temperature and sampling settings for your product and know what each one changes.

A language model does not pick one word with certainty. It scores many possible next tokens, then picks one based on those scores. Temperature and sampling settings control how the model makes that pick. Low settings favor the likeliest tokens, so output is steady and predictable. High settings give less likely tokens a real chance, so output is more varied.

Your product's users see the output, so the randomness of that output affects them directly. A support bot, a data extractor, and a story generator need different settings. Getting this right improves consistency, and it also helps you test and debug, since repeatable output is easier to check.

Before the model outputs a token, it produces a score for every token in its vocabulary. Those scores are turned into probabilities. Temperature rescales the scores before this step. A low temperature sharpens the distribution, so the top token gets most of the probability. A higher temperature flattens it, so more tokens remain plausible.

Sampling settings then decide which tokens are eligible. Top-p (nucleus sampling) keeps the smallest set of top tokens whose probabilities add up to a threshold, such as 0.9, and drops the rest. Top-k keeps only a fixed number of the highest-scoring tokens. Both cut off the long tail of unlikely tokens, which helps avoid odd or incoherent word choices.

The model then draws one token from the remaining set. Repeat this for each token until the answer ends. Because each step is a draw, the same prompt can produce different answers. Some providers also accept a seed value, which can make draws repeatable, but support and behavior vary by provider.

Note that some newer reasoning models do not expose temperature or top-p controls at all, and some providers fix these values. Check your provider's documentation before you build around these settings.

A pattern for setting temperature per task. The client library and parameter names vary by provider, so treat this as a shape, not a copy-paste call.

# Pseudocode: one request helper, different settings per task

TASK_SETTINGS = {
    "extract_fields": {"temperature": 0.0},  # steady, repeatable output
    "summarize":      {"temperature": 0.3},  # mostly steady, some phrasing variety
    "brainstorm":     {"temperature": 0.9},  # more varied ideas
}

def run_task(task, prompt):
    settings = TASK_SETTINGS[task]
    response = llm_client.generate(
        prompt=prompt,
        temperature=settings["temperature"],
        # top_p is often left at the provider default.
        # Change one control at a time while testing.
    )
    return response.text

A short sketch to show the idea. Check the official docs of the tool you use.

  • Assuming temperature 0 means fully deterministic Output can still vary slightly across runs or model updates. Build checks that tolerate small differences instead of exact string matches.
  • Using high temperature for tasks that need accuracy Extraction, classification, and code generation usually need low randomness. Save higher settings for creative or exploratory tasks.
  • Tuning temperature and top-p together without a reason Both shape the same choice. Change one at a time, test the result, and then decide whether the other is needed.
  • Trusting a setting you never tested Run a fixed set of test prompts whenever you change sampling settings, and compare outputs side by side.

Learn how logits and softmax turn raw scores into probabilities. Then study how eval sets let you measure the effect of a setting change on your own task, rather than relying on feel.

What is the best temperature?

No single value is best. Use low settings for structured or factual work and higher settings for creative work. Test a few values on your own examples and keep the one that performs best.

Does temperature make the model smarter?

No. Temperature changes how the model samples from the probabilities it already has. It does not add knowledge or reasoning ability.

Why do I get different answers to the same prompt?

Sampling involves random draws, so repeated runs can differ. Lower temperature reduces the variation but may not remove it entirely.

Should I change both temperature and top-p?

Usually change one. Most providers suggest adjusting temperature or top-p, not both, because they overlap in effect.

Do all models support these settings?

No. Some models, especially certain reasoning models, ignore or restrict these parameters. Check the documentation for the model you use.

Written with AI by CapyLaunch. Spotted a mistake? Email hello@capylaunch.com.