Skip to content
Forward Deployed

Forward Deployed
T10 exercise

Exercise: memory that survives a new thread

Track
The Memory Layer
By
Sourav Banerjee
Last updated
30 Nov 2026

Report an issue

01 Context

Problem statement

Dana runs Fernhill Ceramics on Larkspur Cloud. On Monday she tells the support assistant five facts about her account. On Friday she opens a new chat and asks about them. You give the assistant cross-session memory and check, in code, what it recalls in the new thread and what it leaks to another customer.

The rule you apply comes from lessons 1 and 3 of T10.2. The checkpointer remembers one thread, and only the store brings a fact back in a new one. A store can only return what someone wrote, so check the writes before you blame the read. The namespace is built from the trusted user_id and is the only wall between customers.

02 Outcomes

What you will learn

  • Run the same agent with a checkpointer alone and with a checkpointer plus a SqliteStore, both in SQLite files that survive a restart.
  • Let the model write facts through a tool whose user_id comes from the run's context, never from a tool argument.
  • Score recall in a new thread, and split each miss into a write failure or a read failure.
  • Check that a second user's new thread finds none of Dana's facts.
  • Change a fact and check that the key was reused, not duplicated.

03 Setup

Before you start

Lessons

Dataset

Six messages from Dana and five questions about her facts, in data/. Five messages state one fact each in her first thread. The sixth changes her plan, for the extension.

Code

langgraph (StateGraph, ToolNode, tools_condition, SqliteSaver, SqliteStore), ToolRuntime and tool from langchain.tools, and ChatOllama from langchain_ollama. Nothing from src/ is needed.

Environment

The course's default environment (uv run jupyter lab), or Colab after cloning the repository. Everything runs offline: the chat model is qwen2.5:3b through a local Ollama server (ollama pull qwen2.5:3b), and no API key is needed. The SQLite files go to a temporary folder that the notebook deletes at the end.

04 Architecture

Experiment workflow

The model, the five messages, the five questions and the scoring stay fixed. Only the memory configuration changes between rows.

Diagram: Load Dana's messages and the questions; Agent with SqliteSaver only; Agent with SqliteSaver and SqliteStore; Thread 1: Dana states five facts; Thread 1: Dana states five facts, the model saves them; Reopen both SQLite files; Read Dana's namespace back in code; Ask the five questions, each in a new thread; Ask the same questions as Omar; Recalls, write or read failures, leaksDiagram: Load Dana's messages and the questions; Agent with SqliteSaver only; Agent with SqliteSaver and SqliteStore; Thread 1: Dana states five facts; Thread 1: Dana states five facts, the model saves them; Reopen both SQLite files; Read Dana's namespace back in code; Ask the five questions, each in a new thread; Ask the same questions as Omar; Recalls, write or read failures, leaks

Extension: Dana changes her plan in a third thread, and a fourth thread asks about it.

05 Build

Implementation tasks

  1. Task 1: Load the data

    Load messages.jsonl and questions.jsonl from data/. Keep the five first messages apart from the update message.

  2. Task 2: Build one agent with two memory switches

    Build lesson 3 of T10.2's graph with only the semantic layer: an agent node, a tools node and tools_condition. Declare a Context dataclass with a user_id field as the context schema. With the store switched on, the agent node preloads the user's facts with their keys into the system prompt, and the model holds one tool. With it off, the graph has no tool and no preload.

    @tool
    def save_user_fact(key: str, fact: str, runtime: ToolRuntime[Context]) -> str:
        runtime.store.put(("semantic", runtime.context.user_id), key, {"fact": fact})
    
    store.search(("semantic", user_id), limit=100)
    

    Cap every run with "recursion_limit": 8 in the config.

    New here: the recursion limit comes from T7.1 in T7, where it stops a looping agent.

  3. Task 3: Thread 1: Dana states her facts

    Give each configuration its own SQLite file, with SqliteSaver and SqliteStore on the same file. Send Dana's five messages to thread dana-1 with user_id="dana" in the context. Print every save_user_fact call.

  4. Task 4: Restart and read the store back

    Open both files again with fresh connections, so only what is on disk can help. Read Dana's namespace in code and note which questions have their answer on disk.

  5. Task 5: Ask in new threads

    Ask each question in its own new thread, for each configuration. A reply recalls a fact when it contains the expected word. Add a control row: the checkpointer-only agent asked back in thread dana-1. Then ask the same questions as user_id="omar" with the store switched on, and count a leak when any of Dana's expected words appears.

  6. Task 6: Diagnose the misses

    For each store miss, say whether the answer was on disk. Not on disk is a write failure, and on disk is a read failure. Also list the save calls the model made while answering questions, and what landed in Omar's namespace.

  7. Task 7: Extension: change a fact

    Send the update message to thread dana-3. Count Dana's facts before and after, print the plan fact, then ask the plan question in a fourth thread.

06 Measure

Record results

What you enter stays in this browser only.

ConfigurationFacts on disk after thread 1Recalled in a new thread (of 5)Misses: write or read failureDana's facts leaked
Checkpointer only, same thread (control)nonenonenot run
Checkpointer only, new threadnonenot run
Checkpointer plus store, new threadnot run
Same store, Omar's new threadnot scorednot scored

A recall is a reply that contains the expected word. A write failure is a miss whose fact the model never saved. A read failure is a miss whose fact was on disk.

07 Verify

Done when

0 of 4 done

What you enter stays in this browser only.

08 Debug

Common mistakes

Blaming retrieval for a fact never written

A store miss is often a forgotten write (lesson 3 of T10.2). Read the namespace back in code before you change the prompt or the preload.

Testing recall in the same thread

The checkpointer replays the thread, so the same-thread row passes without any store. Only a new thread_id tests cross-session memory.

Putting `user_id` in the tool schema

Then the model can name another customer and read her memories. Pass it through the run's context and ToolRuntime (lesson 1 of T10.2).

A preload with no limit

search returns 10 items unless you pass limit=, and the 11th fact disappears without an error.

Keeping the database in the data folder

The solution must never write under data/. Use a temporary folder, and delete it so a rerun starts empty.

09 Transfer

Interview angle

“Your agent forgets what a customer told it yesterday. How do you add memory, and how do you test it?”

Show a model answer

Keep the checkpointer for the thread and add a store namespaced by the authenticated user id. Test it by writing in one thread, restarting, and asking in a new thread. Read the store back to separate write failures from read failures, and open a second user's thread to assert nothing leaks.

10 Review

Worked solution

The worked solution is solution/p09_exercise_solution.ipynb (open in Colab). Locally, open it from exercise/solution/ so ../data/ resolves, and run it top to bottom with the default environment and Ollama running. In Colab, clone the repository first and install Ollama, because the notebook reads its data from the repository and calls a local model.

Show our run and the reasoning

Our run: qwen2.5:3b through Ollama at temperature 0, langgraph 1.2.18, langgraph-checkpoint-sqlite 3.1.2, langchain 1.4.7 and langchain-ollama 1.1.0, offline.

Configuration Facts on disk after thread 1 Recalled in a new thread (of 5) Misses: write or read failure Dana's facts leaked
Checkpointer only, same thread (control) none 5 none not run
Checkpointer only, new thread none 0 5 misses, no store to write to not run
Checkpointer plus store, new thread 4 keys, holding 3 of the 5 answers 3 q1 and q3, both write failures not run
Same store, Omar's new thread none for Omar not scored not scored 0

The control row recalls all five facts, so the facts were clear and the model could use them. Opening a new thread with the checkpointer alone drops all five: the thread dana-1 still holds 15 checkpoints on disk, but nothing reads them from dana-2.

With the store, the new thread recalls the plan, the invoice email and the database name. Both misses are write failures. In thread 1 the model saved [name] Dana for "I run a small online shop called Fernhill Ceramics", so the shop name never reached the store. It made no save call at all for "All our servers are in the Dublin region." When asked q1 and q3 later, the agent said it did not know, which is the right answer for an empty fact. No miss had its answer on disk, so the preload and the read are not where to look. An earlier run of the same cells gave the same saves and the same 3 of 5.

Omar's new thread leaked none of Dana's facts, because his namespace is ("semantic", "omar"), built from the context, and the tool has no user_id argument. His row shows a second problem. The model called save_user_fact 4 times while answering his questions, 3 of them with a malformed fact that the tool's schema rejected. One landed: [plan] unspecified is now in Omar's namespace, a guess written as a fact. It also answered q3 with an invented "US East region". Dana's thread made 3 save calls while answering, all rewriting facts she already had.

The extension behaved well. "We have moved from the Business plan to the Enterprise plan" reused the key plan, the count stayed at 4, and a fresh thread answered "We are on the Enterprise plan."

The decision is to change who writes, not how memory is read. Lesson 3 of T10.2 moves a write out of the model's hands when the model forgets it. Here that means a writer node after each user turn that saves facts from the user's own words, and no save tool bound while the agent is answering a question.