Sub topic 2
Deterministic logic versus probabilistic behavior
This chapter assumes you know how a typical software feature is built (inputs, logic, outputs) and have a general sense of what machine learning models and large language models (LLMs) do. You should also be able to read short Python snippets.
Choosing between rules and AI is mostly a choice between two kinds of behavior. The rest of this tutorial depends on understanding that difference, because it determines how you test, ship, and support a feature.
Two kinds of behavior
A deterministic system returns the same output every time it receives the same input. A tax calculator, a password check, and a sort function are deterministic. If the output is wrong, the logic is wrong, and you can find the faulty line and fix it.
A probabilistic system produces outputs that are likely to be right but are not guaranteed. A spam classifier, a recommendation ranker, and an LLM that drafts a reply are probabilistic. They learned patterns from data, and they apply those patterns with some uncertainty. A wrong output does not point to a single faulty line. It is a normal part of how the system works.
Two properties are worth separating, because people often blur them:
- Repeatability: whether the same input gives the same output. An LLM sampled with a nonzero temperature is not repeatable. A classifier that always returns its top label for a fixed input is repeatable.
- Guaranteed correctness: whether the output is always right. Even a repeatable model can be wrong every time on the same input. Repeatability does not mean correctness.
A deterministic rule can also be wrong, but it is wrong in a predictable way. If the rule "refund" in text misroutes a message, it misroutes every message with that pattern, and you can see why.
What changes when behavior is probabilistic
Several practical consequences follow. Each one affects product decisions later in this tutorial.
Correctness becomes a rate
For a deterministic feature, a test either passes or fails. For a probabilistic feature, you measure a rate: 92% of messages are routed correctly, or 4% of summaries contain an unsupported claim. You have to decide in advance which rate is acceptable. The chapter on error cost covers how to set that threshold.
Failures are spread across inputs
A rule fails on a known class of inputs. A model fails on a scattered set of inputs that are hard to predict. Some failures cluster around rare cases, such as a dialect, a new product name, or an unusual format. Averages hide this. A model that is 95% accurate overall can be 70% accurate for one customer segment.
Bugs are harder to reproduce and fix
With a rule, you add a condition and the bug is gone. With a model, you may retrain, change a prompt, or add examples. Each change can fix one case and break another. This is called a regression. You need a fixed test set so that you can notice when a fix in one place causes a failure elsewhere.
Behavior can change without a code change
A deterministic feature changes when your code changes. A probabilistic feature can change when:
- the model provider updates a hosted model
- the input data shifts over time (called drift)
- you edit a prompt
- the retrieval index or context data changes
You should treat these as releases, even if no one deployed code.
Explanations are weaker
You can explain a rule by pointing to it. You usually cannot explain a model's output the same way. If a user, an auditor, or a support agent needs to know exactly why a decision was made, that is a point in favor of deterministic logic.
A side-by-side comparison
| Property | Deterministic logic | Probabilistic AI |
|---|---|---|
| Same input, same output | Always | Not always (sampling) and not guaranteed right |
| Quality measure | Pass or fail tests | Error rate on a test set |
| Failure pattern | Predictable, tied to a rule | Scattered, strongest on rare cases |
| Fixing a bug | Edit the logic | Change data, prompt, or model, then re-test |
| Changes over time | Only when code changes | Can change with data, provider, or prompt |
| Explaining a result | Point to the rule | Limited, often approximate |
| Handles unseen variation | Poorly, unless anticipated | Often well, within limits |
| Cost per request | Very low | Often higher, sometimes much higher |
The row about unseen variation is the main reason to accept the other costs. Rules need every case written out in advance. Models can generalize from examples to inputs nobody listed. That is the benefit you are paying for.
A worked example: measuring the difference
You can see the repeatability difference in a few lines. The first function is deterministic. The second simulates a model that returns a label with some probability, using a seeded random generator so the output is reproducible here.
import random
def rule_classify(text: str) -> str:
if "refund" in text or "invoice" in text:
return "billing"
return "bug"
def model_classify(text: str, rng: random.Random) -> str:
# Simulated model: right 85% of the time for this input.
correct = "billing" if "refund" in text or "invoice" in text else "bug"
wrong = "bug" if correct == "billing" else "billing"
return correct if rng.random() < 0.85 else wrong
text = "please refund my order"
rng = random.Random(7)
rule_outputs = {rule_classify(text) for _ in range(1000)}
model_outputs = [model_classify(text, rng) for _ in range(1000)]
print("rule distinct outputs:", rule_outputs)
print("model billing rate:", model_outputs.count("billing") / 1000)The rule always returns billing, so the set has one element. The simulated model returns billing roughly 85% of the time, so the same input yields two different answers across 1,000 calls. This is a simulation, not a real model, but it shows why a single manual check proves little. One correct answer from a model does not show that the next call will also be correct.
The takeaway for testing: run many examples and report a rate, not a single pass.
Common mistakes
- Treating a demo as proof. A model that handles five hand-picked examples may fail on 20% of real traffic. Demos show the best case.
- Assuming a temperature of zero makes a model deterministic. It reduces variation, but hosted models can still differ between calls because of batching, hardware, or version updates. Do not build logic that depends on byte-identical output.
- Using a model for a task with an exact answer. Arithmetic, date math, permission checks, and lookups by ID have a correct answer that code can compute. A model can approximate them, but code gets them right every time.
- Forcing rules onto fuzzy problems. If the rule list keeps growing and still misses cases, the problem may be a pattern-recognition problem. The next chapters cover how to tell.
- Ignoring the combined design. Many good features use both. The model proposes, and deterministic code validates, formats, or enforces limits.
Combining both kinds of behavior
A common pattern is to let the model handle the fuzzy part and keep deterministic code around it. The model might extract a refund amount from a free-text message. Code then checks that the amount is a positive number, below a policy limit, and matches the order record. If a check fails, the feature falls back to a human or a form.
def validate_refund(extracted: dict, order_total: float, limit: float = 500.0) -> str:
amount = extracted.get("amount")
if not isinstance(amount, (int, float)) or amount <= 0:
return "send to human review"
if amount > order_total or amount > limit:
return "send to human review"
return "approve for processing"
print(validate_refund({"amount": 40.0}, order_total=120.0))
print(validate_refund({"amount": 900.0}, order_total=1000.0))
print(validate_refund({"amount": "forty"}, order_total=120.0))The first call prints approve for processing. The second and third print send to human review. The model's output is never trusted directly. It is treated as an untrusted input that code must check. This pattern limits the damage a wrong output can do, and the chapters on error cost and human oversight build on it.
Decision questions from this chapter
Use these questions when you assess a feature. They feed the scorecard later in the tutorial.
- Is there a single correct output for each input? If so, prefer deterministic logic.
- Can the product tolerate a nonzero error rate, and have you named the rate?
- Does anyone need an exact explanation of each result?
- Can deterministic code validate or limit the model's output?
- Who will notice and respond when behavior changes without a code release?
Exercise
A feature converts a user's typed message, such as "move my meeting to next Tuesday at 3," into a calendar update. Which parts would you build with deterministic logic and which with a probabilistic model? Name one check that protects the user from a model error.
Show answer
Use a model (or a language parser) to interpret the free-form phrase and propose a date, time, and meeting. Use deterministic code for everything with a single correct answer: resolving "next Tuesday" against the current date and the user's time zone once the model gives a structured value, checking the slot is free, checking the user has permission to edit the meeting, and writing the update. A protective check is to show the user the parsed result ("Tuesday, Dec 2, 3:00 PM") and require confirmation before saving. A second check is to reject any output that is not a valid date in the future.
Exercise
A team sets the model temperature to 0 and says the feature is now deterministic, so they will test it with one example per case. What is wrong with this reasoning?
Show answer
Temperature 0 reduces random variation but does not guarantee identical outputs, because hosted models can change with provider updates, batching, or hardware. More importantly, repeatability is not correctness. A model can return the same wrong answer every time. One example per case shows nothing about the error rate across real inputs. The team should test on a larger labeled set, report a rate, and re-run the set whenever the model, prompt, or data changes.