Lesson 08 of 10 /Builds on 1

Why does AI give a different answer to the same question? Temperature, top-p and top-k explained

Ask the same question twice and you get two answers. The model rolls weighted dice for every word, and three dials decide how the dice are loaded. In this lesson we move those dials on a real model.

By the end you can
  • Say why the same prompt gives a different answer each time
  • Explain what temperature does to the next-word odds, and what temperature 0 means
  • Tell top-k from top-p, and say why top-p adapts to how sure the model is
  • Say why temperature 0 can still change its answer on a busy server
  • Know why the newest Claude, Gemini and GPT models are dropping these dials
In this lesson
  1. 1Why does the same question get a different answer?
  2. 2What does temperature do?
  3. LabWhat are top-k and top-p?
  4. 4Greedy or sampled: which should you use?
  5. 5Why is temperature 0 still not fully repeatable?
  6. QuizWhy are the newest models dropping these dials?
Part 1

Why does the same question get a different answer?

In lesson one we saw the one move behind every chatbot. The model looks at the text so far, gives every possible next token a score, picks one, adds it, and repeats. The scores turn into odds, like "Paris 6%, London 5%". The question for this lesson is the pick.

There are two ways to pick. Greedy means always take the top token. Sampling means roll dice that are loaded by the odds, so a 6% word comes out about 6 times in 100 rolls. Chat apps sample. So when you ask the same thing twice, the dice land on different words, and each new word changes the odds for every word after it. One different word early on and the whole answer takes a new road.

That is all "regenerate" does: it rolls the dice again. Three settings decide how the dice are loaded: temperature, top-k and top-p.

Part 2

What does temperature do?

Temperature changes the shape of the odds before the roll. The model's raw scores are divided by the temperature, then turned into percentages. A temperature below 1 makes the gaps between words bigger, so the top word grows and the rest shrink. A temperature above 1 makes the gaps smaller, so the odds flatten out and rare words get a real chance. Temperature 0 is the limit of that: the top word gets 100%, which is the same as greedy.

Here are real numbers from GPT-2, the small open model we use in our labs. After "The Eiffel Tower is located in the city of", at temperature 1 it gives Paris 6.4%, London 4.6% and Amsterdam 3.4%. Yes, London. The model is small and old, and it learned odds from text, not facts. At temperature 0.5 Paris jumps to 34% and London to 18%. At temperature 2 every word sinks below 1%, and the dice land almost anywhere.

Those numbers also show a problem. At temperature 1, more than half of GPT-2's odds for that sentence sit outside its top 40 words. Sample it plainly and about half the rolls come out as a word outside the top 40. That is the problem the next two dials were made to fix.

Three bar charts of GPT-2's next-word odds after 'The Eiffel Tower is located in the city of' at temperature 0.5, 1 and 2. Paris falls from 34% to 6.4% to 0.5%, while everything else rises from 22% to 78% to 98%.
The same odds at three temperatures. Low temperature piles the odds on the top words; high temperature spreads them over everything else. /Sansser, from GPT-2 (124M), 20 Nov 2026
Lab

What are top-k and top-p?

Top-k and top-p are cut-offs. They throw words out before the roll, so the long tail of odd words never gets a turn.

Top-k keeps only the k most likely tokens. Top-k 3 means the dice choose among the top 3 words, whatever their odds are. Top-k 1 is greedy again.

Top-p, also called nucleus sampling, keeps the smallest group of top words whose odds add up to p. Top-p 0.9 means "keep words, from the top down, until they cover 90% of the odds". The group changes size with how sure the model is. After "Thank you very", GPT-2 gives "much" 99.2%, so top-p 0.9 keeps one word. After "The Eiffel Tower is located in the city of", the odds are spread thin, so the same top-p 0.9 keeps about 950 tokens. A fixed top-k cannot do that. It keeps 40 words when the model is sure and 40 words when it is lost.

Top-p comes from a 2019 paper, "The Curious Case of Neural Text Degeneration". Its authors showed that pure greedy text falls into loops and repeats itself, and pure sampling wanders into nonsense from the tail. Cutting the tail and sampling from what is left gave text closer to what a person writes. Try all three dials below, then press "Sample 20 times" and count.

Bar chart of how many tokens GPT-2 keeps at top-p 0.9: 1 after 'Thank you very', 953 after 'The Eiffel Tower is located in the city of', and 3,652 after 'Once upon a time, there was a'.
Top-p 0.9 keeps one token when the model is sure and thousands when it is not. /Sansser, from GPT-2 (124M), 20 Nov 2026
Lab 01Load the dice, then roll themReady

Real GPT-2 odds for six starts. Move temperature, top-p and top-k and watch the bars reshape. Then press Sample 20 times and count how often each word comes out. Temperature 0 is greedy: the same word every time.

Part 4

Greedy or sampled: which should you use?

Neither wins everywhere. Greedy and low temperature suit jobs with one right answer: pulling a date out of an email, sorting tickets, writing code to a clear spec. Higher temperature suits jobs where you want range: names for a product, ten different openings for a story. If you only change one dial, change temperature. OpenAI's own API reference says to change temperature or top-p, but not both.

Every tool picks defaults for you. OpenAI's API defaults to temperature 1 and top-p 1, so nothing is cut. Ollama, the popular way to run open models on your own computer, defaults to temperature 0.8, top-k 40 and top-p 0.9. Google's Gemini docs give a default top-p of 0.95. And in May 2025 Anthropic moved Claude's default top-p from 0.999 to 0.99, so since then the bottom 1% of the odds is cut on every request that keeps the default.

Part 5

Why is temperature 0 still not fully repeatable?

If temperature 0 always takes the top word, the same prompt should give the same answer every time. Google's Gemini docs even say "a temperature of 0 is deterministic". In practice it is not. The dice are fine. The scores themselves shift a little.

A big model does not run your request alone. The server packs it into a batch with other people's requests, and the batch size changes with how busy the server is. The maths inside gives slightly different results for different batch sizes. In September 2025, Horace He at Thinking Machines ran the same prompt 1,000 times at temperature 0 on a large open model and got 80 different answers. The prompt asked about the physicist Richard Feynman. All 1,000 runs matched for the first 102 tokens. At token 103, 992 runs said he was born in "Queens, New York" and 8 said "New York City". When they rebuilt the maths so the batch size could not change the result, all 1,000 runs matched.

We saw the same wobble on GPT-2 on a laptop. We ran each of our six starts alone, then inside a batch of six. The scores moved by up to 0.00016. That sounds like nothing, but look at "The capital of France is": GPT-2 scores "France" and "Paris", its 4th and 5th picks, only 0.004 apart. Here the wobble is about 29 times smaller than the gap, so the two keep their places. But a long answer from a big model passes through hundreds of words, and if the top two words are ever that close, one flip sends greedy down a new path. That is what the 1,000-run test caught. It is also why OpenAI calls its seed setting "best effort" and says plainly that "determinism is not guaranteed".

Screenshot of the Thinking Machines post. 1,000 runs at temperature 0 gave 80 different answers. They split at token 103, between Queens, New York and New York City.
The 1,000-run test at temperature 0, from the Thinking Machines post. /Thinking Machines Lab, Sep 2025, screenshot by Sansser
Check yourself

Why are the newest models dropping these dials?

On 21 October 2026 Anthropic released API version 2026-10-21, and in it temperature, top-p and top-k are gone. A request that sends any of them gets an error, even at the default value. Claude 4.7 and later models already refused anything but the defaults. Google's docs say Gemini 4 and Gemini 3.6 and later ignore custom values and will return errors in a future release. OpenAI's guide for GPT-6.2 says to leave temperature and top-p out.

The sampling has not gone away. Anthropic's migration guide says a request now samples at the defaults, including a temperature of 1. What has gone is your hand on the dial. Our read is that these models think before they answer, and their thinking was trained at one setting, so the labs keep it there. Google's docs say as much for earlier Gemini 3 models: keep the defaults, or the reasoning gets worse.

So if you used temperature 0 to get the same answer every time, it was never a promise, and on the newest models it is not even an option. Ask for the format you want in the prompt, give an example, and use structured outputs where the API has them. If an answer has to stay fixed, save it rather than asking again. And when you want a range of ideas, ask for several in one go instead of turning up a dial. The dials still live on in open models and tools like Ollama, and the lab above works the same way there.

Screenshot of Anthropic's migration guide. It says a request that sends temperature, top_p or top_k now gets an error, even at the default value.
Anthropic's note on API version 2026-10-21. /Anthropic docs, captured by Sansser on 20 Nov 2026

Q1You ask the same question twice and get two different answers. Why?

Q2What does lowering the temperature do to the odds?

Q3Why does top-p 0.9 keep one word after 'Thank you very' but about 950 after 'The Eiffel Tower is located in the city of'?

Q4Why can temperature 0 give different answers on a busy server?

Remember

What to take with you

  1. A chatbot picks each word by a dice roll loaded with the model's odds. That roll is why the same prompt gives different answers.
  2. Temperature reshapes the odds. Low gives the top word more of the odds, high flattens them, and 0 is greedy: the top word every time.
  3. Top-k keeps a fixed number of words. Top-p keeps words until their odds reach a total, so it shrinks when the model is sure.
  4. Temperature 0 does not give the same answer every time on real servers. Batching moves the scores a little, and near-ties flip.
  5. The newest Claude, Gemini and GPT models reject these dials, ignore them or tell you to leave them out. Steer them with the prompt, examples and structured outputs.
Questions

Questions people are asking

/01What is temperature in an LLM?

Temperature is a setting that reshapes the model's next-word odds before it picks a word. Below 1 the most likely word gets more of the odds. Above 1 the odds flatten and rare words appear. At 0 the model always takes the top word.

/02What is the difference between top-p and top-k?

Top-k keeps a fixed number of the most likely words, such as 40. Top-p keeps the most likely words until their odds add up to a total, such as 90%, so it keeps few words when the model is sure and many when it is not.

/03Why does ChatGPT give different answers to the same question?

Because it samples. Each word is picked by a weighted dice roll over the model's odds, so two runs land on different words, and each word changes the odds for the rest of the answer.

/04Does temperature 0 make AI answers the same every time?

Not fully. Temperature 0 always takes the top word. But servers run many requests together, and the batch size shifts the scores a little. When two words are almost tied, the top word can flip, and the answer changes from there.

/05Can you still set the temperature on Claude, Gemini or GPT?

Not on the newest models. Anthropic's API version 2026-10-21 removes temperature, top-p and top-k. Gemini 4 and Gemini 3.6 and later ignore custom values, and OpenAI says to leave them out on GPT-6.2. Older models, open models and tools like Ollama still have them.

Sources 8

  1. Anthropic, Migrating to API version 2026-10-21, sampling parameters are removed platform.claude.com/docs/en/api/migration-2026-10-21
  2. Anthropic, Claude API release notes (21 Oct 2026; default top_p change, 22 May 2025) platform.claude.com/docs/en/release-notes/api
  3. OpenAI, API reference, Create chat completion (temperature, top_p, seed) developers.openai.com/api/reference/resources/chat/subresources/completions/methods/create
  4. OpenAI, Using GPT-6.2 developers.openai.com/api/docs/guides/latest-model
  5. Google, Gemini API prompt design strategies (temperature, topK, topP) aistudio.google.com/docs/prompting-strategies
  6. Ollama, Modelfile reference (sampling defaults) github.com/ollama/ollama/blob/main/docs/modelfile.mdx
  7. Holtzman et al., The Curious Case of Neural Text Degeneration, 2019 arxiv.org/abs/1904.09751
  8. Horace He and Thinking Machines Lab, Defeating Nondeterminism in LLM Inference, Sep 2025 thinkingmachines.ai/blog/defeating-nondeterminism-in-llm-inference
sansser weekly

Get the next lesson in your inbox.

New lessons and how-tos, the tools we tested and what we think, in one short email.

Free, every Friday. Unsubscribe in one click.