Memory and multi-agent systems · Chapter 4 of 16

Long-term facts

By the end of this chapter you can have a model read a message and fill a fixed set of fields with structured output, map those fields to fact keys, keep the facts in SQLite with who said them and when, and read them back into a request as notes. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Memory
  • 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 2 pinned the messages that state facts, and the request filled up with old values next to new ones. Rekha does not work that way. After every call with a family she updates one line of her fact sheet: the walima is now on the 25th, not "the walima was on the 24th and then on the 25th". Her sheet for Fatima and Imran's wedding is eleven lines.

Pooja wants the assistant to keep that sheet itself, from the threads, so Rekha stops typing it. And Karan has a reason of his own: the night assistant restarts every time he deploys it, and a history in a Python list does not survive a restart. Facts that must last a ten-month wedding belong in a database.

What you build: 5 tasks

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

  1. Task 1

    extract.py: one message, its fields

    Write a Pydantic model Fields in extract.py with the fields wedding_id, person, diet, allergy, event, date, venue, caterer, decorator, photographer and theme (each a string or None), budget and guests (each an int or None), tentative (a bool) and importance (an int).

    Then write extract(message): message is one thread message, a dict with sender, text and at. Ask lab-chat with structured output, response_format=Fields, with two messages: a system message holding an instruction to extract the facts the message states, followed by Today is and the message's date (the first 10 characters of its at) and a full stop; and a user message with the sender, a colon, a space and the text. Return the Fields object the model filled. The check calls extract with messages of another wedding, one of them without a year in its date.

  2. Task 2

    facts_of.py: from fields to facts

    Write facts_of(fields) in facts_of.py: fields is a dict of one message's fields (what model_dump gives for a Fields object). Return the facts they state as a list of (key, value) pairs, in this order: budget when it is not None; guests when it is not None; the event, then _date, when there is both an event and a date; the event (or wedding when there is none), then _venue, when there is a venue; diet:, then the person, when there is a person and a diet; allergy:, then the person, when there is a person and an allergy; then caterer, decorator, photographer and theme, each when it is there.

    Under if __name__ == "__main__":, extract every message of WD1063 with task 1's extract, print each message's id and its facts for the messages that state some, and a last line: the number of messages whose facts equal their facts label (as key and value pairs), of, the number of messages, then messages agree with the labels, for example 9 of 10 messages agree with the labels.

  3. Task 3

    store.py: facts that last

    Write a class FactStore in store.py. FactStore(path) opens or creates a SQLite file with a table facts with the columns wedding_id, key, value, said_by, said_at and message_id, one row per wedding and key. put(wedding_id, key, value, said_by, said_at, message_id) saves a fact, replacing the row of the same wedding and key; the value is stored as JSON text. get(wedding_id, key) returns the value (None when there is none), and all(wedding_id) returns that wedding's facts as a dictionary of key and value.

    The check puts facts of two weddings, puts a new value for a key, opens the file again with a new FactStore, and reads the table with sqlite3 itself.

  4. Task 4

    ingest.py: a wedding's thread into memory

    In ingest.py, extract every message of WD1063 in order (task 1), turn its fields into facts (task 2), and put each fact into a FactStore on memory.db (task 3), with the message's wedding id, sender, at and id as wedding_id, said_by, said_at and message_id. Then print the store's facts for WD1063, one per line, sorted by key, as the key, a colon, a space and the value.

    The check starts from a fresh memory.db and compares the rows with Rekha's fact sheet for WD1063 (facts.json): the values, who said each, when and in which message.

  5. Task 5 · interview question

    Interview: fix_me.py, a memory that forgets on restart

    FactStore in fix_me.py saves facts in SQLite, but they are gone every time the program starts again. Run python3 fix_me.py twice: the second run should print remembered: 2027-01-25, the walima date the first run saved.

    Fix FactStore so that what put saves is still there for a new FactStore on the same file, in the same program or a new one. Keep the class's name and its methods.

What you will learn

  • Short-term and long-term memory: what is resent, and what is stored.
  • Structured output: a model filling a schema you define.
  • Today's date in the request, so "22 Jan" gets the right year.
  • Fact keys: one canonical name per fact.
  • Facts in SQLite: an upsert per key, values as JSON, with provenance.

More in Memory

Build "Long-term facts" 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.