Memory and multi-agent systems · Chapter 8 of 16

Episodic memory

By the end of this chapter you can store events as episodes (when, who, what was decided), find them by time and by meaning together, and answer "what did we decide?" from the right meeting. 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

The fact store from chapters 4 to 6 answers "what is the budget?". It cannot answer the questions Rekha gets every week:

  • Sameer: "What did we decide about Meera and Kabir's caterer in June?"
  • Pooja: "Why are we proposing Sawai Haveli? Who decided that, and when?"
  • A cousin: "Didn't we already talk about the mehndi venue?"

These are questions about events: a planning meeting, a day of messages, a decision and the discussion around it. Baraat Bliss has seven planning meetings in /workspace/data/meetings.jsonl, each with a date, who was there, every line said, the decisions and the action items, and months of client sessions in the threads. A fact keeps only the latest value. An event keeps what happened, and that is a different kind of memory.

What you build: 5 tasks

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

  1. Task 1

    episodes.py: meetings as episodes

    Write the class Episodes in episodes.py. Episodes(path) creates, if missing, the SQLite table episodes(id, kind, wedding_id, date, title, attendees, decisions, vector), id the primary key. add(episodes) takes episode dicts (those keys but vector; attendees and decisions are lists), embeds them all with lab-embed in one request, each one's text being its title then its decisions, one per line, and stores each (attendees and decisions as JSON, the vector as float32 bytes), replacing a row with the same id. get(episode_id) returns the episode as a dict without the vector, or None.

    Under if __name__ == "__main__":, add every meeting (baraat's meetings) to episodes.db as an episode of kind meeting, then print one line per meeting, like MT09 2026-07-01 WD1000: Its title (2 decisions), with team instead of the wedding for the team's meeting.

  2. Task 2

    sessions.py: past sessions as episodes

    Write session_episode(wedding_id, number, messages) in sessions.py for one session's thread messages. It returns a dict: id the wedding id, -s and the number (WD1000-s4); kind session; wedding_id; date the day of its first message; title the wedding id, session and the number (WD1000 session 4); attendees who wrote, each once, in the order they first wrote; decisions every fact its messages' facts labels state, in order, as key, a colon, a space and value.

    Under if __name__ == "__main__":, add every session of every wedding (weddings.json's order) to episodes.db with task 1's Episodes, and print one line per wedding like WD1000: 6 sessions, 21 facts stated.

  3. Task 3

    find.py: find an episode by time and by meaning

    Write find(store, wedding_id, query, start=None, end=None, k=3, kind=None) in find.py, store being task 1's Episodes. Keep the episodes of the wedding or of the team (no wedding), whose date is between start and end (ISO days, both included; None means no limit on that side), and of that kind when kind is given; rank them by cosine similarity to the query embedded with lab-embed, and return the best k, most similar first, each a dict with id, kind, date, title and score.

    Under if __name__ == "__main__":, search WD1042's episodes for What did we decide about the guest count? at any time and in September 2026, and print the results.

  4. Task 4

    recall_meeting.py: recall what a meeting decided

    Write month_range(month, year) in recall_meeting.py: the first and last day of a month as ISO days, the month's name in any case. Write recall(wedding_id, question, month, year): find (task 3) in episodes.db the one meeting of that month most similar to the question, then ask lab-chat at temperature 0 with the system message You are Baraat Bliss's planning assistant. Answer from these notes. then a blank line, Notes: on its own line, and the meeting's notes: one line per decision, a dash, a space, We decided: and the decision with a full stop, then one line per transcript line, the speaker, a colon, a space and the text; the question is the user message. recall returns the meeting's id and the reply.

    Under if __name__ == "__main__":, recall for WD1042 What did we decide about the mehndi in August? (August 2026) and print the meeting's id, then the reply.

  5. Task 5 · interview question

    Interview: fix_me.py, a month filter that finds nothing

    in_month(episodes, month) in fix_me.py should keep the episodes whose ISO date falls in a month given as a number from 1 to 12. Run it: August has two meetings, and it finds none.

    Fix in_month so that it keeps exactly the episodes of that month, in their order. Keep the function's name and parameters. The check tries other months, including March and October.

What you will learn

  • Episodic memory: events stored with their time, people and outcome.
  • Episodes from meetings and from sessions, in one table.
  • Finding by time and by meaning: filter first, then rank.
  • Answering from an episode: the decisions first, then the discussion.

More in Memory

Build "Episodic memory" 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.