Lesson 2 of 7 · Patterns
Routing
The routing workflow: an LLM or rule classifies each input and sends it down a specialised path. Prompt a router for JSON, validate it and fall back safely.
- 4 min read
- Intermediate
- Reviewed
- Use routing to send inputs to specialised workflows
- Route by difficulty to choose a cheaper or stronger model
- Prompt an LLM router for structured output and validate the reply
- Fall back safely when the router's answer can't be trusted
Different inputs, different paths
Routing appears in the Agent Architecture course as a way to pick specialists. As a workflow pattern it has two common uses:
- By type: refunds, technical problems and sales questions each get their own chain.
- By difficulty: easy inputs go to a small, fast model; hard ones to a stronger model.
An LLM router you can trust
A router model is asked for JSON only, and your code never trusts the reply until it's been parsed and checked:
import json
ROUTER_PROMPT = """Classify the customer message. Reply with JSON only:
{"route": "refund" | "tech" | "sales" | "other", "difficulty": "easy" | "hard"}
Message: """
# What a router model replied for four messages (a stand-in for real model output)
ROUTER_REPLIES = {
"I want my money back for order A-1": '{"route": "refund", "difficulty": "easy"}',
"The export crashes with error 0x88 on big files after the update": '{"route": "tech", "difficulty": "hard"}',
"Do you do discounts for schools?": '{"route": "sales", "difficulty": "easy"}',
"hmm": 'Sure! This looks like a general question.', # not JSON at all
}
HANDLERS = {"refund": "refund chain", "tech": "troubleshooting chain", "sales": "sales chain", "other": "human queue"}
MODEL_FOR = {"easy": "small model", "hard": "large model"}
def route(message):
reply = ROUTER_REPLIES[message] # in real code: call the router model with ROUTER_PROMPT + message
try:
decision = json.loads(reply)
except json.JSONDecodeError:
return "human queue", "-", "router reply wasn't JSON"
if decision.get("route") not in HANDLERS or decision.get("difficulty") not in MODEL_FOR:
return "human queue", "-", "unknown route"
return HANDLERS[decision["route"]], MODEL_FOR[decision["difficulty"]], "ok"
for message in ROUTER_REPLIES:
handler, model, note = route(message)
print(f"{message[:45]:<46} -> {handler:<22} {model:<12} {note}")Three messages were routed cleanly, each with a model size. The fourth reply wasn't JSON at all (models occasionally ignore format instructions), so it went to the human queue instead of crashing the workflow or being guessed.
Routing by difficulty pays
If 70% of inputs are easy and a small model handles them as well as a large one, routing them to the small model cuts cost and waiting time for most of your traffic, while hard inputs still get the stronger model. Check this on real data: label a sample as easy or hard, and compare both models' results on each group.
Common mistakes
Make an LLM router robust: never trust its reply until it's parsed and checked.
import json
ALLOWED = {"refund", "tech", "sales"}
def parse_route(reply):
# Turn a router model's reply into a route name. Return "human" if the reply isn't
# valid JSON, or if its "route" isn't one of ALLOWED.
return json.loads(reply)["route"]
print(parse_route('{"route": "tech"}'))
print(parse_route('{"route": "marketing"}'))
print(parse_route('I think this is a refund question'))
A router model replies with text that isn't valid JSON. What should the workflow do?
Summary
- Routing classifies inputs and sends them down specialised paths.
- Route by type (different chains) and by difficulty (different model sizes).
- Ask the router for JSON, then parse and validate it in code.
- Unclear or invalid router replies go to a fallback, never a guess.
Next: parallelisation, doing several things at once.
Finished reading? Tick it off to track your progress.