new·Earn with mozg — 20% of every monthSend somebody here and take a fifth of every plan payment they make, for as long as they keep paying — not a bounty on the first invoice. Your handle is the link, the window is thirty days, and the commission lands on your balance the second they pay. Free to join: if you have signed in, you already have the link. mozg.sh/earnall news →
mozg.beta
Sign in

LangChain · Agents · all subjects

agent state & memory

11 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Long-term memory definition and scope

Long-term memory lets agents store and recall information across different conversations and sessions. Unlike short-term memory, which is scoped to a single thread, long-term memory persists across threads and can be recalled at any time.

Long-term memory storage architecture

Long-term memory is built on LangGraph stores, which save data as JSON documents organized by namespace and key. 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 make it easier to organize information. This structure enables hierarchical organization of memories, and cross-namespace searching is supported through content filters.

Adding long-term memory to an agent

To add long-term memory to an agent, create a store and pass it to the create_agent function. Tools can then read from and write to the store using the runtime.store parameter.

Long-term memory store backends available

LangChain supports multiple store backends for long-term memory including InMemoryStore, PostgreSQL, Redis, and MongoDB. For PostgreSQL, install langgraph-checkpoint-postgres and psycopg[binary]. For JavaScript/Node, install @langchain/langgraph-checkpoint-postgres. Other backends are available through store integrations.

PostgreSQL setup for long-term memory

To use PostgreSQL for long-term memory, install langgraph-checkpoint-postgres with psycopg[binary]. The langgraph-checkpoint-postgres package by default installs psycopg (Psycopg 3) without extras, but psycopg[binary] is recommended for most users. For JavaScript environments, install @langchain/langgraph-checkpoint-postgres via npm.

Reading and writing long-term memory in tools

Tools can read from and write to long-term memory using the runtime.store parameter. LangChain provides separate patterns for reading memory from tools and writing memory to tools, supporting both InMemoryStore and PostgreSQL backends.

Three types of memory in agents

According to the conceptual guide referenced, there are three types of memory: semantic memory, episodic memory, and procedural memory. The documentation includes a Memory conceptual guide that covers these types and strategies for writing memories.

Agent memory with checkpointer

Add memory to agents using a checkpointer to maintain state across interactions. Python: use `InMemorySaver()` from `langgraph.checkpoint.memory`. JavaScript: use `MemorySaver` from `@langchain/langgraph`. Pass the checkpointer to `create_agent()` or `create_deep_agent()` with the `checkpointer` parameter. When invoking the agent, set `config={"configurable": {"thread_id": "unique-id"}}` to enable memory per thread. For production, use persistent checkpointers that save to a database.

Access state in tools with ToolRuntime

Add runtime: ToolRuntime parameter to tool signature to access state. At call time, ToolNode injects the value automatically; the parameter is not included in the tool schema sent to the model. Use runtime.state to read current conversation state including message history and custom fields.

Update state from tools with Command

Use @Command to update the agent's state from within a tool. Include a ToolMessage in the update so the model can see the result of the tool call. When tools update state variables, define a reducer for those fields to handle conflicts when multiple tools update the same field in parallel.

Store provides long-term persistent memory

The @BaseStore provides persistent storage that survives across conversations, unlike state which is short-term. Access the store through runtime.store. The store uses a namespace/key pattern to organize data. For production, use persistent implementations like @PostgresStore, MongoDBStore, or RedisStore instead of InMemoryStore.

Give your agent this brain