Step 3 of 4
Routing & Decision Engines
Not every decision needs a generated paragraph. Use typed decisions to route work to code, small models, big models, or humans.
12 min read
A lot of LLM spend goes on work that isn't really generation. "Is this message about an order?" "Which team handles this?" "Does this reply break policy?" These are decisions - yes/no, pick one, or a score - and they deserve a different tool than a model that writes paragraphs. Treating them as typed decisions makes systems cheaper, faster, and much easier to test.
Decision engines vs LLMs#
Three decision primitives#
Almost every routing or gating question reduces to one of three shapes:
Yes / No
booleanIs this spam? Does the reply mention a refund? Is the user asking for a human?
Choice
enumWhich category: billing, shipping, returns, technical, complaint? Which model tier should handle it?
Score
0-1How urgent? How confident are we the answer is grounded? How likely is this to violate policy?
A decision engine is any component that returns one of these typed values instead of free text. It might be a rule, a small classifier, an embedding-similarity check, or an LLM constrained to structured output - what matters is the contract: a typed, validated answer your code can branch on.
Why typed decisions beat generated text#
| Free-text LLM answer | Typed decision | |
|---|---|---|
| Output | "It seems like this is probably a shipping question..." | category = "shipping", confidence = 0.93 |
| Parsing | Fragile string matching | None - it's already a value |
| Testing | Hard to assert on | Unit-testable like any function |
| Cost & latency | Full generation | Tiny classifier, or a few output tokens |
| Composability | Hard to combine | Evaluate several in parallel and combine with plain logic |
Because decisions are independent, you can evaluate several in parallel - category, urgency, and "wants a human?" at once - then combine them with ordinary code:
from typing import Literal
from pydantic import BaseModel, Field
class Triage(BaseModel):
category: Literal["order_status", "product", "returns", "complaint", "other"]
urgent: bool
wants_human: bool
confidence: float = Field(ge=0, le=1)
triage = llm.with_structured_output(Triage).invoke(message) # or a fine-tuned classifier
if triage.wants_human or triage.category == "complaint":
route_to_human(message)
elif triage.confidence < 0.7:
route_to_big_model(message) # unsure → spend more
Gating outputs, not just inputs#
Decisions are just as useful after generation. Before a reply leaves your system, run it through checks that return typed verdicts:
- 01Draft replyFrom any handler
- 02Policy checkOffers a banned discount? → score
- 03Grounding checkClaims supported by sources? → yes/no
- 04Send or escalatePlain if/else on the scores
Building a routed support desk#
Here's the pattern applied to a customer-support inbox. The router assigns each message a category, and each category gets the cheapest handler that can do the job well.
- Order statusCode: look up the order, fill a template
- Product questionSmall LLM + product docs
- ReturnsBig LLM + policy + order history
- ComplaintHuman agent, with a drafted summary
Six real-looking messages through the desk:
| Message | Route | LLM generation? |
|---|---|---|
| "Where's my order #48213?" | Order status → database lookup + template | No |
| "Has order 51907 shipped yet?" | Order status → database lookup + template | No |
| "Does the X200 work with USB-C docks?" | Product → small model with product docs | Yes (small) |
| "I want to return a gift without a receipt, and it was bought abroad." | Returns → big model with policy + history | Yes (large) |
| "Third time asking - this is unacceptable." | Complaint → human, with an auto-summary | No |
| "Can you reset my delivery address to my office?" | Order status → authenticated form link | No |
Four of the six messages never needed generated text at all, one used a small model, and only one needed the expensive model. Latency drops for the common cases, costs drop with them, and the one hard case gets more attention, not less.
What makes a router good#
- 1Categories are mutually exclusive and cover everything - always include an 'other' route.
- 2Low confidence routes up (to a bigger model or a human), never down.
- 3Deterministic handlers get the high-volume, well-defined cases.
- 4Every route is logged with its decision and confidence so you can audit and retrain.
- 5The router is evaluated on a labeled test set like any classifier - track accuracy per category.
- 6Outputs pass a final policy/grounding gate regardless of which route produced them.