Memory and multi-agent systems · Chapter 15 of 16
Capstone
By the end of this chapter you have built Baraat Bliss's planning assistant from the parts of the whole lab: a memory that follows the policy, screens what it must never keep and forgets on request; a front desk and four specialists that hand over, answer together and stop loops; and an assistant that takes every message, remembers or answers, and logs the turn. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Many agents
- 4 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 first assistant forgot the bride's mother was Jain, quoted January's budget after the March cut and gave a cousin the old mehndi date. Pooja wants the new one on the threads before the December weddings, and she has one condition: "Show me it does the right thing on the conversations that went wrong." Rekha wrote those down as five scenarios. Sameer added his: no PAN, no card number, no health detail anywhere, not even in a log.
Every piece exists already, in its own chapter. A capstone is where they meet, and where the gaps between them show.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
memory.py: the memory
Write accept and the class Memory in memory.py. Memory(path) creates, if missing, facts(wedding_id, key, value, said_by, said_at, accepted, why), one row per value ever said, value as JSON. add(wedding_id, key, value, by, at, tentative=False) decides with accept (tentative never; budget only by the wedding's decides budget; guests by its decides guests, the couple or the planner; else anyone), stores the row and returns a dict with key, value, accepted and why. seed(wedding_id, messages) adds every labelled fact of the messages, with no model call. remember(wedding_id, sender, text, at) returns None, storing nothing, when kit's redact would change the text or kit's sensitive says True, checked in that order; otherwise it adds every fact that baraat's fact_keys finds in kit's extract fields, tentative from the fields, and returns what add returned.
current(wedding_id) is each key's newest accepted value by said_at; pending(wedding_id) the refused rows said after their key's current value, oldest first, each with key, value, said_by and why; forget(wedding_id, name) deletes the facts whose key ends with a colon and the name, and returns how many; notes(wedding_id) is a dash, a space, the key, a colon, a space and the value per current fact, sorted by key, one per line.
- Task 2
team.py: the team
Write Team(max_hops=3, max_tokens=4000) with answer(wedding_id, text, facts) in team.py. Route the text with kit's route; planner means a planner takes it. The agent gets a request to lab-chat at temperature 0 with kit's system(agent, wedding_id, facts), the text, and kit's transfer_tools(agent); add its usage total_tokens to the tokens spent. No tool call is the answer. A transfer call when more than max_tokens are spent or max_hops handovers already happened goes to a planner. A transfer to an agent already on the path is a ping-pong: ask every agent on the path at the same time, without tools, with the same system message and text, and merge the answers with kit's merge in the path's order. Otherwise the target joins the path and gets the text.
answer returns a dict: path, the agents reached and then planner if a planner took it; by, who answered (the agent, the agents merged, or planner alone); reply, the reply (PLANNER for a planner).
- Task 3
assistant.py: the assistant
Write Assistant(path="baraat.db", team=None) in assistant.py: a Memory on that file, a Team (a new one when none is given), and the table log(wedding_id, at, sender, text, reply, answered_by) in the same database. handle(wedding_id, sender, text, at) returns the reply. A text with the word delete or forget, in any case, is a deletion request: forget the facts about the sender, delete the sender's earlier log rows, and reply Done, the sender, a colon, a space, we deleted your messages and the facts about you, and the number of facts in brackets, with a full stop. A text ending with a question mark gets the team's reply, with the memory's current facts.
Anything else is remembered: None gives Thank you. For your privacy we do not keep that.; no facts gives Thank you, noted.; otherwise one line per fact, Noted: key is value. when accepted, Not changed: key stays the current value or unset, then why in brackets and a full stop. Every turn is then logged with kit's redact of the text, the reply, and the team's by joined with a comma and a space, or memory.
- Task 4
scenarios.py: five scenarios
Play five scenarios through Assistant, each on a fresh database seeded with the wedding's thread up to, not including, its message, the turns at that message's at (else 2026-10-22T10:00:00+05:30), and print each as ## and the title and the wedding id in brackets, then for every turn the sender, a colon, a space and the text, and each line of the reply indented by two spaces.
1 A diet change, WD1057: WD1057-m045 from its sender, then Meera Iyer: Is Lakshmi Iyer vegetarian? 2 A budget request that is not accepted, WD1057: WD1057-m051 from its sender, then Meera Iyer: What is the budget? 3 A deletion request, WD1042 up to WD1042-m276: Kavya Sharma: I am vegetarian, by the way., then WD1042-m137's text from her, then WD1042-m276. 4 A question for two specialists, WD1057, the whole thread: Meera Iyer asks R131's text. 5 A loop, with a hop limit of 1, WD1042, the whole thread: Ananya Sharma asks R045's text, to a team with max_hops=1.
What you will learn
- A memory layer behind one class: history, authority, screening, forgetting.
- A team that routes, hands over, merges a ping-pong and stops at its limits.
- An assistant that decides what each message is and logs every turn.
- Scenarios as the test of a whole system: read the transcript like Rekha.
More in Many agents
Build "Capstone" 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.