CloudCodeTree LogoCloudCodeTree
AI NewsTutorialsAbout
CloudCodeTree Logo
CloudCodeTree
  • AI News
  • Tutorials
  • About
← Back to AI News
Give Your LangGraph Agent Persistent Memory With Three Lines of Code

Give Your LangGraph Agent Persistent Memory With Three Lines of Code

Chris Harper

2 min read

Jun 28, 2026 · 12:13 UTC

AI
Tutorial
Agents
LLM

TL;DR: Add an InMemorySaver checkpointer and a thread_id config key; your LangGraph agent remembers every fact across turns and scales to PostgreSQL with a one-line swap.

What you'll be able to do after this:

  • Build agents that remember names, preferences, and decisions across a full conversation session without any per-turn history management
  • Scale from in-process InMemorySaver to PostgreSQL in one line and keep memory across server restarts
  • Handle long conversations without hitting context limits using built-in message trimming and summarization

The core pattern

A checkpointer saves full graph state after every step. The thread_id config key identifies which conversation to read from and write to:

from langgraph.checkpoint.memory import InMemorySaver
from langgraph.graph import StateGraph, MessagesState

checkpointer = InMemorySaver()
builder = StateGraph(MessagesState)
# ... add your nodes and edges ...
graph = builder.compile(checkpointer=checkpointer)

config = {"configurable": {"thread_id": "user-123"}}

graph.invoke(
    {"messages": [{"role": "user", "content": "My name is Alex."}]},
    config
)
# Same thread_id — the agent remembers:
result = graph.invoke(
    {"messages": [{"role": "user", "content": "What's my name?"}]},
    config
)
# → "Your name is Alex."

Change thread_id to start a fresh conversation. Keep it and the checkpointer replays full state before every step — no explicit history management.

Scale to production — one swap

from langgraph.checkpoint.postgres import PostgresSaver

checkpointer = PostgresSaver.from_conn_string("postgresql://user:pass@host/db")
checkpointer.setup()  # run once; creates the schema

MongoDB, Redis, and Oracle checkpointers follow the same interface.

Handling context overflow

Long threads eventually exceed the context window. The two patterns from the LangGraph docs:

from langchain_core.messages import trim_messages

# Keep only the most recent messages (by token count)
trimmed = trim_messages(state["messages"], max_tokens=20, strategy="last")

Or add a SummarizationNode to replace old turns with a compact AI-generated summary — the agent retains the gist of earlier conversation without paying for every token of raw history.

Cross-session long-term memory

For facts that survive across different thread_ids (user preferences, past decisions), add a store alongside the checkpointer:

from langgraph.store.memory import InMemoryStore

store = InMemoryStore()
graph = builder.compile(store=store, checkpointer=checkpointer)

Inside nodes, call store.search(namespace, query=text) to retrieve relevant facts semantically and store.put(namespace, key, value) to save new ones. Swap InMemoryStore for a vector-backed store (pgvector, Redis) for semantic recall across thousands of entries.

Sources: How to add memory to your graph — LangGraph Docs | LangGraph persistence overview