TellWorldAI World Archive
Ask中文

Home›Practice›Practice

Practice

Long-Term Memory in LangChain Agents

Give a LangChain agent long-term memory with a LangGraph store, organized by namespace and key, read and written from tools, and set up with PostgreSQL dependencies.

Who this is for and what you will be able to do

This guide is for developers who have already built a LangChain agent and want it to remember information across conversations and sessions. By the end you should be able to tell short-term memory from long-term memory, understand how a LangGraph store organizes data, pass a store to an agent so that tools can read and write it, and install the dependencies for the PostgreSQL backend.

How long-term memory differs from short-term memory

Long-term memory lets an agent store and recall information across different conversations and sessions[1]. Short-term memory is scoped to a single thread. Long-term memory persists across threads and can be recalled at any time[1].

The foundation: LangGraph stores

Long-term memory is built on LangGraph stores. A store saves data as JSON documents and organizes them by namespace and key[1]. A namespace works like a folder and a key works like a file name. Namespaces often include user or org IDs or other labels, which makes memories easier to organize[1].

Namespaces can be hierarchical, and cross-namespace searching is supported through content filters[1].

Short-term and long-term memory

Short-term memory is scoped to a single thread. A conversation in one thread can pick up where it left off, but another thread cannot see it[1]. Long-term memory persists across threads and can be recalled at any time, so it suits information that should survive between conversations[1].

The two work well together. The current conversation's context stays in its thread, while anything that must outlive the conversation is written to the store. Which information belongs in long-term memory depends on what your application needs to remember, and there is no single answer for every product.

Namespaces and keys

Every memory in the store sits under a custom namespace and has its own key. A namespace works like a folder and a key works like a file name, so the pair locates one memory[1].

Many applications put a user ID or an org ID into the namespace, which keeps each user's memories apart and makes them easier to organize[1]. Namespaces can also be hierarchical. When you need to search across namespaces, content filters narrow the results[1].

Adding memory to an agent

First, create a store, then pass it to create_agent[1]. Tools read from and write to the store through the runtime.store parameter[1]. Two store options are covered: InMemoryStore and PostgreSQL[1].

Reading and writing memory from tools

Tools reach the store through the runtime.store parameter, and both reads and writes go through it[1]. Because the model decides when to call a tool, the model can decide when to save something and when to look it up. Keeping that logic in tools lets the same agent mix recall and storage with the rest of its work[1].

Installing the PostgreSQL dependencies

With pip, install the checkpoint package with the binary extra of psycopg:

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

With uv, add the same packages to the project:

uv add langgraph-checkpoint-postgres "psycopg[binary]"

By default, langgraph-checkpoint-postgres installs psycopg (Psycopg 3) without extras. The command above adds psycopg[binary], which is recommended for most users[1]. For other installation options, see the Psycopg installation documentation[1].

In a JavaScript project, use npm:

npm install @langchain/langgraph-checkpoint-postgres

Other store backends

Other store backends, including Redis and MongoDB, are listed in the store integrations list[1]. For MongoDB, there is a separate walkthrough for long-term memory[1].

Memory types and writing strategies

Semantic, episodic, and procedural memory are the main memory types. The Memory conceptual guide covers them in more depth, along with strategies for writing memories[1]. For the persistence details of the store itself, see the Persistence guide of LangGraph stores[1].

Next steps

To go further, read the persistence guide for LangGraph stores and choose a backend that fits your stack[1].

Adapted from the Long-term memory page of LangChain's official documentation[1], released under the MIT License.

Sources

  1. 1
    Long-term memoryLangChain docs (MIT)

Cite this page: Long-Term Memory in LangChain Agents. TellWorld, https://www.tellworld.net/practice/practice-langchain-long-term-memory. Updated 2026-11-14

Related news

View all