Courses / Agentic AI from Scratch (Python)

Session 27 of 40

Agent design patterns, and long-term memory in a store

Notes

How Do You Add a Self-Check and Long-Term Memory to a LangGraph Agent?

Agentic AI Foundations · Session 27 — Agent design patterns, and long-term memory in a store

Before you start: what do I need before I add a self-check and a memory store?

You need the code folder for this session open in your terminal. It holds memory_graph.py, the prompts in model_prompts/ (check.md, remember.md, fallback.md and the carried-over ones), the sample data in data/ and one build prompt in prompts/. Install the libraries with pip install langchain langchain-openai langchain-chroma langchain-community langchain-text-splitters langgraph langgraph-checkpoint-sqlite, set LLM_BASE_URL, LLM_API_KEY, LLM_MODEL and EMBED_MODEL as in earlier sessions, then build the policy index and the ticket database with python build_index.py and python make_tickets_db.py. Recap of what this session assumes: the Copilot is a routed graph with a policy path, a ticket path and a fallback (sessions 23 and 24), saved by a SQLite checkpointer with one thread id per conversation (session 25), with a person’s approval before a ticket is written (session 26). Have paper or a notes app ready for the Flashcards and the Model answer.

Summary: what are agent design patterns and long-term memory?

A fluent answer can still be wrong, so the Copilot gets a self-check: after a policy answer, a second model call tests whether the policy text supports it, the graph retries once, and then it gives up with a plain message instead of looping. A self-check is one of several agent design patterns, beside a router, a planner and executor, and a supervisor, and the advice is to choose the simplest one that works. A checkpointer remembers one conversation, so the Copilot also gets a store: a place outside any thread where a fact about a person is filed under a namespace and read back in any later thread. The store used here, InMemoryStore, forgets everything when the program ends. Together the check and the store finish the last two requirements of Milestone 2.

Key concepts: self-check and long-term memory terms explained in plain English

Term Definition
Reflection A step after an answer in which a second model call checks the answer, and the writer retries if the check fails.
Self-check The Copilot’s form of reflection: a node that asks whether the retrieved policy text supports every fact in the answer.
check_node The graph node that sends the policy text and the answer to a model and records a typed verdict, supported and reason.
Retry limit The cap on how often a failed check sends the graph back; here one retry, then give_up_node.
Router A pattern that classifies an input and sends it to a specialist path.
Planner and executor A pattern in which one step breaks a task into subtasks and other steps carry them out; the LangGraph docs call it orchestrator-worker.
Supervisor A pattern in which a main agent coordinates other agents as tools.
Store A place outside any one conversation where a graph keeps facts, readable from any thread.
Namespace The folder-like tuple a memory is filed under, such as (user_id, "memories").
InMemoryStore A store that keeps its facts in the running program only; they are lost when it ends.

Explanations: how do a self-check and a memory store work in the Copilot?

Why does the Copilot need a self-check?

Failure first. An employee asks how many unused leave days can be carried over, and the Copilot replies, in an illustrative example: “Employees may carry over 30 unused leave days. (source: leave-policy.md)”. It looks sourced and confident, yet the policy says nothing of the kind. The model wrote a plausible number, the same fluent-but-made-up behaviour you met in session 3, and nothing compared it with the policy text before it went out.

Think of a proofreader who reads the draft beside the source page before the post goes out, or a cashier who counts the change a second time. The second look is not proof, but it catches many slips. That is a self-check, also called reflection: a step after the answer in which a second model call asks whether the sources support it. The LangGraph docs describe the same shape as an evaluator-optimizer workflow, where one model call creates a response and another evaluates it, and the loop continues until an acceptable response is generated.

How does check_node work, and why only one retry?

check_node in memory_graph.py sends two things to a model: the policy text the answer used (kept in the state as context) and the answer. The model replies in a typed shape, so the graph can branch on it:

class Verdict(BaseModel):
    supported: bool
    reason: str

The prompt in model_prompts/check.md says supported is true only when every fact in the answer, such as a number, a name or a rule, appears in the context. If the policy helper already said it does not know (needs_human), there is nothing to check and the node passes the answer on.

Then route_after_check decides where to go:

def route_after_check(state):
    if state["supported"]:
        return "save_turn"
    return "policy_node" if state["attempts"] < 2 else "give_up_node"

A supported answer is saved. An unsupported one goes back to policy_node for one more try, and the attempt counter in the state counts it. A second failure goes to give_up_node, which replaces the answer with “I can’t confirm that answer from the policies. Please raise it with a person.” Picture a teacher who sends work back once and then calls the parent: a check that can fail needs a limit, or a stubborn question would loop for ever and every trip round the loop would cost more model calls.

Be honest about what the check is. It is another model, so it reduces bad answers but is not proof. A policy question now costs a few extra chat calls: the check, and a retry when the check fails.

For the demo only, the state has a switch called bad_attempts. While it is at least the attempt number, policy_node pretends the model made something up (the made-up 30-day sentence), so you can watch the check catch it. Real runs leave it at 0.

Which other agent design patterns exist, and which does the Copilot use?

Two everyday pictures: a post room that sorts each parcel to the right desk, and a project lead who splits a job into tasks and hands them out. Agent design patterns are named shapes like these. The LangGraph docs describe routing workflows as processing an input and then directing it to a context-specific task, and orchestrator-worker setups as an orchestrator that breaks a task into subtasks, delegates them to workers and synthesizes their outputs, often used when the subtasks cannot be predefined. In the subagents pattern a main agent coordinates subagents as tools.

Pattern What it does Where the Copilot uses it
Router Classifies an input and sends it to a specialist path sort and route_by_kind
Reflection A second call checks the answer; retry if it fails check_node
Planner and executor Breaks a task into subtasks; workers carry them out Not used yet
Supervisor A main agent coordinates other agents as tools Sessions 29 onward

The Copilot is a router with a self-check. The same docs add that a single agent with the right tools and prompt can often achieve results similar to a multi-agent system, so start with the simplest pattern that works and add another only when a real problem needs it.

What is a memory store, and how does it differ from the checkpointer?

Failure first. A user writes, “I work part time in the Berlin office.” A day later they open a new chat and ask, “Which office do I work in?” The Copilot has no idea. The first message sat in the state of one thread, and a new thread starts empty.

Picture a filing cabinet with a drawer for each person. Any desk can open Priya’s drawer and read what is in it, whichever chat is open. That is a store. Another picture: a contact card, written once and read in later chats. The LangGraph docs describe a store as the way for an agent to carry things such as a user’s preferences past the end of one conversation. Each memory is filed under a namespace, similar to a folder, and a key, like a file name.

Question Checkpointer Store
What does it keep? The state of one thread Facts that belong to a person
Who can read it? The same thread id Any thread, using the same user id
In memory_graph.py SqliteSaver on copilot_checkpoints.db InMemoryStore()
After a restart The file survives InMemoryStore is lost

The docs add that most applications use both: a checkpointer tracks the current thread and a store tracks durable information across threads, and you compile the graph with both. The Copilot does exactly that.

How do remember_fact and fallback_node use the store?

remember_fact runs on each message, right after receive. It asks a model, with model_prompts/remember.md, to pick out a lasting fact about the person (an office, a team, a working pattern) as one short sentence, or an empty string when there is none. A question or small talk gives nothing. A real fact is saved:

store.put((user_id, "memories"), str(uuid.uuid4()),
          {"fact": memory.fact.strip()})

The namespace is (user_id, "memories"); the key is a new unique id, so each fact is its own entry; the value is a dictionary. A node gets the store by naming it as a parameter, and gets the user id from the config: config["configurable"]["user_id"]. The config holds two ids with two jobs. user_id says whose memories to use. thread_id says which conversation to continue.

On the other side, fallback_node reads the facts back:

facts = [item.value["fact"]
         for item in store.search((user_id, "memories"))]

It fills them into model_prompts/fallback.md. The prompt tells the model to note a fact the person states, to answer from the saved facts when they answer the question, and otherwise to say it cannot help. With no facts it says “(nothing saved yet)”. Because the lookup is keyed by the user id, a fact saved in thread chat-1 is found in thread chat-2, while a different user has an empty drawer.

The store here lists a person’s facts. The docs also describe a store that finds memories by meaning when it is configured with an embedding model; this lesson’s code does not set that up.

What happens to the store when the program stops?

InMemoryStore keeps facts in the running program only. The docs call it suitable for development and testing, and recommend a persistent store for production. Run the program again and the drawer is empty, even though copilot_checkpoints.db still holds each thread’s saved state. A store that writes to a file or a database is used the same way, with no change to the nodes: you swap the object passed to compile.

Memory about a person also raises a privacy question. Before you keep facts, decide which kinds are worth keeping, and keep the namespace per user so one person’s facts stay out of another’s answers.

What goes wrong without these two parts, and what does it mean for the design?

Three situations you will meet:

  1. The Copilot quotes 30 leave days that the policy does not state. No step compared the answer with the policy text.
  2. A user states their office, opens a new chat and is asked again. The fact lived in one thread’s state.
  3. A fact saved under one shared namespace appears in another person’s answer. The namespace left out the user id.

What these mean for the design:

What you learned The design decision Where it shows
A wrong answer can look fluent Check it against the sources check_node
A failing check can loop Allow one retry, then stop route_after_check
Facts outlive a thread Keep them in a store remember_fact
Facts belong to a person Put the user id in the namespace (user_id, "memories")

Basic assignment: run python build_index.py, python make_tickets_db.py and then python memory_graph.py. Before each run, write down what you expect: the leave question with no made-up answer, then with one, then with two; then the fact in one thread, the question in a new thread and the same question from a different user. After the run, check each expectation against what you see and find the node in memory_graph.py that explains it.

Project step: change one thing and see. Set bad_attempts=3 and attempts < 3 in route_after_check: how many checks now run? Tell the Copilot two facts in two threads and ask for both in a third. Change the namespace to ("all-users", "memories"): what goes wrong, and why does the user id matter? Remove the needs_human early exit in check_node: what does the check make of an “I don’t know” answer? The build prompt is prompts/1-write-memory-graph.md. Stop when you can say which node produced each behaviour.

Flashcards: test yourself on self-checks and long-term memory

Say the answer aloud before revealing it. Speaking is a different skill from reading, so practise both.

1. What is a self-check (reflection)? A step after an answer in which a second model call checks whether the sources support it. If the check fails, the graph retries or gives up. In the Copilot the step is `check_node`.
2. What does the verdict from `check_node` hold? Two fields: `supported`, true only when every fact in the answer is in the policy text, and `reason`, one short sentence. The graph branches on `supported`.
3. Why does the graph retry only once? A check that can fail must not loop for ever, and each retry costs more model calls. After the attempt counter reaches 2 the route goes to `give_up_node`, which says it cannot confirm the answer.
4. Why is a self-check not proof? It is another model call, so it can be wrong too. It reduces bad answers but does not remove them, which is why a person stays in the loop for risky actions.
5. Name the four agent design patterns from this session and the one the Copilot uses. Router, reflection, planner and executor, and supervisor. The Copilot uses a router (`sort` and `route_by_kind`) and reflection (`check_node`).
6. What is a store, and how is it different from a checkpointer? A store keeps facts outside any one thread, so any thread can read them; the checkpointer keeps the state of one thread. Most applications use both.
7. What is a namespace, and which one does the Copilot use? A folder-like tuple that a memory is filed under. The Copilot uses `(user_id, "memories")`, so each person has their own drawer.
8. What do `user_id` and `thread_id` each do? `user_id` says whose memories to use, and `thread_id` says which conversation to continue. A fact saved in one thread is found in another because both threads share the user id.
9. Which calls write and read a memory? `store.put` writes a fact under a namespace and a key, and `store.search` returns the facts in a namespace. `remember_fact` writes; `fallback_node` reads.
10. What happens to `InMemoryStore` when the program ends? Its facts are lost, because they were kept in the running program only. For real use, swap in a store that writes to a file or database; the nodes stay the same.

Model answer: how do I explain a self-check and long-term memory to a teammate?

The core question this session answers: “A teammate asks: how do we stop the Copilot sending an answer the policy does not support, and how does it remember a user across chats?”

Five beats a teammate listens for, in this order: the check · the limit · the store · store or checkpointer · the honest limit. Skip any of these five and the answer is incomplete.

  1. The check: a second model call tests the answer against the policy text.
  2. The limit: one retry, then a plain give-up.
  3. The store: facts filed under a namespace that holds the user id.
  4. Store or checkpointer: the thread keeps its state; the store keeps the person’s facts.
  5. The honest limit: the check is another model, and InMemoryStore is lost on restart.

The full answer, said aloud:

(The check) “After the policy answer, a check node sends the policy text and the answer to a second model call, which says whether every fact in the answer is supported.

(The limit) An unsupported answer gets one more try. A second failure goes to a node that says it cannot confirm the answer and asks for a person, so the graph does not loop for ever.

(The store) A remember step saves a lasting fact with store.put under the user’s namespace, and the fallback reads it with store.search, so a new thread finds it.

(Store or checkpointer) The checkpointer keeps one thread’s state; the store keeps a person’s facts across threads, and we compile the graph with both.

(The honest limit) The check reduces bad answers but is not proof, and the in-memory store forgets everything when the program ends, so real use needs a store that writes to a file or database.“

Missing a beat is a failure state. Leave out the limit and the next question is what stops an endless loop; leave out the honest limit and the next question is whether the check can be trusted. Practise until the five come out without prompting, then stop talking and let the questions come.

Why this matters: why do self-checks and memory matter at work?

In a conversation with a teammate or manager: people ask how a wrong answer is caught before it is sent and what the assistant remembers about them. “A second step checks it against the source, and facts are filed under the person’s id” is a concrete reply.

On the job: quality rules for an assistant, a privacy review of what it stores, and and a choice between one agent and several draw on the ideas here. A check with a limit and a store with a clear namespace are the parts reviewers ask about.

In the rest of this course: session 28 runs the finished Copilot through the six Milestone 2 checks, sessions 29 to 32 let several agents and outside tool servers work together, and later sessions test and secure what you built.

After this unit: what do I do after this lesson, and what comes next?

Assignment. Run memory_graph.py and match each behaviour to its node. Then change the retry limit and the namespace as in the project step. The brief for the project work is the project file. Keep memory_graph.py: session 28 uses it as copilot_graph.py.

What’s next. Session 28 is the Milestone 2 lab: one script, check_milestone2.py, runs six checks on the finished Copilot and a small test set, and tells you which part of the graph to look at when a check fails.

Want help building this with someone?

Tell us what you are working on and we will write back about a mentor-led cohort, live sessions or 1:1 training. It is only an enquiry: you choose whether to go further.

Ask about mentor-led training

TechEazy Consulting training material — not affiliated with or endorsed by OpenAI, Anthropic, LangChain or any other company named in this course.