Memory and multi-agent systems · Chapter 10 of 16

Routers

By the end of this chapter you can put a front desk in front of your specialists: a router that reads a client's message and picks the one who should answer, measured with accuracy and a confusion matrix, made safe with a schema and a way out to a person, and wired to the specialists so each question comes back answered. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Many agents
  • 5 tasks
  • Python
  • In the all labs plan

The first 2 chapters of Memory and multi-agent systems are free. This one comes with the all labs plan.

Why you need this

Chapter 9 split the assistant into four specialists. Now somebody has to decide who answers. At Baraat Bliss that somebody is Rekha: every WhatsApp message lands with her first, and she forwards the menu question to catering and the payment question to Sameer. She cannot read four weddings' messages at night.

Karan wants a front desk that does what Rekha does: read, decide, forward. Sameer wants a number before it goes live: "how often does it send a payment question to the decor people?" And Pooja wants a rule for the messages nobody planned for: "if it does not know, a planner answers. Never a guess."

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    route.py: a router

    Rekha wants every client message to reach the right specialist. Write route(text) in route.py: it asks lab-chat at temperature 0 with a system message holding exactly

    Route the client's message to one of: venue, catering, decor, budget.

    and the text as the user message, and returns the label the model chose, without spaces or a full stop around it, in lower case; when the reply is none of the four labels, it returns planner.

    Under if __name__ == "__main__":, route the first eight requests of baraat's requests() and print one line each: the request's id, a space and the label, for example R100 venue

  2. Task 2

    evaluate.py: how good is the router?

    Route every request of baraat's requests() with task 1's route. A request is routed right when the label is one of its needs. Print accuracy, a colon, a space, how many were right, of, how many requests, and the percentage with one decimal in brackets, for example accuracy: 90 of 100 (90.0%)

    Then a confusion matrix: a header line, then one line per true specialist, venue, catering, decor and budget in that order, each its name and five counts separated by spaces, how many of its requests were routed to venue, catering, decor, budget and planner. A request routed right counts on its label's line and column; one routed wrong counts on its route's line.

    Last, one line per request routed wrong, in the file's order: missed, a space, the id, a colon, routed to and the label, a comma, needs and its needs joined by and, for example missed R100: routed to decor, needs venue and budget

  3. Task 3

    structured.py: routing with a schema, and a way out

    Write a Pydantic model Route in structured.py with specialist, a Literal of venue, catering, decor, budget and planner, and reason, a string. Then route(text, model="lab-chat"): it asks that model at temperature 0 with client.beta.chat.completions.parse and Route as the response format, with a system message of your own saying to route the message, or to a planner when no specialist fits, and returns the parsed Route.

    It returns a Route to planner without asking the model when the text is empty or only spaces, and, with a reason of your own, when the reply cannot be read or parsed. Under if __name__ == "__main__":, route Can the mandap have marigold strings and diyas?, What time is it in London? and three spaces, and print each as the specialist, a colon, a space and the reason. The check also routes with lab-chat-sloppy, which wraps its JSON in a code fence.

  4. Task 4

    front_desk.py: the front desk

    specialists.py has chapter 9's four specialists: specialists() returns them by name, each with answer(question, wedding_id). Write front_desk(text, wedding_id) in front_desk.py: route the text with task 3's route, then ask that specialist and return a pair, the specialist's name and its answer; for planner, return planner and PLANNER without asking a specialist. Build the specialists once.

    Under if __name__ == "__main__":, send Is the chicken halal?, Is the venue cost inside our budget? and What time is it in London? for WD1071, and print each as who answered, a colon, a space and the answer.

  5. Task 5 · interview question

    Interview: fix_me.py, a router that sends everything to a planner

    route(text) in fix_me.py should send each client message to venue, catering, decor or budget. Run it: every request goes to a planner.

    Fix route so that it returns the specialist the model chose, whatever case, spaces or full stop its reply comes with, and planner only when the reply is none of the four. Keep the function's name, the client and the instructions. The check also gives route replies such as a capitalised label with a full stop and a newline.

What you will learn

  • A router is a classifier: the labels are your specialists.
  • Measuring a router: accuracy, and a confusion matrix that shows which mistakes it makes.
  • Structured routing: a schema with a Literal, and a fifth label for "none of these".
  • The front desk: route, then hand the question to that specialist.
  • Comparing labels: the reply is text, and text has case and full stops.

More in Many agents

Build "Routers" yourself

Baraat Bliss, a Jaipur wedding planner, must remember months of family decisions across many agents. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Memory and multi-agent systems are free. This one comes with the all labs plan.