Dayward AIWeChat Mini Program
From Frontend Engineer to Agent Engineer in 30 Days
Week 3 ยท D16๐Ÿ”’ LockedAbout 6 hours

Dynamic Routing With a Supervisor: Structured-Output Routing, Override, routingReason

Implement a supervisor node that uses structured output to dynamically decide which subagent to route to, and support human override plus recording the reason behind each routing decision.

Today's goals 0/3

Sign in to tick these off and save your progress.

Today's Goals

  1. Implement a supervisor node that outputs a routing decision as structured output
  2. Add a routingReason to the routing result to make debugging and retrospectives easier
  3. Implement an override mechanism for a human to correct the model when it routes incorrectly

Yesterday's graph contained one obvious fiction: all three edges hard-wired, with the same handler always following the entrance. Today one of those edges gets its destination decided by the model at runtime, and we think through "what if it decides wrong" once and for all.

Plain-Language Walkthrough

A dispatch desk does not work the case, it decides whose case it is

When you call an emergency line, the person answering does not chase the thief or fight the fire. They do one thing: hear your first two sentences, decide whether this is police, traffic, or fire, and pass the ticket to the matching department. The whole call may last twenty seconds, and those twenty seconds decide everybody else's workload โ€” send it to the wrong department and the responders discover the mistake on arrival, it gets transferred again, and all the time went into travel.

That is the Supervisor pattern: one node dedicated to deciding who should take this case, handing it off and doing no work itself. In our e-commerce support scenario there are three takers, and their names do not change this week: order_lookup for order status and shipping progress, refund_draft to draft a refund proposal by the rules without executing a refund, and smalltalk for chat and as the catch-all.

Why make the decision its own node rather than letting the order-lookup agent judge for itself? Because deciding and executing are two different kinds of work. Deciding needs the whole picture, speed, and cheapness, usually in a few dozen tokens; executing needs detail, tools, and a pile of rules. Mixed together you can neither swap a smaller model in for the decision alone nor measure triage accuracy separately โ€” and that metric happens to be the most worthwhile one to watch in a multi-agent system.

Technically the Supervisor rests on a conditional edge: this edge's destination is unknown at compile time and is decided at runtime by a selector function. Yesterday's three unconditional edges hard-wired the order; today's edge hands "where next" to a field in the state.

graph.js
import { StateGraph, START, END } from '@langchain/langgraph'
 
// The supervisor writes two fields only: who takes it (route) and why (routingReason).
// It does not answer the user's question
const supervisor = async (state) => decide(lastText(state))
 
// The selector reads state and calls no model: the decision was already made inside
// supervisor, and this only translates it into a node name
const selectRoute = (state) => state.route ?? 'smalltalk'
 
export const graph = new StateGraph(AgentAnnotation)
  .addNode('supervisor', supervisor)
  .addNode('order_lookup', orderLookup)
  .addNode('refund_draft', refundDraft)
  .addNode('smalltalk', smalltalk)
  .addEdge(START, 'supervisor')
  // The third argument maps the selector's return value to a node name. Write it out in
  // full: returning an unregistered name is a compile-time error, and a registered node
  // nobody points at makes compile() throw UnreachableNodeError
  .addConditionalEdges('supervisor', selectRoute, {
    order_lookup: 'order_lookup',
    refund_draft: 'refund_draft',
    smalltalk: 'smalltalk',
  })
  .addEdge('order_lookup', END)
  .addEdge('refund_draft', END)
  .addEdge('smalltalk', END)
  .compile()

All four teach one thing: turn "where next" from a hard-wired order in the code into one runtime table lookup. The mechanics are now covered, and everything remaining is the genuinely hard question โ€” what goes inside decide. Can you just have the model say "I think the order-lookup colleague should see this" and parse that sentence?

Sign in and link WeChat to read

Sign in, then link your WeChat mini program account on the account page. Linked accounts share one free article per day. Unlock additional articles with rewarded videos in the mini program.

Mini program QR code

Scan in WeChat to open Dayward AI. Open the same course and chapter, choose the rewarded video, then refresh this page after completion.

Interview questions

  • How is the routing decision usually implemented in a supervisor pattern? What should that node do, and what should it not do?Supervisor ๆจกๅผ้‡Œ็š„่ทฏ็”ฑๅ†ณ็ญ–ไธ€่ˆฌๆ€Žไนˆๅฎž็Žฐ๏ผŸ่ฏท่ฏด่ฏด่ฟ™ไธช่Š‚็‚น่ฏฅๅšไป€ไนˆใ€ไธ่ฏฅๅšไป€ไนˆใ€‚
    Common in ChinaCommon overseasBasic#multi-agent#routing#langgraph

    ๐Ÿ”’ Unlock this chapter to see the answer keys.

  • Why use structured output rather than natural language for routing? What exactly goes wrong with free text?ไธบไป€ไนˆ่ฆ่ฎฉๆจกๅž‹่พ“ๅ‡บ structured output ่€Œไธๆ˜ฏ่‡ช็„ถ่ฏญ่จ€ๆฅๅš่ทฏ็”ฑ๏ผŸ่‡ช็„ถ่ฏญ่จ€ๅˆฐๅบ•ๅทฎๅœจๅ“ช๏ผŸ
    Common in ChinaCommon overseasIntermediate#structured-output#routing#reliability

    ๐Ÿ”’ Unlock this chapter to see the answer keys.

  • How should the system handle an uncertain or wrong routing decision, and how do you pick the threshold?่ทฏ็”ฑไธ็กฎๅฎšๆˆ–่€…่ทฏ็”ฑ้”™่ฏฏๆ—ถ๏ผŒ็ณป็ปŸๅบ”่ฏฅๆ€Žไนˆๅ…œๅบ•๏ผŸ้˜ˆๅ€ผ่ฏฅๆ€Žไนˆๅฎš๏ผŸ
    Common in ChinaCommon overseasDeep dive#routing#fallback#reliability

    ๐Ÿ”’ Unlock this chapter to see the answer keys.

  • What is a field like routingReason actually worth in production? Is it just logging?routingReason ่ฟ™็ฑป่ฐƒ่ฏ•ไฟกๆฏๅœจ็”Ÿไบง็ณป็ปŸ้‡Œๆœ‰ไป€ไนˆไปทๅ€ผ๏ผŸๅชๆ˜ฏๆ‰“ๆ—ฅๅฟ—่€Œๅทฒๅ—๏ผŸ
    Common in ChinaCommon overseasIntermediate#observability#routing#debugging

    ๐Ÿ”’ Unlock this chapter to see the answer keys.

Comments