Lesson 41.2 · 2 of 5 · 5 min read · Reviewed 24 Nov 2026
Routing
By the end, you can
- Explain how routing splits inputs into specialised paths.
- Use routing to send easy requests to a cheaper model.
RoutingRouting: A workflow pattern that classifies an input and sends it to a specialised prompt, model or handler. puts a classification step first and then hands the input to one of several specialised handlers.
Example
A support inbox receives billing questions, technical problems and simple FAQs. A first call (or a conventional classifier) labels each message. Code then sends:
- billing questions to a prompt with refund policy and billing tools
- technical problems to a prompt with troubleshooting guides
- FAQs to a smaller, cheaper model with the FAQ document
Why it works
One prompt that tries to handle every case well tends to get long and conflicted: improving one category hurts another. Separate routes can each be optimised and evaluated on their own.
Design notes
- Give the classifier explicit categories with definitions and examples of borderline cases (chapter 11).
- Include an other or unsure route, and decide what it does: a general prompt or a human.
- Measure routing accuracy separately. A misrouted request fails no matter how good the downstream prompt is.
Key points
- Routing classifies the input, then sends it down a specialised path.
- Each route gets its own prompt, tools or model, tuned independently.
- Easy requests can go to a smaller, cheaper, faster model.
Route support messages and measure accuracy
Build a router with explicit categories and an unsure route, then measure routing accuracy on its own.
You will have: A keyword router, a labelled test set result with routing accuracy, and the list of misrouted messages.
Steps
- Read the labelled messages: each has the route a person chose.
- Write classify(text) with keyword rules for billing, technical and faq, returning unsure when no rule or several rules match.
- Send each route to its own handler, with faq going to a smaller model in the config.
- Measure accuracy against the labels and list every mistake.
- Fix one rule and see accuracy change.
Done when
- Every message gets exactly one route, including unsure.
- Accuracy is printed as a percentage of the labelled set.
- The mixed billing and technical message goes to unsure, not to a guess.
Open in the Playground No API key needed.
Starter code (Python)
ROUTES = {"billing": {"model": "large", "prompt": "refund policy + billing tools"},
"technical": {"model": "large", "prompt": "troubleshooting guides"},
"faq": {"model": "small", "prompt": "FAQ document"},
"unsure": {"model": "large", "prompt": "general prompt, or a person"}}
RULES = {"billing": ["refund", "invoice", "charged"], "technical": ["error", "crash", "login"], "faq": ["opening hours", "where is", "how do i"]}
labelled = [("I was charged twice", "billing"), ("The app shows error 502", "technical"),
("What are your opening hours?", "faq"), ("Refund for invoice 88 please", "billing"),
("Login fails and I was charged anyway", "unsure"), ("Where is my nearest branch?", "faq"),
("Can I change my username?", "unsure"), ("App crash on start", "technical")]
def classify(text):
# TODO: return the single matching route, or "unsure" for none or several
return "faq"
correct = 0
for text, label in labelled:
route = classify(text)
correct += route == label
print(f"accuracy: {correct / len(labelled):.0%}")
Show a solution
ROUTES = {"billing": {"model": "large", "prompt": "refund policy + billing tools"},
"technical": {"model": "large", "prompt": "troubleshooting guides"},
"faq": {"model": "small", "prompt": "FAQ document"},
"unsure": {"model": "large", "prompt": "general prompt, or a person"}}
RULES = {"billing": ["refund", "invoice", "charged"], "technical": ["error", "crash", "login"], "faq": ["opening hours", "where is", "how do i"]}
labelled = [("I was charged twice", "billing"), ("The app shows error 502", "technical"),
("What are your opening hours?", "faq"), ("Refund for invoice 88 please", "billing"),
("Login fails and I was charged anyway", "unsure"), ("Where is my nearest branch?", "faq"),
("Can I change my username?", "unsure"), ("App crash on start", "technical")]
def classify(text):
t = text.lower()
hits = [route for route, words in RULES.items() if any(w in t for w in words)]
return hits[0] if len(hits) == 1 else "unsure"
correct, mistakes = 0, []
for text, label in labelled:
route = classify(text)
print(f"{route:<9} ({ROUTES[route]['model']} model) <- {text}")
if route == label:
correct += 1
else:
mistakes.append((text, route, label))
print(f"routing accuracy: {correct / len(labelled):.0%}")
print("misrouted:", mistakes)