LangChain and LangGraph · Chapter 16 of 25

Memory and threads

By the end of this chapter you can give an agent a memory that lasts between messages, keep each tenant's conversation apart in its own thread, read and change what a thread has saved, and keep long conversations cheap by sending the model only the recent part. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: LangGraph
  • 4 tasks
  • Python
  • In the all labs plan

The first 2 chapters of LangChain and LangGraph are free. This one comes with the all labs plan.

Why you need this

On WhatsApp a tenant does not write one perfect message. They write "Which room am I in?", the assistant asks for their tenant ID, and they reply "T1050". That second message only makes sense next to the first.

Every agent so far starts from nothing on each invoke: the state is made from what you pass in, and thrown away at the end. So the reply "T1050" arrives alone, and the assistant has no idea what it is for. And RoomRaja has 120 tenants writing at once: whatever remembers must keep Isha's conversation away from Kavya's.

LangGraph's answer is the checkpointer: it saves the graph's state after every step, under a thread id you choose, and loads it again on the next invoke with the same id. Memory is not a new feature of the model. It is the state, saved per thread.

What you build: 4 tasks

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

  1. Task 1

    two_threads.py: two tenants, two threads

    Two tenants write at the same time. Build an agent with create_agent: lab-chat, the tools get_tenant and rent_due (each a @tool around RoomRaja's function of the same name), and an InMemorySaver from langgraph.checkpoint.memory as its checkpointer. Each tenant's WhatsApp number is their thread_id. Send these four messages, in this order, each with the config for its thread:

    9862581386: How much rent do I owe?

    9759205577: How much rent do I owe?

    9862581386: T1061

    9759205577: T1072

    After each, print one line: the phone number, a colon and a space, then the text of the agent's last message.

  2. Task 2

    inspect_state.py: read the state, then change it

    Build task 1's agent (lab-chat, get_tenant and rent_due, an InMemorySaver) and, on the thread 9412728836, send How much rent do I owe? and then T1023. Then read the thread's saved state with the agent's get_state and print two lines: Messages: and how many messages it holds, then Types: and their types (human, ai or tool) joined with a comma and a space.

    The tenant asks RoomRaja to delete the chat. Remove every message from the thread with the agent's update_state and one RemoveMessage whose id is REMOVE_ALL_MESSAGES (from langgraph.graph.message), and print After delete: and how many messages the thread now holds. Last, send T1023 again on the same thread and print Reply: and the agent's last message.

  3. Task 3

    trim.py: send only the recent messages

    Long chats cost tokens on every request. Build a graph by hand: MessagesState, one node called model, START to model to END, compiled with an InMemorySaver. The model node trims the state's messages with trim_messages from langchain_core.messages: keep the last 4 (max_tokens=4 with token_counter=len, which counts messages), strategy last, starting on a human message. It prints Sending X of Y messages (X the trimmed count, Y the state's) and returns lab-chat's reply to the trimmed messages.

    On the thread 9436564110, invoke it with these four messages, in order:

    Hi, I am Sahil from HIN-104

    Is breakfast included?

    What time is dinner?

    Can I switch off dinner for tomorrow?

  4. Task 4 · interview question

    Interview: memory_is_state.py, where the memory lives

    Write a function that makes an agent with create_agent (lab-chat, the one tool rent_due, the checkpointer you pass it). Make one InMemorySaver and an agent with it, and send How much rent do I owe? on the thread chat-1. Then send T1042 three times and print one line each:

    Same thread: and the reply, from the same agent on chat-1

    New thread: and the reply, from the same agent on the thread chat-2

    New saver: and the reply, from a new agent with a new InMemorySaver, on chat-1

    The interviewer will ask: where does the agent's memory live, and why did only one of the three remember?

What you will learn

  • Checkpointers: InMemorySaver, and what a checkpoint holds.
  • Threads: the thread_id in the config, one conversation each.
  • Reading and changing a thread: get_state and update_state.
  • Deleting messages with RemoveMessage.
  • Trimming: sending only the recent messages with trim_messages.

More in LangGraph

Build "Memory and threads" yourself

RoomRaja, a Pune co-living company, answers tenants on WhatsApp by hand. You build its assistant. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of LangChain and LangGraph are free. This one comes with the all labs plan.