Key Differences Between LangGraph’s 'State' and 'Memory' Components
This article clarifies the distinct roles of LangGraph’s State and Memory components: State is the dynamic runtime context for graph invocations, while...
Direct answer
The key difference between LangGraph’s State and Memory lies in their scope and purpose: State is the dynamic runtime context for each graph invocation, holding dialogue history and intermediate results, while Memory includes short-term (thread-scoped, part of State) and long-term (cross-session, stored in dedicated stores) components to retain user interactions. State is updated via node patches during execution, whereas Memory is designed for persistent recall across sessions or threads.
For developers building stateful AI agents with LangGraph, understanding the difference between State and Memory is critical to creating efficient, context-aware systems. Though both handle data retention, their purposes, scope, and persistence methods vary significantly.
LangGraph’s Memory system preserves prior interaction data, enabling agents to learn from feedback and adapt to user preferences. It has two types: short-term (thread-scoped) and long-term (cross-session/cross-thread). Short-term memory is part of the agent’s State, while long-term memory uses LangGraph’s dedicated stores for persistence.
State acts as the graph’s dynamic runtime context during an invocation. It serves as short-term memory, with a lifecycle tied to each graph run. This shared structure holds dialogue history, intermediate tool/LLM results, and derived values. Nodes pass State between them, and each node writes a "delta" patch to update it—merged by the runtime into a new State. State can be saved to a database, allowing threads to resume by reloading it at step start.
Short-term memory (thread-scoped) tracks ongoing conversations via message history and related data (files, retrieved docs). It’s part of State and persisted via thread checkpoints. Long-term memory stores cross-session user/app data, shared across threads, and can be recalled using custom namespaces (not limited to a single thread ID).
Sources
FAQ
- What is the role of State in LangGraph?
- State serves as the dynamic runtime context for each graph invocation, storing dialogue history, intermediate results from tools/LLMs, and other session-specific data. It is shared between nodes and updated with patches after each node runs, enabling seamless data flow in the graph.
- How do short-term and long-term memory differ in LangGraph?
- Short-term memory is thread-scoped (tied to a single conversation session) and managed as part of the State, using checkpoints for persistence. Long-term memory is cross-session/cross-thread, stored in LangGraph’s dedicated stores, and can be recalled using custom namespaces.
- Can LangGraph’s State be saved and resumed later?
- Yes, State can be persisted to a database as a checkpoint. This allows threads to resume at any time, as the State is read at the start of each step to restore the previous context.