Temperature, top_p and Sampling Explained for LLM APIs

JS
Jai Singh
24 Nov, 2026 4 min read 9 views

Temperature, top_p and top_k are sampling settings that control how random a model's word choices are. Low values make output more predictable and repeatable. High values make it more varied and creative. Some newer models no longer accept custom values, so always check the current documentation for the model you use.

How a model picks the next token

At each step the model produces a probability for every token in its vocabulary. The word "Paris" might get 92 percent after "The capital of France is", with small shares left for other tokens. The sampler then picks one token using those probabilities. Repeating this step builds the whole answer, one token at a time. Sampling settings reshape the probabilities before the pick.

Temperature

Temperature rescales the probabilities. A low temperature sharpens the distribution, so the most likely token wins almost every time. A high temperature flattens it, so less likely tokens get picked more often. At 0, the model effectively always takes the top choice. For many APIs the range is 0 to 1 (some allow up to 2), and the default is often 1.

  • Low (0 to 0.3): extraction, classification, code, factual Q&A, anything where you want the same answer each time.
  • Medium (0.5 to 0.8): general chat and writing.
  • High (0.9 and above): brainstorming, names, story ideas, where variety is the point.

top_p and top_k

top_p (nucleus sampling) keeps only the smallest set of tokens whose probabilities add up to p, then samples from that set. With top_p of 0.9, the unlikely tail is cut off. top_k keeps only the k most likely tokens. Both limit odd choices. Vendors usually advise changing temperature or top_p, but not both at once, because their effects overlap and are hard to reason about together.

Setting them in code

import anthropic

client = anthropic.Anthropic()

def ask(prompt, **sampling):
    reply = client.messages.create(
        model="claude-haiku-4-5",  # a model that accepts sampling parameters
        max_tokens=100,
        messages=[{"role": "user", "content": prompt}],
        **sampling,
    )
    return reply.content[0].text

for temp in (0.0, 1.0):
    print(temp, "->", ask("Suggest a name for a coffee shop. Reply with the name only.", temperature=temp))

Run the loop a few times. At temperature 0 you should see the same name every run, or very nearly so. At 1.0 the names change from run to run. This example uses claude-haiku-4-5 because it accepts sampling parameters; the current-generation models listed in the other posts may reject non-default values.

Temperature 0 is not a guarantee

Even at 0, outputs can differ slightly between runs because of how large models run on parallel hardware, batching, and floating point behaviour. Treat low temperature as "mostly repeatable", not as a hash function. If you need exact repeatability, cache the result of the first call.

When the model does not allow sampling parameters

Some current models reject temperature, top_p and top_k set to anything other than the default, and the API returns a 400 error. For those models you steer variety through the prompt instead. Ask for "three different options" when you want variety, or specify a strict format and rules when you want consistency. Design your code so sampling arguments are optional and can be dropped per model, rather than hard-coded into every call.

An experiment you can run in five minutes

Pick one prompt that has several valid answers, such as "Write a tagline for a bike shop". Run it ten times at a low setting and ten times at a high setting, and print the results side by side. You will see the low-temperature set collapse onto one or two phrasings, and the high-temperature set spread across many. Now try the same with a prompt that has one right answer, such as a data extraction task, and count how many runs are correct at each setting. This kind of measurement tells you far more than any rule of thumb, because the right value depends on the model and the task.

Why low temperature can hurt

Very low settings are not always better. For open-ended writing the output can become repetitive and flat. For long reasoning tasks, a little randomness can let the model recover from a poor first step. The best value is the one that scores highest on your own test set, so measure before you decide.

Practical advice

  • Leave the defaults alone until you have a measured reason to change them.
  • Use a low value for pipelines that parse the output.
  • Do not use high temperature to fix a weak prompt. Improve the instructions first.
  • Change one setting at a time and compare over many runs, not one.
  • Store the settings you used with each logged request so results can be explained later.
Was this article helpful?

Comments

No comments yet. Have a question or a tip? Be the first to share it.

Leave a comment

Comments are reviewed before they appear.