Overview
LangGraph agents are stateful by design — but that state resets every session. Remem adds cross-session memory that persists across sessions, deployments, and LLM switches. The pattern is simple: add two nodes to your graph.load_memory runs before your agent responds, fetching relevant memories and injecting them into state. save_memory runs after, storing what the user just said. Your agent node itself needs no changes — it just reads memory_context from state.
Prerequisites
Install the required packages:.env file with your API keys:
The Pattern
The three-node structure keeps memory concerns completely separate from your agent logic:Steps
1
Define AgentState
Add
memory_context to your TypedDict. This field carries memories from load_memory into the agent node without any direct coupling between the two.2
Create the load_memory node
This node runs first. It fetches memories relevant to the user’s latest message using
AsyncRememClient.recall() and writes them into state as a formatted string.3
Create the agent node
The agent node reads
memory_context from state and injects it into the system prompt. No changes needed to your core agent logic — memory is just another string in the prompt.4
Create the save_memory node
After the agent responds, save the user’s last message as an episodic memory. This node always returns state unchanged — its only job is the side effect of writing to Remem.
5
Wire the graph
Connect the three nodes in order, set
load_memory as the entry point, and compile:6
Run the graph
Invoke the graph with a
HumanMessage, a user_id, and an empty memory_context. LangGraph fills memory_context during execution via the load_memory node.Complete Example
Here is the full working implementation in a single file:context() vs recall() in LangGraph
Both methods retrieve memories, but they serve different purposes in a LangGraph workflow:
Use this pattern to handle both cases in
load_memory:
Tips
Handle first-time users gracefully
Handle first-time users gracefully
The first time a user interacts,
recall() returns an empty list. Handle this explicitly so your agent does not crash or hallucinate prior context:Don't save every message
Don't save every message
Not every message is worth remembering. Filter out low-signal messages before calling
save_memory to keep your memory store clean:Store key facts as semantic memories
Store key facts as semantic memories
When your agent detects an important fact about the user, store it separately as semantic memory with high importance so it always surfaces in future sessions — regardless of what the user is currently talking about:
What’s Next
Search API
Full reference for recall — semantic search across stored memories
Context API
Full reference for context — ranked retrieval by importance and recency
Error Handling
Handle Remem errors gracefully so your LangGraph agent never crashes