When to use rules instead of a model

A language model is the right tool for messy input. For anything you could write as an if statement, it is the expensive, unpredictable way to do it.

A laptop showing an order pipeline where a model extracts items from a WhatsApp message and rule checks decide price, stock and credit

Picture a distributor that takes orders from shops over WhatsApp. A developer is asked to automate it, and the first version hands everything to a language model: read the message, work out the items, look up prices, check stock, decide whether the shop's credit allows it, write the reply. It demos beautifully. Then in the second week it approves an order for a shop that was KES 14,000 over its limit, because the reply sounded reasonable and nobody had told it the limit in the right way.

The fix was not a better prompt. It was taking most of the work away from the model. The model now does one thing, reading the message, and plain code does everything that has a right answer.

This is a pattern we use everywhere at Stage 3 of the NGAZI ladder, where AI sits inside a business process, and it is the core idea behind UpeoXpense: AI reads, code decides.

The test: could you write it as an if statement?#

For each step in a process, ask whether a competent person could write down the rule. If they could, use code. If they could not, consider a model.

StepCan you write the rule?Use
Pull items and quantities out of "nipe cartons 10 za Ketepa"No, language is open-endedModel
Match "Ketepa 100s" to a product codeMostly; a synonym table plus fuzzy matchRules, model as fallback
Price each lineYes, from the price listRules
Check stockYes, a queryRules
Check the credit limitYes, arithmeticRules
Decide to hold or acceptYes, if any check fails, holdRules
Write a friendly reply in the customer's languageNo, tone and language varyModel

In most business workflows, the table looks like this: a model at the edges, where language comes in and goes out, and rules in the middle, where money and stock are decided.

Why rules win wherever they can#

They are the same every time. The credit check gives the same answer on Tuesday as on Friday. A model's answer can change with the wording of the input, the order of the context, or an update to the model itself.

They can be tested exhaustively. You can write a unit test for "limit 25,000, balance 20,800, order 4,200: accept" and another for "order 4,300: hold". You cannot prove a model will never approve the second.

They can be read by an auditor. When a shop asks why its order was held, the answer is a line of code and two numbers, not "the model thought so".

They are cheap and fast. A lookup costs nothing measurable. Every model call has a cost and a delay, which metering every request makes very visible.

Where a model genuinely earns its place#

None of this means models are a bad idea. They are excellent at the parts rules cannot handle:

  • Reading unstructured input. Free-text WhatsApp orders in English, Swahili and Sheng; photos of receipts and delivery notes; voice-note transcripts.
  • Mapping the long tail. When the synonym table has no match for "zile majani ya kijani", a model can suggest candidates for a person to confirm, and the confirmed answer goes back into the table.
  • Writing for people. Replies, summaries and explanations, where tone and language matter and there is no single right wording.

The pattern that comes out is consistent: the model turns messy input into structured data, the structured data goes through rules, and the model may turn the result back into friendly language. The model never holds the decision.

Make the boundary a contract#

The hand-off between model and rules should be a strict schema. The model returns, say, a list of items with a product phrase, a quantity and a unit. Code validates it: are quantities positive integers, are units from the allowed list, is anything missing? If validation fails, the message goes to a person, not back to the model for a guess.

Store the model's raw output beside the validated result. When something goes wrong, you can see whether the model misread the message or a rule did the wrong thing, which are very different bugs.

What to do this week#

Take one AI feature you have built or are building, and write out every step it performs as a table like the one above. Mark each step model or rules. For every step the model does that you could write as a rule, move it to code and add a unit test. Start with anything that touches money, stock or permissions.

All posts

Want this working in your business?

We build the AI systems we write about - for retail, clinics, workshops and property businesses across Africa. Tell us what you run and we will be honest about where it helps.