Sub topic 9
Combining rules, LLM calls, and cascades
This chapter assumes you can call a chat API from Python, read JSON, and interpret accuracy. It also assumes you have the classify and route functions from the earlier chapters. Here you will decide when a rule should handle an item, when the LLM should, and how to chain the two.
Three routing strategies
A router can decide the destination in three ways:
- Rule-based: keyword matches, regular expressions, or metadata such as the sender's address. Rules are fast, free, and predictable. They break when wording varies.
- LLM-based: the model reads the text and picks a label. It handles varied wording and ambiguity. It costs money, adds latency, and can be wrong in ways that are hard to predict.
- Hybrid: rules handle the clear cases and the LLM handles the rest.
| Strategy | Strength | Weakness |
|---|---|---|
| Rules | Cheap, fast, auditable | Brittle on new phrasing |
| LLM | Flexible, handles ambiguity | Slower, costs per call, can vary |
| Hybrid | Cheap on easy items, flexible on hard ones | Two systems to maintain |
When rules are enough
Use a rule when the signal is explicit and rarely wrong. A message sent to [email protected] is almost certainly about billing. A subject line that starts with "Invoice #" is a strong sign too. Rules like these are also easy to explain to the people who own each queue.
Avoid rules for vague signals. The word "charge" can appear in a billing complaint, but it can also appear in "my phone won't charge". A rule on that word would misroute the second message.
A rules-first cascade
A cascade tries a cheap step first and moves to a costlier step only when the cheap step cannot decide. The code below adds a rule layer in front of route.
import re
RULES = [
(re.compile(r"^\s*invoice\s*#", re.IGNORECASE), "billing-team"),
(re.compile(r"\b(reset|forgot)\b.*\bpassword\b", re.IGNORECASE), "account-services"),
]
def cascade_route(text, call_llm, threshold=0.7):
for pattern, queue in RULES:
if pattern.search(text):
return queue, "rule"
return route(text, call_llm, threshold), "llm"The function returns the queue and the step that made the decision. Recording that step lets you measure each layer separately later.
Common errors
- Rules that are too broad. A loose pattern sends items to the wrong queue, and the LLM never sees them. Test every rule against your labeled set before you trust it.
- Rules that go stale. Product names and phrasing change. Review rules on a schedule.
- Not logging the deciding step. If you do not record "rule" or "llm", you cannot tell which layer causes errors.
- Using the LLM for everything. Calls for trivial cases add cost and delay for no gain in accuracy.
Choosing a strategy
Start with the LLM alone if you have no labeled data and want a baseline fast. Then run your labeled set through it and look for traffic the LLM handles every time with high confidence, such as the sender-address cases. Move those to rules. Keep the LLM for the long tail.
Exercise
Your rule charge → billing-team sends "my laptop won't charge" to billing. Name two ways to fix it, and say which layer you would log to find such errors.
Show answer
Narrow the rule, for example to \b(charged|refund|invoice)\b, or remove it and let the LLM decide. Log the deciding step ("rule" or "llm") with each routing result. Then filter the misrouted items by "rule" to see that this rule caused them.
Exercise
In cascade_route, what queue does a message get if no rule matches and the LLM returns the label "other" with confidence 0.9?
Show answer
"human-triage". The route function looks up "other" in QUEUES, finds nothing, and falls back to human-triage even though confidence is above the threshold.