Memory and multi-agent systems · Chapter 9 of 16

Why many agents

By the end of this chapter you can say why one assistant that does every job gets slower, dearer and less accurate as it grows, measure it, and split it into specialists: agents with their own instructions, their own slice of the data and their own tools, each scoped to one job. 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

The memory is working. The assistant knows WD1042's budget, the mehndi date and who eats what, and it keeps them right when families change their minds. So Pooja wants more from it: suggest venues, check a caterer's Jain menu, compare decorators, tell a father what has been paid.

Karan did the obvious thing first. He put everything in one system message: the notes, all 22 vendors, Sameer's budget sheet and the whole policy. The first family to try it asked "What is the theme?" and got a sentence from the policy about who decides the theme.

Rekha was not surprised. "Baraat Bliss does not work like that either. I talk to the families, but the menu goes to the catering lead, the money goes to Sameer, and nobody reads the whole file to answer one question." Sameer had his own complaint: every question, even "thanks!", was paying for two thousand tokens of vendors and policy.

What you build: 5 tasks

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

  1. Task 1

    one_prompt.py: one prompt that holds everything

    Pooja wants to know what one assistant that does every job would have to read. Write everything(wedding_id) in one_prompt.py: it returns one system message holding an instruction to answer from the notes, then seven parts separated by blank lines, each a heading line and its lines: Notes: with notes(wedding_id); Venues:, Caterers:, Decorators: and Photographers: with vendor_lines of each type; Budget sheet: with sheet_lines(wedding_id); and Policy: with the whole of /workspace/data/policy.md. catalogue.py, beside it, has the helpers.

    Under if __name__ == "__main__":, count WD1042's message as the API counts a request that holds only it (baraat's tokens), and print it as the wedding id, a colon, a space, the count, tokens, a comma, a space, the share of lab-chat's 8192 tokens as a whole number with a percent sign, and of the window, for example WD1000: 1234 tokens, 15% of the window. Then ask lab-chat at temperature 0 When is the wedding? with that system message, and print the reply.

  2. Task 2

    tool_soup.py: twelve tools at once

    area_tools.py has twelve tools, three for each area (TOOLS, by area) and AREA_OF, the area of each tool's name. Take WD1042's requests from baraat's requests(). First send each request's text to lab-chat as one user message with all twelve tools; then again with only the three tools of the request's route.

    A request went to the right area when the first tool call's tool belongs to one of the request's needs; a reply with no tool call did not. Print two lines, all twelve first, then own three, each with a colon, a space, how many went to the right area, of, how many requests, to the right area, a comma, a space and the average of the replies' usage prompt_tokens rounded to a whole number, then prompt tokens a request, for example: all twelve: 20 of 25 to the right area, 900 prompt tokens a request

  3. Task 3

    specialist.py: a specialist

    Write a dataclass Specialist in specialist.py with the fields name, description, instructions, data (a function: data(wedding_id) returns the text of what the specialist may see) and tools (a list of tool schemas, empty by default), and a method answer(question, wedding_id).

    answer sends lab-chat at temperature 0 a system message made of the instructions, a blank line and data(wedding_id), then the question as a user message; it sends the tools only when there are some, and returns the reply's text.

    Under if __name__ == "__main__":, make a catering specialist whose data is Notes: on its own line and the wedding's caterer, diet:, allergy: and guests facts, a blank line, Caterers: and vendor_lines("caterer"), and print its answer to Is anyone allergic to peanuts? for WD1057.

  4. Task 4

    four.py: four specialists

    Write specialists() in four.py: it returns a dictionary of policy.md section 6's four specialists, venue, catering, decor and budget, each a Specialist with a description and instructions of its own. Each one's data for a wedding is Notes: and only its own facts from notes(wedding_id, keep), then its own data, each heading after a blank line: venue has the facts whose key ends in _venue or _date, and guests, then Venues: and the venue lines; catering has caterer, every diet: and allergy: fact, and guests, then Caterers: and the caterer lines; decor has decorator, theme and photographer, then Decorators: and Photographers: with theirs; budget has budget and guests, then Budget sheet: and sheet_lines(wedding_id), then policy_section(2).

    Under if __name__ == "__main__":, ask about WD1057 venue When is the sangeet?, catering What does Lakshmi Iyer eat?, decor Who is the photographer? and budget Who approves a payment above 2 lakh?, and print one line each, in that order: the name, a colon, a space and the answer.

  5. Task 5 · interview question

    Interview: fix_me.py, a specialist that answers anything

    The catering specialist in fix_me.py was told to answer anything the family asks. Run it: a parking question is answered from the catering notes.

    Scope it. answer(question, wedding_id) must still answer catering questions (menus, dishes, diets, allergies, caterers, tastings) from its notes, and return NOT_MINE, exactly, for anything else, without answering it from the notes. Keep the function's name and parameters, and NOT_MINE as it is. The check asks about parking, rooms, decorators, payments and allergies, for several weddings.

What you will learn

  • One prompt for everything: what it holds, what it costs, and why it answers worse.
  • One tool list for everything: twelve tools, and the wrong one wins.
  • What an agent is: instructions, data, tools and memory, scoped to one job.
  • Four specialists for venue, catering, decor and budget (policy.md, section 6).
  • Staying in scope: a specialist that knows what it does not handle.

More in Many agents

Build "Why many agents" 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.