You are viewing the v1 docs for LangChain, which is currently under active development. Learn more.
Overview
LangChain agents use LangGraph persistence to enable long term memory. This is a more advanced topic and requires knowledge of LangGraph to use.Memory storage
LangGraph stores long-term memories as JSON documents in a store. Each memory is organized under a customnamespace
(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.
Read long-term memory in tools
tool the agent can use to look up user information"
- The
InMemoryStore
is a store that stores data in memory. In a production setting, you would typically use a database or other persistent storage. Please review the store documentation for more options. If you’re deploying with LangGraph Platform, the platform will provide a production-ready store for you. - For this example, we write some sample data to the store using the
put
method. Please see the BaseStore.put API reference for more details. - The first argument is the namespace. This is used to group related data together. In this case, we are using the
users
namespace to group user data. - A key within the namespace. This example uses a user ID for the key.
- The data that we want to store for the given user.
- The
get_store
function is used to access the store. You can call it from anywhere in your code, including tools and prompts. This function returns the store that was passed to the agent when it was created. - The
get
method is used to retrieve data from the store. The first argument is the namespace, and the second argument is the key. This will return aStoreValue
object, which contains the value and metadata about the value. - The
store
is passed to the agent. This enables the agent to access the store when running tools. You can also use theget_store
function to access the store from anywhere in your code.
Write long-term memory from tools
of a tool that updates user information"
- The
InMemoryStore
is a store that stores data in memory. In a production setting, you would typically use a database or other persistent storage. Please review the store documentation for more options. If you’re deploying with LangGraph Platform, the platform will provide a production-ready store for you. - The
UserInfo
class is aTypedDict
that defines the structure of the user information. The LLM will use this to format the response according to the schema. - The
save_user_info
function is a tool that allows an agent to update user information. This could be useful for a chat application where the user wants to update their profile information. - The
get_store
function is used to access the store. You can call it from anywhere in your code, including tools and prompts. This function returns the store that was passed to the agent when it was created. - The
put
method is used to store data in the store. The first argument is the namespace, and the second argument is the key. This will store the user information in the store. - The
user_id
is passed in the config. This is used to identify the user whose information is being updated.