§ Tutorial
Add long-term memory to a LangGraph agent. Persistent, across runs.
LangGraph handles a graph's state within a run. This tutorial gives your agent memory that survives across runs — expose OctaMem recall and save as tools, so the agent remembers facts, preferences, and decisions.
Framework
LangGraph
SDK
octamem (Python)
Est. time
~10 min
LangGraph is excellent at orchestrating an agent’s steps, but its state is scoped to a run. To make an agent remember across sessions, you give it a long-term memory store it can read from and write to. With OctaMem you do this in two ways: expose memory as tools (works with any LangGraph agent), or use the octamem[langchain] drop-in memory for LangChain chains.
Prerequisites
- Python 3.9+ and an OpenAI API key (or any LangChain-supported model).
- A free OctaMem API key from platform.octamem.com.
Step 1: Install
pip install octamem 'octamem[langchain]' langgraph langchain-openaiSet your keys as environment variables: OCTAMEM_API_KEY and OPENAI_API_KEY.
Step 2: Expose memory as tools
Wrap client.add and client.get as LangChain tools and bind them to a create_react_agent. The agent decides when to save and when to recall.
import os
from octamem import OctaMem
from langchain_core.tools import tool
from langchain_openai import ChatOpenAI
from langgraph.prebuilt import create_react_agent
# Reads OCTAMEM_API_KEY from the environment.
memory = OctaMem()
@tool
def save_memory(content: str, topic: str = "") -> str:
"""Store a fact, preference, or decision in long-term memory."""
memory.add(content=content, previous_context=topic)
return "saved"
@tool
def recall_memory(query: str, topic: str = "") -> str:
"""Search long-term memory for context relevant to the query."""
return str(memory.get(query=query, previous_context=topic))
agent = create_react_agent(
ChatOpenAI(model="gpt-4o-mini"),
tools=[save_memory, recall_memory],
)
# Turn 1 — the agent stores something.
agent.invoke({"messages": [("user", "Remember our launch date is April 15.")]})
# Later, a brand-new run — the agent can recall it.
out = agent.invoke({"messages": [("user", "When is our launch again?")]})
print(out["messages"][-1].content)Alternative: LangChain drop-in memory
If you’re using a LangChain ConversationChain rather than a LangGraph graph, OctaMem ships a drop-in memory class:
from octamem.integrations.langchain import OctaMemMemory
from langchain.chains import ConversationChain
from langchain_openai import ChatOpenAI
chain = ConversationChain(
llm=ChatOpenAI(model="gpt-4o-mini"),
memory=OctaMemMemory(),
)
chain.predict(input="My launch date is Sept 1.")How it works
Memory lives in your OctaMem account, not in the graph state. Each add stores a typed record with provenance; each getretrieves relevant memories by natural-language query. Because it’s external, the same memory is available across runs, across agents, and even across tools — see the four types of AI agent memory for the model behind it.
FAQ
Does LangGraph have built-in long-term memory?
LangGraph manages state within a graph run and can persist it with a checkpointer, but that is short-term/session state. For durable memory that survives across separate runs and agents, you connect an external memory store such as OctaMem and expose it as tools the agent can call.
How do I add memory to a LangGraph agent with OctaMem?
Install the octamem SDK, create a client with your API key, and wrap client.add and client.get as LangChain tools. Bind those tools to your model in create_react_agent (or a custom graph node). The agent then calls save_memory and recall_memory as needed, and the data persists in your OctaMem account across runs.
Is the memory shared across agents?
Yes. Because memory lives in your OctaMem account rather than in the graph's state, any agent using the same API key can recall what another stored — including agents built on other frameworks or MCP clients like Cursor and Claude.