Careers

Why AI gives a different answer every time

Ask the same question twice and get two answers? Learn what temperature and sampling are, and which setting to use for code, facts and ideas.

On this page

Ask an AI chatbot the same question twice and you will often get two different answers. This is not a bug. It comes from a setting called temperature and the way models pick each word. Once you understand it, you can control it.

The model has a list of choices

As we explained in how LLMs work, a model writes one token at a time. At each step it does not have one answer in mind. It has a list of possible next tokens, each with a probability.

For the sentence "My favourite fruit is", the list might look like this:

Next wordChance
mango30%
apple25%
banana20%
orange15%
something else10%

The model then has to pick one. How it picks is called sampling.

Always the top choice: greedy

The simplest way is to always take the most likely word. This is called greedy picking. It gives the same answer every time, but the writing can become dull and repetitive.

Rolling weighted dice: sampling

Most chat tools instead roll a kind of weighted dice. "Mango" wins most often, but "banana" sometimes wins too. Because of this small randomness, the same question can lead down a different path and produce a different answer.

Everyday comparison: asking a friend for a restaurant suggestion. They have a favourite, but they don't name the same one every single time.

What temperature does

Temperature is a dial that changes how adventurous the dice are.

  1. Low temperature (close to 0): the top choice almost always wins. Answers are focused, predictable and repeatable.
  2. Medium temperature (around 0.7 to 1): a balance. Natural-sounding writing with some variety.
  3. High temperature (above 1): unlikely words get a real chance. Answers become creative, surprising, and sometimes nonsense.
Note

Temperature does not make a model smarter or more accurate. It only changes how it picks from the choices it already has. Low temperature can still give a wrong answer; it will just give the same wrong answer every time.

Top-k and top-p, in one line each

You may see two more settings next to temperature:

  • Top-k: only consider the k most likely words, for example the top 40.
  • Top-p: only consider the smallest group of words whose chances add up to p, for example 90%.

Both cut off the long tail of unlikely words so the dice never land on something silly. Most of the time, changing temperature alone is enough.

Which setting should you use?

TaskTemperatureWhy
Code, maths, extracting dataLow (0 to 0.3)You want the same correct answer every time
Summaries, explanations, emailsMedium (0.5 to 0.8)Clear but natural
Brainstorming, stories, namesHigher (0.9 to 1.2)You want variety

In chat apps you usually cannot change temperature, but when you call a model through an API or run one on your own laptop you can. Our comparison of local model runners shows tools where you can try it yourself.

Quick recap

  • At every step, a model has a list of possible next words with chances.
  • Sampling picks one, with a bit of randomness. That is why answers change.
  • Temperature controls how much randomness: low for facts and code, higher for ideas.
  • Low temperature means consistent, not necessarily correct.

Rahul Agarwal

Instructor, SkillByAI

Web developer for 15 years. Teaches developers and founders to build real products with open-source AI tools.

Get the next tutorial by email

One email when a new post lands, with the code ready to run. No spam.

By subscribing you agree to receive emails from SkillByAI. Unsubscribe anytime.