0% completed
Why Does the Same Question Give a Different Answer Each Time?
On This Page
Part 1: The Program Chooses From a List of Chances
Part 2: Why the Program Uses Randomness
Part 3: One Early Difference Changes the Rest
Part 4: Other Sources of Difference
Part 5: How to Make Results More Stable
Common Wrong Beliefs
What This Means for Your Work
Try It Yourself
Check Your Understanding
Key Takeaways
Short answer: A model does not produce one fixed answer. It writes an answer in small pieces of text called tokens. Before each token, the model calculates a chance for every possible next token, and the program chooses from that list of chances with some randomness. One different token early in an answer changes everything after it, because the model reads the tokens already chosen each time it produces a list. Changed instructions, an updated model, and small numeric differences on the hardware can also change an answer.
Look at the list on the left. It is the same in all three runs, but each run begins with a different token.
This lesson uses the request in the picture. An engineer sends it to the same assistant three times:
Suggest a name for a function that removes duplicate orders.
The three answers contain three names: remove_duplicate_orders, deduplicate_orders, and drop_duplicate_orders.
Part 1: The Program Chooses From a List of Chances
What Happens Inside an LLM When I Ask a Question? describes how an answer is produced one token at a time. In each calculation (a pass), the model gives every possible next token a chance between 0 and 1. The program then chooses one token and adds it to the text.
For the first token of the function name, the list could be the following: "remove" gets 0.40, "ded" gets 0.25, "drop" gets 0.16, "filter" gets 0.12, and "unique" gets 0.07. The numbers are illustrative, which means that they are invented for this example. A real list holds every token that the model can produce, and this list is shortened to five tokens whose chances together equal 1. In one widely used tokenizer (the program that cuts text into tokens), "ded" is the first token of both "dedupe" and "deduplicate".
The program does not always take the token with the highest chance (the top token). It makes a random choice, and a token with a higher chance is chosen more often. This step is called sampling. The token "remove" is therefore chosen in about 40 of 100 runs, and the other 60 runs begin with a different token.
Part 2: Why the Program Uses Randomness
The program could take the top token in every pass. The name would then be remove_duplicate_orders in every run. However, the companies that run models (providers) use sampling by default, for two reasons.
First, taking the top token in every pass often gives worse text when the text is long and can continue in many ways. Researchers at the University of Washington and two other institutions compared the two methods on long texts. They reported that, in their tests, always choosing the most likely text produced uninteresting output that sometimes repeated the same phrase in a loop.
Second, different answers (variation) are often what the user wants. An engineer who asks for a function name a second time wants a different name and not the same one again. The same is true for a draft of an email, a list of test cases, or ideas for a design.
Part 3: One Early Difference Changes the Rest
One random choice would have little effect if it changed only one token. It changes much more, because every pass reads all the tokens that were chosen before it.
Notice the column for pass 2. After "remove", the likely next token is "_duplicate". After "ded", the likely next token is "uplicate" or "upe", and "_duplicate" is now an unlikely next token.
The difference continues after the name. The assistant usually adds a short code example and a sentence that explains the name, and both of them repeat the chosen name. An answer of 200 tokens contains 200 choices, so two long answers to one question are rarely identical.
Part 4: Other Sources of Difference
Sampling is usually the main source of variation, and three other sources exist. In each of them, the engineer types the same request about duplicate orders, but the calculation is different.
- The application changed its instructions. The model receives the question together with the instructions written by the developer of the application (the system prompt). The developer can edit those instructions at any time, and users do not see the change.
- The provider updated the model. Some model names always point to the newest version. When the provider releases a new version under that name, the same request reaches a different model.
- The hardware calculates with small numeric differences. A computer rounds decimal numbers, so the result of a long calculation depends slightly on the order of its steps. That order can differ between machines. When two tokens have almost equal chances, a tiny rounding difference can change which one has the highest chance.
The third source remains when the program does not use sampling. Researchers at two universities and a software company ran models that always took the top token. The output still changed when they changed the number or type of chips, or the number of requests processed together. Two large providers state in their documentation that results can still vary at a temperature of 0. Part 5 describes this setting.
Part 5: How to Make Results More Stable
Variation helps the engineer who wants several names to choose from. Variation is a problem when another program depends on the answer. One example is a script that generates code and must produce the same function name in every build.
Look at the four cards from left to right. Each one reduces one source of difference:
- Lower the temperature. Temperature is a setting that makes the program choose the top tokens more often or less often. A related setting, top-p, removes the tokens with the lowest chances from the list before the choice. The next lesson explains both settings, and several of the newest models do not accept them.
- Fix the seed where the API has one. A seed is the starting number of the random generator, and the same seed gives the same series of random choices. Send the same seed with every request. Some APIs offer a seed setting, and at least one of them states that it tries to repeat the result and does not guarantee it.
- Pin the model version. Use the name of one fixed version and not a name that always points to the newest version.
- Fix the output format. Ask for a fixed structure, such as JSON with one field called
name. With this structure, many tokens of the answer are always the same, so fewer choices can differ.
These settings reduce variation and do not remove it. A program that needs the same name tomorrow should store the name and not ask for it again.
Common Wrong Beliefs
| Belief | What actually happens |
|---|---|
| The model changes its opinion between two runs. | The model produces nearly the same list of chances for the same input. The program chooses a different token from that list. |
| A different answer means that one of the two answers is wrong. | Several tokens can be good next tokens. All three function names in the example are acceptable. |
| The randomness is a defect that providers have not fixed yet. | Providers choose to use sampling, because it often gives better long text and gives the user different answers to choose from. |
| A temperature of 0 gives exactly the same answer every time. | It removes most of the variation. Small numeric differences on the hardware and model updates can still change the answer. |
What This Means for Your Work
- Run an important prompt several times before you judge it. One run shows one sample of what the prompt can produce. Five runs show the range.
- Do not compare the output with an exact string in a test. Check properties of the output, such as "the name is a valid Python identifier". How Do I Test an LLM Feature When the Output Changes Every Run? covers this subject.
- Check a factual answer that changes between runs. Three different names for a function are acceptable. Three different values for a fact mean that at least two of them are wrong.
- Store an answer that you need again. If other code already uses
deduplicate_orders, save that name and do not expect the next run to return it. - Record the model version and the settings with each result. Without them, you cannot know whether a changed answer came from sampling or from a changed model.
Try It Yourself
The program below is a small, simplified version of the choosing step. A real model calculates a new list of chances in every pass from the whole text so far. The simplified program reads the chances from a small table and uses only the last token. Run it and read the three sections of the output.
Section 1 prints three names for the seeds 4, 5, and 6: remove_duplicate_orders, deduplicate_orders, and drop_duplicate_orders. Section 2 always takes the top token and prints remove_duplicate_orders three times. Section 3 counts the first token of 1,000 sampled runs, and "remove" is chosen 391 times, which is close to its chance of 0.40.
Now change SEEDS to [9, 9, 9] and run the program again. Section 1 prints dedupe_orders three times, because the same seed gives the same random choices. Notice that a fixed seed repeats one result, and that this result is not always the name with the top tokens.
Check Your Understanding
An engineer sends the same prompt to the same model twice. Both answers begin with the same 11 tokens, and starting at the 12th token they are completely different. What explains this?
Key Takeaways
- The program chooses each token from a list of chances with some randomness. This step is called sampling, and it is usually the main reason that two runs differ.
- One early difference changes the rest of the answer. Every later pass reads the tokens that were already chosen.
- Three other sources also change an answer. They are changed instructions in the application, an updated model, and small numeric differences on the hardware.
- Settings reduce variation and do not remove it. Lower the temperature where the model allows it, fix the seed, pin the model version, fix the output format, and store the results that you need again.
The next lesson, What Do Temperature and Top P Control?, shows the arithmetic of the two settings that change the choosing step.
Reading Progress
0%
On This Page
Part 1: The Program Chooses From a List of Chances
Part 2: Why the Program Uses Randomness
Part 3: One Early Difference Changes the Rest
Part 4: Other Sources of Difference
Part 5: How to Make Results More Stable
Common Wrong Beliefs
What This Means for Your Work
Try It Yourself
Check Your Understanding
Key Takeaways