Your returns agent does one job well: its plan, its tools and its rules are all about returns. But customers write about everything, and a question like "Do you ship to Canada?" needs no plan and no tools. A handoff solves this: one small call, the router, reads the message and decides who handles it. Then your code calls that specialist.
The router only chooses. It doesn't answer the customer. It replies with a small JSON object, because the reader of its answer is an if in your code, not a person. A router for a shop with a shipping team could work like this:
"Where is my parcel?" → {"agent": "shipping"}
"Do you sell gift cards?" → {"agent": "general"}
Each one gets only the prompt and the tools its job needs. The router chooses. The general assistant answers without tools, so it can't change anything. Only the returns agent can touch an order.
For the three messages in MESSAGES, your program must print the route and then the work of whoever got the message:
Message: I want to return the lamp, my email is ana@example.com. Route: returns Plan: 1. Find the customer's orders by email … Stopped: the agent called finish Steps: 6 Cost: 0.0092 Message: Do you ship to Canada? Route: general Answer: Yes, we ship to Canada. Delivery takes 7 to 10 business days. Message: How many days do I have to return a product? Route: general Answer: You have 30 days from the day you ordered. To start a return, write to us with your email address and the product.
After your run, the check sends one more message, about a double charge. The router answers with billing, a name nobody offered it. Your code must send that message to the assistant without tools, so order 5 stays as it is.
Your task: ROUTER, GENERAL, MESSAGES, ask_plain(system, message) and run_agent are ready in the file. Write route(message), which asks the router and returns the name under "agent". Write handle(message), which prints the route, runs the returns agent for returns and prints the general assistant's answer for everything else. Then loop over MESSAGES.
Assignment
Follow the Coding Tutorial to build the router, the handoff to the returns agent and the general assistant's answer.
Hint
The router's reply is text that holds JSON: turn it into a dictionary with json.loads before you take the name. Use else for the general assistant, not a second comparison, so a route you didn't expect still gets a safe answer.
Free plan: 30 tutor messages. PRO is unlimited.
