Sub topic 8
Routing Items to Handlers
This chapter assumes you can call a chat API from Python, read JSON, and know what accuracy means. It also assumes you have a classify function that returns a (label, confidence) pair, as built in earlier chapters. Here you turn that prediction into an action: sending each message to the right handler.
From label to destination
Routing means choosing where an item goes next. The classifier produces a label. The router maps that label to a queue, a team, or a function. Keep this mapping in plain code, not in the prompt. A lookup table is easy to read, test, and change without touching the model.
QUEUES = {
"billing": "billing-team",
"technical": "support-engineering",
"account": "account-services",
}The table has no entry for "other". That is deliberate. The fallback label has no team that owns it, so it needs a different destination: a human.
Two reasons to send an item to a person
An item should go to human triage in two cases:
- The model is not confident. The label may be valid, but the score is below your threshold.
- The label has no handler. This covers
"other"and any label you add later and forget to map.
The router below handles both cases.
def route(text, call_llm, threshold=0.7):
label, confidence = classify(text, call_llm)
if confidence < threshold:
return "human-triage"
return QUEUES.get(label, "human-triage")QUEUES.get(label, "human-triage") returns the default when the key is missing. Using QUEUES[label] would raise a KeyError on an unmapped label and crash the pipeline on the very input you most need to handle safely.
Recall that classify returns ("other", 0.0) when the model output is malformed after all retries. Because 0.0 is below any sensible threshold, a parsing failure also lands in human triage. No extra code is needed.
Choosing the threshold
The default of 0.7 is a starting guess. Model-reported confidence is not a calibrated probability. A model may say 0.9 and still be wrong fairly often. Pick the threshold by testing on labeled examples, not by intuition.
Raising the threshold sends more items to humans. Errors drop, but workload rises. Lowering it automates more items and lets more mistakes through. The right balance depends on what a wrong route costs. Misrouting a password reset is cheap to fix. Misrouting a legal complaint may not be.
Common errors
- Indexing the table directly. Use
.getwith a default so unknown labels fail safe. - Routing on label alone. Always check confidence first.
- Mapping
"other"to a busy team. This turns the fallback into a dumping ground. Send it to triage and review what lands there. - Hard-coding queue names inside the prompt. The model then has to produce them exactly, and a rename breaks the prompt.
- Forgetting to log the decision. Without a record of text, label, confidence, and destination, you cannot debug a bad route later.
Check your understanding
Exercise
A new label "shipping" is added to LABELS, but nobody adds it to QUEUES. A message is classified as "shipping" with confidence 0.95. Where does route send it, and why is that acceptable?
Show answer
It goes to "human-triage". The confidence is above the threshold, but QUEUES.get("shipping", "human-triage") finds no entry and returns the default. The message is not lost or crashed on, and the missing mapping shows up as a pile of items in triage that you can notice and fix.
Exercise
Why is it better to keep the label-to-queue table in code than to ask the model to output the queue name directly?
Show answer
The table can be tested, reviewed, and changed without editing the prompt or re-evaluating the model. The model only has to pick from a small label set, which is easier to validate than free-form queue names that can drift or be misspelled.