新版文档(v1.x)
langchain
Long-term memory

Long-term memory

import LongTermMemoryCreateAgentInmemoryPy from '/snippets/code-samples/long-term-memory-create-agent-inmemory-py.mdx'; import LongTermMemoryCreateAgentInmemoryJs from '/snippets/code-samples/long-term-memory-create-agent-inmemory-js.mdx'; import LongTermMemoryCreateAgentPostgresPy from '/snippets/code-samples/long-term-memory-create-agent-postgres-py.mdx'; import LongTermMemoryCreateAgentPostgresJs from '/snippets/code-samples/long-term-memory-create-agent-postgres-js.mdx'; import LongTermMemoryStorageInmemoryPy from '/snippets/code-samples/long-term-memory-storage-inmemory-py.mdx'; import LongTermMemoryStorageInmemoryJs from '/snippets/code-samples/long-term-memory-storage-inmemory-js.mdx'; import LongTermMemoryStoragePostgresPy from '/snippets/code-samples/long-term-memory-storage-postgres-py.mdx'; import LongTermMemoryStoragePostgresJs from '/snippets/code-samples/long-term-memory-storage-postgres-js.mdx'; import LongTermMemoryReadToolInmemoryPy from '/snippets/code-samples/long-term-memory-read-tool-inmemory-py.mdx'; import LongTermMemoryReadToolInmemoryJs from '/snippets/code-samples/long-term-memory-read-tool-inmemory-js.mdx'; import LongTermMemoryReadToolPostgresPy from '/snippets/code-samples/long-term-memory-read-tool-postgres-py.mdx'; import LongTermMemoryReadToolPostgresJs from '/snippets/code-samples/long-term-memory-read-tool-postgres-js.mdx'; import LongTermMemoryWriteToolInmemoryPy from '/snippets/code-samples/long-term-memory-write-tool-inmemory-py.mdx'; import LongTermMemoryWriteToolInmemoryJs from '/snippets/code-samples/long-term-memory-write-tool-inmemory-js.mdx'; import LongTermMemoryWriteToolPostgresPy from '/snippets/code-samples/long-term-memory-write-tool-postgres-py.mdx'; import LongTermMemoryWriteToolPostgresJs from '/snippets/code-samples/long-term-memory-write-tool-postgres-js.mdx';

Long-term memory lets your agent store and recall information across different conversations and sessions. Unlike short-term memory (opens in a new tab), which is scoped to a single thread, long-term memory persists across threads and can be recalled at any time.

Long-term memory is built on LangGraph stores (opens in a new tab), which save data as JSON documents organized by namespace and key.

Usage

To add long-term memory to an agent, create a store and pass it to @[create_agent]:

pip install -U langgraph-checkpoint-postgres "psycopg[binary]"
uv add langgraph-checkpoint-postgres "psycopg[binary]"

By default, langgraph-checkpoint-postgres installs psycopg (Psycopg 3) without extras. The install above adds psycopg[binary], which is recommended for most users. For other options, see the Psycopg installation docs (opens in a new tab).

For other store backends, including Redis and MongoDB, see the [store integrations](https://docs.langchain.com/oss/integrations/long-term-memory) list. For a MongoDB walkthrough, see [long-term memory with MongoDB](https://docs.langchain.com/oss/integrations/memory/mongodb-long-term-memory).

Tools can then read from and write to the store using the runtime.store parameter. See Read long-term memory in tools and Write long-term memory from tools for examples.

For a deeper dive into memory types (semantic, episodic, procedural) and strategies for writing memories, see the [Memory conceptual guide](https://docs.langchain.com/oss/concepts/memory#long-term-memory).

Memory storage

LangGraph stores long-term memories as JSON documents in a store (opens in a new tab).

Each memory is organized under a custom namespace (similar to a folder) and a distinct key (like a file name). Namespaces often include user or org IDs or other labels that makes it easier to organize information.

This structure enables hierarchical organization of memories. Cross-namespace searching is then supported through content filters.

For more information about the memory store, see the Persistence (opens in a new tab) guide.

Read long-term memory in tools

Write long-term memory from tools