LangChain and LangGraph · Chapter 20 of 25

Multi-agent

By the end of this chapter you can put a whole agent inside a graph as one node, let a supervisor send each message to the right specialist agent, let an agent hand the conversation to another with a tool, and weigh one agent with every tool against a team of small ones, with numbers. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Production
  • 4 tasks
  • Python
  • In the all labs plan

The first 2 chapters of LangChain and LangGraph are free. This one comes with the all labs plan.

Why you need this

RoomRaja's assistant now does a lot: rent, repairs, notice, deposits. Put all of it in one agent and its system prompt grows a paragraph for every job, and every request carries every tool. Tanvi runs her company the other way: Faisal handles money, Mrunal handles repairs, and the front desk knows whom to call.

Sid wants the assistant built like that: a rent specialist with the rent tool, a maintenance specialist with the ticket tools, a move-out specialist with notice and deposits, and something in front that decides who gets each message. Each specialist is an agent like chapter 15's. LangGraph gives you three pieces to join them: a graph can be a node of another graph, a node can choose the next node itself, and a tool can move the conversation to another agent.

What you build: 4 tasks

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

  1. Task 1

    subgraph.py: an agent inside a graph

    Make the maintenance agent with create_agent: lab-chat, the tools raise_ticket and ticket_status from roomraja_tools.py (given), the system prompt You are RoomRaja's maintenance specialist. Use your tools. and the name maintenance. Add it as the node maintenance of a StateGraph on MessagesState: START, maintenance, END, compiled as graph.

    Under if __name__ == "__main__":, stream the graph for the human message The tap in WAK-102 is leaking in the updates mode with subgraphs on. For each chunk print the path: for each part of the namespace, the text before its colon, then the name of the node that updated, all joined by a space, a greater-than sign and a space (maintenance > model). Then print Reply:, a space and the content of the agent's last message.

  2. Task 2

    supervisor.py: a supervisor

    Make three specialists with create_agent, each with lab-chat, its own tools from roomraja_tools.py, the system prompt You are RoomRaja's rent specialist. Use your tools. (maintenance specialist, move-out specialist for the others) and its name: rent with rent_due; maintenance with raise_ticket and ticket_status; move_out with give_notice and deposit_refund_estimate.

    A supervisor node asks lab-chat with the system message Route the tenant's message to one of: rent, maintenance, move_out. Reply with the label only. and the tenant's last message, and returns a Command that goes to that specialist, or to rent when the reply is none of them. MessagesState; START, supervisor; each specialist to END; compiled as graph.

    Under if __name__ == "__main__":, for Is there a late fee on my rent? I am T1004, The geyser in KOT-203 is not working and I am moving out next month, T1088, print the last message's name, a colon, a space and its content.

  3. Task 3

    handoff.py: a handoff

    Write three handoff tools, transfer_to_rent, transfer_to_maintenance and transfer_to_move_out, each described with what that specialist handles, with one argument, reason. Each returns a Command to the parent graph that goes to that specialist and updates messages with the messages so far plus a ToolMessage answering the call: Handed to, the specialist's name, a colon, a space and the reason.

    The front desk is create_agent with lab-chat, the three handoff tools, the system prompt You are RoomRaja's front desk. Hand every message to the right specialist. and the name front_desk; the specialists are task 2's. START, front_desk; each specialist to END; compiled as graph.

    Under if __name__ == "__main__":, for I am T1033, how much will I get back from my deposit? and The tap in HIN-102 is leaking, print the names of the AI messages' agents in order, each once, joined by a space, a hyphen, a greater-than sign and a space, then a colon, a space and the last message's content. The check sends another message too.

  4. Task 4 · interview question

    Interview: one_or_many.py, one agent or several?

    Send The fan in VMN-201 is not working to three things with lab-chat: one agent made with create_agent, every tool (roomraja_tools' ALL) and the system prompt You are RoomRaja's tenant assistant. Use your tools.; one request with task 2's routing system message; and task 2's maintenance specialist alone. For each, add up the prompt (input) tokens from the usage_metadata of its AI messages, and count those messages.

    Print three lines: One agent, 6 tools:, then the tokens, prompt tokens in, the count, requests; Supervisor:, the tokens, prompt tokens in 1 request; Specialist, 2 tools:, the tokens, prompt tokens in, the count, requests.

    The interviewer will ask: one agent or several? What does a team cost, what does it buy, and what goes wrong when the supervisor routes a message to the wrong specialist?

What you will learn

  • Subgraphs: a compiled graph, an agent included, as one node.
  • Streaming from inside a subgraph.
  • A supervisor: a node that returns Command(goto=...).
  • Handoffs: a tool that returns a Command to the parent graph.
  • One agent or several: what a team costs and what it buys.

More in Production

Build "Multi-agent" yourself

RoomRaja, a Pune co-living company, answers tenants on WhatsApp by hand. You build its assistant. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of LangChain and LangGraph are free. This one comes with the all labs plan.