Sub topic 4
Why the Same Input Can Produce Different Outputs
This chapter assumes you understand prompts, model inference, and basic Python. It also assumes you have seen the classic machine learning pattern of training, validation, and deployment.
A traditional program usually maps the same input to the same output. An LLM application often does not. The difference can come from the model, the sampling settings, the retrieved context, or the surrounding application.
Where variation comes from
A language model predicts a probability distribution over possible next tokens. A token is a small piece of text, such as a word or part of a word. The application then selects tokens repeatedly until it produces a response.
For example, after the prompt The capital of France is, the model might assign these probabilities:
| Next token | Probability |
|---|---|
| Paris | 0.96 |
| Lyon | 0.01 |
| Marseille | 0.01 |
| Other tokens | 0.02 |
For an open-ended prompt, several continuations may have similar probabilities. Sampling can select different continuations on different requests.
Common sources of variation include:
- Temperature: Higher values flatten the probability distribution and make less likely tokens more selectable. Lower values make the most likely token more dominant.
- Top-p or top-k sampling: These settings restrict selection to a subset of likely tokens.
- Ties and numerical differences: Even greedy decoding, which selects the most likely token, can vary when candidates have nearly equal scores or when hardware calculations differ.
- Model updates: A provider can change the model behind a label, or you can change the model identifier yourself.
- Context changes: Retrieval may return different documents, ordering may change, or a document may be updated.
- Application state: Conversation history, tool results, timestamps, user preferences, and feature flags can alter the effective prompt.
- Output limits: A response cut off by a token limit may take a different path when a small probability changes early in generation.
The effective input is therefore more than the visible user message:
effective_input =
system instructions
+ conversation history
+ retrieved context
+ tool results
+ current user message
+ runtime settingsA small sampling example
The following program illustrates why a fixed probability distribution can produce different results. A random seed makes one run repeatable, but it does not make production requests repeatable unless every relevant input and implementation detail is also controlled.
import random
choices = ["accept", "review", "reject"]
weights = [0.70, 0.25, 0.05]
for seed in [1, 2, 3]:
random.seed(seed)
result = random.choices(choices, weights=weights, k=1)[0]
print(f"seed={seed}: {result}")A seed is useful for debugging. It is not a complete reproducibility strategy. Many hosted APIs do not expose a seed, and even an exposed seed cannot control changing model weights, retrieved documents, or tool responses.
Testing non-deterministic behavior
Tests should measure properties of an answer rather than one exact wording. Useful checks can verify that an answer:
- contains required facts,
- follows a schema,
- cites an allowed source,
- avoids prohibited content,
- stays below a length limit,
- satisfies a business rule.
Run each evaluation case several times and record a pass rate. A case that passes once but fails frequently is not reliable enough for release.
Practical reproducibility levels
Choose the level that matches the risk:
- Debugging: Save the effective prompt, model identifier, settings, retrieved documents, and tool inputs.
- Offline evaluation: Freeze the model identifier, test data, retrieval snapshot, and evaluator version. Run cases repeatedly.
- Production correctness: Validate structured outputs and business rules. Add retries, fallbacks, and human review where failures matter.
- Strict reproducibility: Use a self-hosted, pinned model and controlled inference environment. Accept the operational cost and reduced flexibility.
The goal is usually not identical text. The goal is stable behavior within defined quality boundaries.
Exercise
A support summarizer sometimes writes one sentence and sometimes three, but both versions include the ticket's issue and requested action. Should this automatically fail evaluation?
Show answer
No. If the contract permits either length, evaluate the required facts and format rather than exact wording. Add a length check only when length is a real product requirement.