Skip to main content
Long-term memory lets your agent store and recall information across different conversations and sessions. This guide shows how to use MongoDB Atlas as the persistent store backend. MongoDB stores memories as documents in a collection. With an Atlas Vector Search index, the same store can run semantic search over stored memories.
For a deeper dive into memory types and strategies for writing memories, see the Memory conceptual guide.

Setup

Installation

MongoDBStore ships in the same package as the MongoDB checkpointer:

Credentials

Set your Atlas connection string:
If you want automated tracing of your model calls, set your LangSmith API key:

Usage

Create a MongoDBStore with fromConnString (await the promise) and pass it to createAgent. fromConnString connects the client and calls start() to ensure indexes exist. Import MongoDBStore from the package root; there is no /store subpath export.

Memory storage

LangGraph stores memories as JSON documents organized by namespace and key. Namespaces typically include a user or org ID to keep memories scoped. The example below configures vector indexing so store.search can run semantic queries. Pass an embeddings instance and indexConfig. Install @langchain/openai if you use OpenAIEmbeddings (or swap in another embeddings class):
For more information about store operations, see the Stores guide.

Read long-term memory in tools

Tools can read from the store using the runtime parameter, which LangGraph injects automatically:

Write long-term memory from tools

Tools can also write to the store using runtime.store.put, persisting data that survives across threads:

See also