When a Handwritten Rule Beats a Learned Model
A practical guide to choosing between explicit rules, learned models and hybrids, with a cost comparison you can adapt to your own case.
Key takeaways
- If the correct behavior is fully specified by a law, contract or formula, implement it directly.
- A learned model adds ongoing costs in data pipelines, monitoring, retraining and explanation that a short rule avoids.
- Compare a rule and a model on the same held-out cases, using the costs of each kind of mistake and not accuracy alone.
- Rules and models combine well, with rules as guardrails, fallbacks, filters or input features.
- A growing pile of exceptions that contradict each other is a sign that learning may now be the better tool.
A handwritten rule wins when you can state the logic correctly, when you lack the data to learn anything better, or when the gain from a model is too small to pay for building and looking after it. This happens often, including in teams that employ capable machine learning people.
A learned model earns its place when the pattern is real, involves many weak signals that interact, and is worth enough to justify the extra machinery. The choice should rest on a measured comparison, and frequently the answer is to use both.
Cases where the rule is the specification
Some questions have answers by definition. Is this date in the past? Does the order total exceed the free-shipping limit? Is the user a member of the group allowed to see this file?
Training a model to reproduce such logic gives you an approximation of something you already know exactly. It will be wrong occasionally, probably near the boundaries, which are where disputes arise. Nobody wants an access check that is right 99.7 percent of the time.
The same applies to physical or accounting identities. If revenue is price times quantity, multiply. A model may still predict the quantity.
Cases where data cannot support a model
Learning needs examples that resemble the future and carry reliable labels. You may have neither.
- A new product has no history.
- The event of interest is so rare that you have a handful of instances.
- Outcomes were never recorded, or were recorded inconsistently.
- Conditions just changed, so old data describes a different world.
- Privacy rules or licenses forbid using the data for this purpose.
An expert's rule can run from day one. It can also start the data collection that makes a model possible later, provided you log inputs, decisions and eventual outcomes. Be aware that the rule shapes what you observe: if it blocks a transaction, you may never learn whether it was legitimate. Letting a small, safe random sample through, where that is acceptable, preserves evidence.
The full price of a model
A rule is read in a code review. A model brings companions: a training dataset, feature code that must behave identically in training and serving, evaluation sets, a deployment path, monitoring and a plan for retraining.
| Concern | Handwritten rule | Learned model |
|---|---|---|
| Explaining one decision | Point to the condition | Needs attribution tools, which describe the model only |
| Changing behavior | Edit and review code | Change data or settings, retrain, re-evaluate |
| Failing quietly | When the world moves past the rule | Also when data pipelines or input distributions shift |
| Handling many weak signals | Poorly | Well, given enough data |
| Needed skills | Domain and software | Domain, software, data and statistics |
Rules are not free of decay. A threshold chosen three years ago can be badly wrong today, and nobody may be checking. Whatever you deploy needs an owner.
A comparison with numbers
Suppose a screening task has 200 true positive cases in a held-out month. All figures here are invented.
The existing rule flags 100 cases, of which 80 are true. Its precision is 80/100 = 0.80, and its recall, the share of real positives found, is 80/200 = 0.40.
A candidate model flags 160 cases, of which 120 are true. Precision is 120/160 = 0.75 and recall is 120/200 = 0.60.
Which is better depends on price. Say a missed case costs 10 and a false alarm costs 2:
rule: misses 200 − 80 = 120, false alarms 100 − 80 = 20
cost = 120 × 10 + 20 × 2 = 1,240
model: misses 200 − 120 = 80, false alarms 160 − 120 = 40
cost = 80 × 10 + 40 × 2 = 880 The model saves 360 units in the month. That must exceed what it costs to run and maintain, with some margin for the uncertainty in a single month's sample. If a miss cost 2 and a false alarm 10, the rule would score 440 and the model 560, and the rule would win.
Before accepting the result, check that both were judged on cases the model never trained on, that the team can absorb 60 extra flags, and that the gain holds across the groups you care about. Precision and recall and unequal error costs go deeper.
Improve the rule first
A fair contest uses a good rule. Many comparisons pit a carefully tuned model against a threshold nobody has revisited.
Spend an afternoon: look at the cases the rule gets wrong, tune its thresholds on training data, and add one obvious condition. A small decision tree can suggest candidate conditions, which you can then review and write by hand. Methods for interpretable models occupy the useful ground between the two, giving learned numbers in a form people can read.
def flag(amount, account_age_days, failed_logins):
"""Return True if a case should go to manual review."""
if failed_logins >= 5:
return True
return amount > 1000 and account_age_days < 30 Code like this is testable line by line. The thresholds are placeholders, and real ones should be checked against labeled cases. It is an illustration, not a validated fraud control.
Speed, size and where code must run
Constraints of the setting sometimes decide the matter. A check inside a payment path, a database trigger or a tiny embedded device may allow only microseconds and a few kilobytes. A handful of comparisons fits easily. A model may need a service call, with its own timeouts and outages, unless it is small enough to translate into plain code.
Stakes of explanation matter too. When a person is refused something, you may owe them a specific and stable reason. A written rule gives one directly, and changes to it leave an obvious record. With a model you need to plan how reasons are produced and verify that they reflect what the system did.
Also consider how a mistake gets fixed. If a rule misfires, you edit a line and ship it the same day. Correcting a model's behavior on a particular case may mean adding examples, retraining and checking that nothing else moved. Where urgent corrections are likely, keep an override rule in front of the model from the start.
How to run the contest fairly
- Freeze an evaluation set from a period later than anything used for tuning either contender.
- Make sure both see only information available at decision time.
- Match the workload: compare at the same number of flagged cases when review capacity is fixed.
- Report both kinds of error and the total cost, with an interval on the difference.
- List the cases where they disagree, and have someone who knows the work read a sample.
The disagreement list is often the most informative output. It shows whether the model finds something real or leans on an artifact, and it frequently suggests a new condition for the rule.
Hybrids that work
Most mature systems layer the two.
- Hard constraints around a model. Legal exclusions and sanity limits apply regardless of the score.
- Rules for the easy cases. Obvious approvals or rejections are handled directly, and the model scores the ambiguous middle.
- Rules as features. Each expert condition becomes an input, and the model learns how much to trust it.
- Rules as fallback. If the model is unavailable or an input is far outside its training range, the system reverts to simple logic.
- Model as advisor. The model proposes new conditions or ranks cases for human review.
If a rule filters cases before the model, train and evaluate the model on the population it will actually see. A model trained on everything may behave differently on only the leftover cases.
Signs the balance is shifting
Revisit the decision when you notice these:
- the rules file keeps growing and edits break older behavior,
- experts can judge examples but disagree on how to phrase the criterion,
- the inputs are images, audio or free text,
- adversaries adapt faster than you can write conditions,
- you now have a large, trustworthy set of labeled outcomes.
The reverse happens too. If a model's behavior turns out to be reproduced almost entirely by two conditions, consider replacing it. Retiring a model is a legitimate engineering result.
Either way, keep the comparison runnable. A saved evaluation set and a simple baseline let you rerun the contest whenever data or costs change.
Common questions
Are rules fairer than models?
Not automatically. A rule can encode an unfair criterion in plain sight, and a model can learn one indirectly. Rules are easier to inspect, but both need their outcomes measured across affected groups, and legal requirements vary by jurisdiction.
Can a language model replace hand rules?
It can handle fuzzy text judgments that are hard to write as conditions. It is less predictable and costlier per call than ordinary code, so exact checks such as formats, permissions and arithmetic still belong in code.