Blog: Adding Long-Term Memory to LangGraph and LangChain Agents (#637)

* Add blog post: Adding Long-Term Memory to LangGraph and LangChain Agents

* blog: update langgraph post date to 2026-03-24 and add cover image

* blog: fix claude-code-telegram filename to match frontmatter date (2026-03-25)

* blog: set claude-code-telegram date to 2026-03-23

* blog: fix date timezone offset by adding T12:00 to all post dates

* ci: trigger fresh CI run

* blog: fix broken docs link (routeBasePath is /)
This commit is contained in:
Ben 2026-03-24 13:51:27 -04:00 committed by GitHub
parent 39bf6820d6
commit 0ad6ee3156
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
29 changed files with 1116 additions and 851 deletions

View file

@ -4,7 +4,7 @@ description: Learn how Hindsight handles contradictory information by tracking t
authors: [chrislatimer]
image: /img/blog/2026-02-09/consolidation-pipeline.png
date: 2026-02-09
date: 2026-02-09T12:00
hide_table_of_contents: true
---

View file

@ -2,7 +2,7 @@
title: "What's new in Hindsight 0.4.11"
description: New features and improvements in Hindsight 0.4.11
authors: [nicoloboschi]
date: 2026-02-13
date: 2026-02-13T12:00
hide_table_of_contents: true
---

View file

@ -2,7 +2,7 @@
title: "What's new in Hindsight 0.4.12"
description: New features and improvements in Hindsight 0.4.12
authors: [nicoloboschi]
date: 2026-02-18
date: 2026-02-18T12:00
hide_table_of_contents: true
---

View file

@ -1,7 +1,7 @@
---
title: "Your Vercel Chat SDK bot forgets everything. Hindsight fixes that."
authors: [nicoloboschi]
date: 2026-02-26
date: 2026-02-26T12:00
tags: [chat-sdk, slack, discord, typescript, memory]
image: /img/blog/vercel-chat.png
---

View file

@ -2,7 +2,7 @@
title: "What's new in Hindsight 0.4.13 and 0.4.14"
description: New features and improvements in Hindsight 0.4.13 and 0.4.14
authors: [nicoloboschi]
date: 2026-02-27
date: 2026-02-27T12:00
hide_table_of_contents: true
---

View file

@ -2,7 +2,7 @@
title: "Your CrewAI Agents Forget Everything Between Runs. Here's the Fix."
authors: [benfrank241]
date: 2026-03-02
date: 2026-03-02T12:00
tags: [crewai, agents, python, memory, tutorial]
image: /img/blog/crewai-memory.png
---

View file

@ -2,7 +2,7 @@
title: "I Gave 100+ LLMs a Permanent Memory With One Python Package"
authors: [benfrank241]
date: 2026-03-03
date: 2026-03-03T12:00
tags: [litellm, python, memory, openai, anthropic, tutorial]
image: /img/blog/litellm-memory.png
---

View file

@ -2,7 +2,7 @@
title: "What's new in Hindsight 0.4.15"
description: New features and improvements in Hindsight 0.4.15
authors: [nicoloboschi]
date: 2026-03-03
date: 2026-03-03T12:00
hide_table_of_contents: true
image: /img/blog/release0415.png
---

View file

@ -2,7 +2,7 @@
title: "The Open-Source MCP Memory Server Your AI Agent Is Missing"
authors: [benfrank241]
date: 2026-03-04
date: 2026-03-04T12:00
tags: [mcp, memory, agents, docker, tutorial]
image: /img/blog/mcp-agent-memory.png
hide_table_of_contents: true

View file

@ -1,7 +1,7 @@
---
title: "Give Your OpenAI App a Memory in 5 Minutes"
authors: [benfrank241]
date: 2026-03-05
date: 2026-03-05T12:00
tags: [memory, openai, python, docker, rag, llm, vector, embedding]
image: /img/blog/add-memory-to-openai-application.png
hide_table_of_contents: true

View file

@ -2,7 +2,7 @@
title: "What's new in Hindsight 0.4.16"
description: New features and improvements in Hindsight 0.4.16
authors: [nicoloboschi]
date: 2026-03-05
date: 2026-03-05T12:00
hide_table_of_contents: true
image: /img/blog/release0416.png
---

View file

@ -1,7 +1,7 @@
---
title: "The Memory Upgrade Every OpenClaw User Needs"
authors: [benfrank241]
date: 2026-03-06
date: 2026-03-06T12:00
tags: [openclaw]
image: /img/blog/adding-memory-to-openclaw-with-hindsight.png
hide_table_of_contents: true

View file

@ -1,7 +1,7 @@
---
title: "What's New in Hindsight Cloud: Document File Upload"
authors: [benfrank241]
date: 2026-03-09
date: 2026-03-09T12:00
tags: [hindsight-cloud, release, memory]
hide_table_of_contents: true
---

View file

@ -1,7 +1,7 @@
---
title: "Pydantic AI Persistent Memory: Add It in 5 Lines of Code"
authors: [benfrank241]
date: 2026-03-09
date: 2026-03-09T12:00
tags: [memory, openai, anthropic, gemini, python, rust, agents, rag, vector, pydantic-ai, knowledge-graph]
image: /img/blog/pydantic-ai-persistent-memory.png
hide_table_of_contents: true

View file

@ -1,7 +1,7 @@
---
title: "Run Hindsight with Ollama: Local AI Memory, No API Keys Needed"
authors: [hindsight]
date: 2026-03-10
date: 2026-03-10T12:00
tags: [ollama, tutorial, python, memory, local, privacy, hindsight, llm, open-source]
image: /img/blog/run-hindsight-with-ollama.png
hide_table_of_contents: true

View file

@ -2,7 +2,7 @@
title: "What's new in Hindsight 0.4.17"
description: New features and improvements in Hindsight 0.4.17
authors: [nicoloboschi]
date: 2026-03-10
date: 2026-03-10T12:00
hide_table_of_contents: true
image: /img/blog/release0417.png
---

View file

@ -1,7 +1,7 @@
---
title: "What's New in Hindsight Cloud: Programmatic API Key Management"
authors: [benfrank241]
date: 2026-03-11
date: 2026-03-11T12:00
tags: [hindsight-cloud, release, api]
hide_table_of_contents: true
---

View file

@ -1,7 +1,7 @@
---
title: "How We Built Time-Aware Spreading Activation for Memory Graphs"
authors: [chrislatimer]
date: 2026-03-12
date: 2026-03-12T12:00
tags: [retrieval, graph, temporal, spreading-activation, memory]
image: /img/blog/spreading-activation-memory-graphs.png
---

View file

@ -1,7 +1,7 @@
---
title: "How We Built Disposition-Aware Agents That Actually Think Differently"
authors: [chrislatimer]
date: 2026-03-13
date: 2026-03-13T12:00
tags: [disposition, personality, skepticism, empathy, reflect, agents]
image: /img/blog/disposition-aware-agents.png
---

View file

@ -2,7 +2,7 @@
title: "What's new in Hindsight 0.4.18"
description: New features and improvements in Hindsight 0.4.18
authors: [nicoloboschi]
date: 2026-03-13
date: 2026-03-13T12:00
hide_table_of_contents: true
image: /img/blog/release0418.png
---

View file

@ -1,7 +1,7 @@
---
title: "Give the Only Self-Improving AI Agent (Hermes) a Memory Upgrade It Deserves"
authors: [benfrank241]
date: 2026-03-17
date: 2026-03-17T12:00
tags: [hermes, agents, python, memory, tutorial, plugin]
image: /img/blog/hermes-agent-memory.png
---

View file

@ -1,7 +1,7 @@
---
title: "I Built a Chatbot That Never Forgets — In 80 Lines of Python"
authors: [benfrank241]
date: 2026-03-17
date: 2026-03-17T12:00
tags: [streamlit, tutorial, python, memory, chatbot, web-ui]
slug: python-chatbot-memory-streamlit
image: /img/blog/streamlit-chatbot-memory.png

View file

@ -2,7 +2,7 @@
title: "What's new in Hindsight 0.4.19"
description: New features and improvements in Hindsight 0.4.19
authors: [nicoloboschi]
date: 2026-03-18
date: 2026-03-18T12:00
hide_table_of_contents: true
image: /img/blog/release0419.jpg
---

View file

@ -3,7 +3,7 @@ slug: sandboxed-agent-persistent-memory-nemoclaw
title: "Give NemoClaw the Best Agent Memory Available In One Command"
description: Add persistent memory to a NemoClaw sandboxed AI agent without changing code. One command, one network policy, memories survive across sessions.
authors: [hindsight]
date: 2026-03-19
date: 2026-03-19T12:00
image: /img/blog/2026-03-19/nemoclaw-memory.png
hide_table_of_contents: true
---

View file

@ -1,7 +1,7 @@
---
title: "Agent Memory Benchmark: A Manifesto"
authors: [nicoloboschi]
date: 2026-03-23
date: 2026-03-23T12:00
tags: [benchmark, memory, agents, evaluation, longmemeval, locomo, open-source]
image: /img/blog/amb/explorer-0.png
hide_table_of_contents: true

View file

@ -1,7 +1,7 @@
---
title: "OpenClaude: Build a Claude Code Agent with Long-Term Memory — and Take It Everywhere"
authors: [fabioscarsi, nicoloboschi]
date: 2026-03-25
date: 2026-03-23T12:00
tags: [claude-code, telegram, hindsight, memory, mcp, agents, tutorial]
image: /img/blog/claude-code-telegram.png
hide\_table\_of\_contents: true

View file

@ -0,0 +1,259 @@
---
title: "Adding Long-Term Memory to LangGraph and LangChain Agents"
description: Learn how to add long-term memory to LangGraph and LangChain agents using three integration patterns — tools, nodes, and BaseStore — with per-user memory banks and semantic recall.
authors: [DK09876]
date: 2026-03-24T12:00
tags: [langgraph, langchain, integrations, agents, memory]
image: /img/blog/langgraph-longterm-memory.png
hide_table_of_contents: true
---
![Adding Long-Term Memory to LangGraph and LangChain Agents](/img/blog/langgraph-longterm-memory.png)
LangGraph agents are stateful by design — checkpointers save graph state between steps, and the Store API persists data across threads. But neither gives agents true long-term memory: the ability to extract meaning from conversations, build up knowledge over time, and recall it semantically when relevant.
That's what Hindsight adds. Hindsight is a memory layer for LLM applications that automatically extracts facts from conversations, builds entity graphs, and retrieves relevant context using four parallel recall strategies. The `hindsight-langgraph` package brings that to LangGraph — and since the memory tools are standard LangChain `@tool` functions, they work with plain LangChain too.
<!-- truncate -->
## The problem
LangGraph's built-in persistence is designed for graph state — checkpoints, intermediate values, cross-thread key-value storage. It's good at "what did this graph do last time?" but not at "what does this agent know about this user?"
Consider a support agent that talks to the same customer across dozens of sessions. With checkpointers alone, each new thread starts cold. With `InMemoryStore` or `PostgresStore`, you can manually store and retrieve facts, but you're responsible for:
- Deciding what to store (fact extraction)
- Deciding what's relevant (semantic retrieval)
- Handling contradictions and updates
- Building knowledge graphs from raw conversations
Hindsight does all of this automatically. You retain conversations, and it extracts facts, builds entity graphs, and retrieves relevant memories using four parallel strategies: **semantic** (embedding similarity), **BM25** (keyword overlap), **graph traversal** (entity relationships), and **temporal** (recency weighting). Each strategy catches different things — semantic recall finds conceptually similar memories, graph traversal finds memories linked through shared entities, and temporal weighting surfaces recent context before older facts. Together they substantially outperform single-strategy retrieval.
## Three integration patterns
We built three ways to add Hindsight memory to LangGraph, at different abstraction levels.
### 1. Tools — the agent decides (LangChain & LangGraph)
Give the agent retain/recall/reflect tools and let it decide when to use memory. These are standard LangChain `@tool` functions, so they work with both LangGraph (via `create_react_agent`) and plain LangChain (via `bind_tools()`).
```python
from hindsight_client import Hindsight
from hindsight_langgraph import create_hindsight_tools
from langchain_openai import ChatOpenAI
from langgraph.prebuilt import create_react_agent
client = Hindsight(base_url="http://localhost:8888")
tools = create_hindsight_tools(client=client, bank_id="user-123")
# With LangGraph
agent = create_react_agent(ChatOpenAI(model="gpt-4o"), tools=tools)
# Or with plain LangChain
model = ChatOpenAI(model="gpt-4o").bind_tools(tools)
```
The agent gets three tools:
- **`hindsight_retain`** — stores the conversation and extracts facts from it
- **`hindsight_recall`** — searches the memory bank for relevant context
- **`hindsight_reflect`** — synthesizes across multiple memories to produce a summary or answer a question about what the agent knows (useful for questions like "what has this user told me about their stack?")
The agent calls these based on conversation context — storing facts when the user shares something important, recalling when asked about past context, and reflecting when it needs to synthesize accumulated knowledge.
**Best for**: ReAct agents that need to reason about when memory is relevant. Works with LangGraph for automatic tool execution loops or with plain LangChain if you manage the loop yourself.
### 2. Nodes — memory as graph steps
Add recall and retain as automatic nodes in your graph. No tool-calling required — memory runs on every turn.
```python
from hindsight_langgraph import create_recall_node, create_retain_node
from langgraph.graph import StateGraph, MessagesState, START, END
recall = create_recall_node(client=client, bank_id_from_config="user_id")
retain = create_retain_node(client=client, bank_id_from_config="user_id")
builder = StateGraph(MessagesState)
builder.add_node("recall", recall)
builder.add_node("agent", agent_node)
builder.add_node("retain", retain)
builder.add_edge(START, "recall")
builder.add_edge("recall", "agent")
builder.add_edge("agent", "retain")
builder.add_edge("retain", END)
```
The recall node runs before the LLM, searches Hindsight for memories relevant to the user's message, and injects them as a `SystemMessage`. The retain node runs after, storing the conversation. Both resolve per-user bank IDs from `RunnableConfig` at runtime.
**Best for**: Agents where you always want memory context injected automatically, without relying on the LLM to decide when to use memory tools.
### 3. BaseStore — drop-in backend
Replace LangGraph's `InMemoryStore` with Hindsight as the storage backend. If your team already uses LangGraph's store patterns, this is the lowest-friction path.
```python
from hindsight_langgraph import HindsightStore
store = HindsightStore(client=client)
graph = builder.compile(checkpointer=checkpointer, store=store)
```
Namespace tuples map to Hindsight bank IDs (`("user", "123")` → bank `user.123`), banks are auto-created, and `search()` uses Hindsight's full semantic recall instead of basic vector similarity.
**Best for**: Teams already using LangGraph's `store` patterns who want better retrieval without restructuring their graph.
---
### Which pattern fits your use case?
| | Tools | Nodes | BaseStore |
|---|---|---|---|
| Works with plain LangChain | Yes | No | No |
| Memory runs automatically | No (LLM decides) | Yes | Yes |
| Uses existing store interface | No | No | Yes |
| LLM controls when to remember | Yes | No | No |
| Lowest migration cost | — | Low | Lowest |
---
## Complete working example
Here's a full support agent that remembers each user across sessions using the nodes pattern. This is copy-pasteable and runnable against either self-hosted Hindsight or Hindsight Cloud.
```python
import asyncio
from hindsight_client import Hindsight
from hindsight_langgraph import create_recall_node, create_retain_node
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, SystemMessage
from langgraph.graph import StateGraph, MessagesState, START, END
from langgraph.checkpoint.memory import MemorySaver
# --- Setup ---
client = Hindsight(base_url="http://localhost:8888")
# For Hindsight Cloud:
# client = Hindsight(base_url="https://api.hindsight.vectorize.io", api_key="...")
llm = ChatOpenAI(model="gpt-4o")
checkpointer = MemorySaver()
# --- Memory nodes ---
# bank_id_from_config pulls the user ID from RunnableConfig at runtime,
# so one graph definition serves all users with isolated memory banks.
recall = create_recall_node(client=client, bank_id_from_config="user_id")
retain = create_retain_node(client=client, bank_id_from_config="user_id")
# --- Agent node ---
async def agent_node(state: MessagesState):
system = SystemMessage(content=(
"You are a helpful support agent. "
"Relevant memories about this user have been injected above. "
"Use them to personalize your response."
))
response = await llm.ainvoke([system] + state["messages"])
return {"messages": [response]}
# --- Graph ---
builder = StateGraph(MessagesState)
builder.add_node("recall", recall)
builder.add_node("agent", agent_node)
builder.add_node("retain", retain)
builder.add_edge(START, "recall")
builder.add_edge("recall", "agent")
builder.add_edge("agent", "retain")
builder.add_edge("retain", END)
graph = builder.compile(checkpointer=checkpointer)
# --- Run ---
async def chat(user_id: str, thread_id: str, message: str):
config = {
"configurable": {
"user_id": user_id,
"thread_id": thread_id,
}
}
result = await graph.ainvoke(
{"messages": [HumanMessage(content=message)]},
config=config,
)
return result["messages"][-1].content
async def main():
# Session 1: user shares context
print("Session 1")
print(await chat("user-42", "thread-1", "Hi! I'm running into issues with our Postgres connection pool. We're on SQLAlchemy 2.0."))
print(await chat("user-42", "thread-1", "We're using async sessions with asyncpg. The pool keeps exhausting under load."))
# Session 2: new thread, same user — agent remembers
print("\nSession 2 (new thread)")
print(await chat("user-42", "thread-2", "Hey, back again. Still fighting the connection pool issue."))
# Agent recalls SQLAlchemy 2.0, asyncpg, and the pool exhaustion context
# without the user having to repeat themselves.
asyncio.run(main())
```
What Hindsight extracts from Session 1 and stores in `user-42`'s memory bank:
```
- Uses SQLAlchemy 2.0 with async sessions
- Uses asyncpg driver
- Experiencing connection pool exhaustion under load
- Running Postgres
```
When Session 2 starts on a fresh thread, the recall node searches the memory bank for context relevant to "Still fighting the connection pool issue" and injects those facts as a `SystemMessage` before the LLM responds. The agent picks up exactly where the last session ended.
---
## Per-user memory in one line
All three patterns support dynamic bank IDs. Instead of hardcoding a bank, resolve it from the graph's config at runtime:
```python
recall = create_recall_node(client=client, bank_id_from_config="user_id")
# Each invocation gets its own isolated memory bank
await graph.ainvoke(
{"messages": [...]},
config={"configurable": {"user_id": "user-456"}},
)
```
One graph definition serves all users. Memory banks are created automatically and kept fully isolated.
## Getting started
```bash
pip install hindsight-langgraph
```
Works with both self-hosted Hindsight and [Hindsight Cloud](https://ui.hindsight.vectorize.io/signup). For cloud, pass your API key when creating the client:
```python
client = Hindsight(base_url="https://api.hindsight.vectorize.io", api_key="your-key")
# or
from hindsight_client import configure
configure(api_key="your-key") # defaults to the cloud URL
```
## What to build with this
Long-term memory unlocks a different class of agent behavior. A few patterns we've seen work well:
- **Support agents** that remember each customer's history, preferences, and past issues across sessions
- **Sales assistants** that accumulate context about prospects over multiple touchpoints
- **Personal productivity agents** that build up a model of a user's work style, priorities, and decisions
In all three cases, the agent gets meaningfully better the longer it runs — not just because of a longer context window, but because Hindsight distills conversations into structured knowledge it can retrieve precisely when relevant.
Full docs: [LangGraph integration](/sdks/integrations/langgraph) | [GitHub](https://github.com/vectorize-io/hindsight/tree/main/hindsight-integrations/langgraph)

View file

@ -20,6 +20,12 @@ chrislatimer:
url: https://github.com/chrislatimer
image_url: https://github.com/chrislatimer.png
DK09876:
name: DK09876
title: Hindsight Team
url: https://github.com/DK09876
image_url: https://github.com/DK09876.png
fabioscarsi:
name: Fabio Scarsi
title: Contributor

Binary file not shown.

After

Width:  |  Height:  |  Size: 309 KiB