Sub topic 4
Rule-based routing
Rule-based routing sends each request to a small or large model using fixed checks written by you. This chapter assumes you know basic Python, have called a hosted model through an API, and understand per-token pricing. It builds on the idea that a router can only act on signals it can see before calling any model.
The working model
A rule-based router is a function from a request to a model name. It runs before any model call, costs almost nothing, and gives the same answer for the same input. That makes it easy to test, explain, and debug.
The router pays for exactly one model per request. A request goes to the small model or the large one, never both. This separates it from a cascade, which always pays for the small model first.
A first router
The router below uses two signals: prompt length and a list of task words that tend to need stronger reasoning.
def route(prompt: str) -> str:
text = prompt.lower()
hard_markers = ("prove", "step by step", "refactor", "analyze")
if len(prompt) > 4000 or any(m in text for m in hard_markers):
return "large"
return "small"
print(route("What is the capital of France?"))
print(route("Please analyze this contract for risks."))The first call prints small. The second prints large because it contains "analyze".
The rule is "default small, escalate on evidence of difficulty". The reverse default, "default large, downgrade on evidence of ease", is safer for quality but saves less money. Pick the default based on which mistake costs you more.
Choosing signals
Good rule signals are cheap to compute and correlate with difficulty. Common ones are:
- Prompt length, as a rough proxy for how much context the model must handle.
- Task words, such as "prove" or "refactor".
- The endpoint or feature that sent the request. A "summarize" button and a "write code" button can map straight to different models.
- The user's plan, such as free or paid.
- Required output format, such as strict JSON, which weak models often break.
Signals tied to the product, like the endpoint, are the most reliable. Signals read from free text, like keywords, are the least reliable because users phrase things in many ways.
Estimating what a rule saves
Once a rule sends a fraction of traffic to the small model, you can compute expected cost per request.
def expected_cost_router(frac_sent_to_small, cost_small, cost_large):
# A router pays for exactly one model per request.
return frac_sent_to_small * cost_small + (1 - frac_sent_to_small) * cost_large
# 70% of traffic to the small model, priced per request.
print(expected_cost_router(0.7, 0.001, 0.02))This prints about 0.0067. Without routing, every request costs 0.02, so the rule cuts cost by roughly two thirds. The estimate assumes the small model's answers are good enough. The rule must send hard requests to the large model, or the saving costs you quality.
Common errors
- Keyword false positives. "Analyze" appears in easy requests like "analyze this sentence's grammar". The request goes to the large model and wastes money.
- Keyword false negatives. A hard request phrased without any marker goes to the small model and gets a weak answer. The router never notices.
- Length as difficulty. A long prompt can be an easy task, such as extracting a date from a pasted email. A very short prompt can be hard.
- Rules that drift. Rules are written once and rarely revisited. User behavior and model quality both change, so old rules slowly become wrong.
- Silent rule growth. Each failure adds another keyword. After a while no one knows why the list looks the way it does.
When rules are enough
Rules work well when the traffic is predictable and difficulty follows the product's structure. For example, a support tool might send "reset my password" templates to the small model and "diagnose this error log" to the large one, based on which button the user pressed.
Rules work poorly when difficulty depends on content you cannot capture with simple checks. Two later chapters cover the alternatives. A cascade lets the small model signal its own uncertainty. A learned classifier replaces hand-written rules with a model trained on labeled examples.
Exercise
A router sends 60% of requests to the small model at 0.002 per request. The large model costs 0.05 per request. What is the expected cost per request, and name one way a keyword rule could send a request to the wrong model?
Show answer
Expected cost is 0.6 0.002 + 0.4 0.05 = 0.0012 + 0.02 = 0.0212 per request. A keyword rule can misroute by false positive (an easy request containing "analyze" goes to the large model) or by false negative (a hard request with no listed word goes to the small model).
Exercise
Why is the endpoint a more reliable routing signal than a keyword in the prompt text?
Show answer
The endpoint is set by your own product, so it is exact and does not depend on how users phrase things. Keywords come from free text, where the same task can be worded in many ways and the same word can appear in easy and hard requests.