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 word | Chance |
|---|---|
| mango | 30% |
| apple | 25% |
| banana | 20% |
| orange | 15% |
| something else | 10% |
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.
- Low temperature (close to 0): the top choice almost always wins. Answers are focused, predictable and repeatable.
- Medium temperature (around 0.7 to 1): a balance. Natural-sounding writing with some variety.
- High temperature (above 1): unlikely words get a real chance. Answers become creative, surprising, and sometimes nonsense.
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?
| Task | Temperature | Why |
|---|---|---|
| Code, maths, extracting data | Low (0 to 0.3) | You want the same correct answer every time |
| Summaries, explanations, emails | Medium (0.5 to 0.8) | Clear but natural |
| Brainstorming, stories, names | Higher (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.