Sub topic 8
Triage and routing agents
This chapter assumes you know how a handoff works (a tool call that switches the active agent) and can read basic Python and JSON. A triage agent is the usual first agent in a handoff system. This chapter covers how to build one that routes reliably.
What a triage agent does
A triage agent reads the user's request and decides who should handle it. It does not solve the problem itself. Its only output is a routing decision: hand off to a specialist, ask a clarifying question, or escalate to a person.
Keeping triage narrow has two benefits. The prompt stays short, so the model classifies more consistently. The agent also needs no domain tools, so a misrouted request cannot cause a harmful action.
Designing the routes
Each route needs a one-sentence description the triage model can compare against the request. Good route sets share three properties:
- Routes do not overlap. If "billing" and "refunds" both claim a duplicate charge, the model will split between them.
- Every route has a clear owner with the right tools.
- A fallback route exists for requests that fit nothing.
Routes defined as handoff tools put the description in the tool schema, so the model sees the choices as ordinary tool options. A handoff to an unknown target should fail loudly rather than guess.
Validating the decision
The model's routing output is untrusted input. It may name a route that does not exist, return malformed JSON, or choose a route with low confidence. Check it in code before acting on it.
import json
ROUTES = {"billing", "technical", "refunds"}
FALLBACK = "human_review"
def route(classify, request: str) -> str:
"""classify(request) returns JSON such as {"route": "billing", "confidence": 0.9}."""
try:
decision = json.loads(classify(request))
target = decision["route"]
confidence = float(decision["confidence"])
except (ValueError, KeyError, TypeError):
return FALLBACK
if target not in ROUTES or confidence < 0.7:
return FALLBACK
return target
def fake_classify(request: str) -> str:
if "charged" in request:
return '{"route": "billing", "confidence": 0.92}'
if "crash" in request:
return '{"route": "technical", "confidence": 0.4}'
return '{"route": "weather", "confidence": 0.95}'
print(route(fake_classify, "My card was charged twice")) # billing
print(route(fake_classify, "The app will crash")) # human_review
print(route(fake_classify, "Will it rain?")) # human_reviewThe second request names a valid route but with low confidence. The third names a route that does not exist. Both go to the fallback.
Common errors
- Overloaded triage prompt. Adding business rules to triage turns it into a second specialist. Move the rules to the specialist's instructions.
- No fallback. Without one, the model is forced to pick a wrong route for out-of-scope requests.
- Routing on the first message only. A user may change topic mid-conversation. Let specialists hand back to triage, and keep a turn limit so the agents cannot bounce the request between them forever.
- Trusting self-reported confidence. Model confidence is poorly calibrated. Treat the threshold as a tunable value and measure it on labeled examples.
Exercise
A triage agent sends 15% of "I was charged twice and want my money back" requests to billing and 85% to refunds. What is the likely cause, and what are two fixes?
Show answer
The route descriptions overlap, so the model cannot tell which one owns the request. Fix it by rewriting the descriptions so that each request type belongs to exactly one route (for example, refunds covers returned goods, billing covers payment errors). Alternatively, merge the two routes into one agent. You can also add labeled examples of this request to the triage prompt.