LangMem is an open-source Python SDK developed by LangChain to provide AI agents and LangGraph workflows with long-term memory. It allows agents to learn, adapt, and maintain behavioral consistency across sessions by automatically extracting, storing, and consolidating semantic facts, episodic interaction summaries, and procedural execution patterns.
graph TD
Interaction[User & Agent Interaction] --> Collector[Conversation Collector]
Collector --> Extractor[Background LLM Extractor & Consolidator]
Extractor --> SemanticMem[Semantic Memory<br/>Facts & User Profile]
Extractor --> EpisodicMem[Episodic Memory<br/>Past Task Summaries]
Extractor --> ProceduralMem[Procedural Memory<br/>Rules & System Directives]
SemanticMem & EpisodicMem & ProceduralMem --> BaseStore[(LangGraph BaseStore / Persistent DB)]
BaseStore --> Tools[Hot-Path Recall & Manage Tools]
Tools --> Agent[LangGraph Agent Graph]
LangMem structures agent memory into three primary operational categories:
BaseStore), running seamlessly with PostgreSQL, SQLite, or in-memory stores.Install LangMem:
pip install -U langmem
Example usage extracting and retrieving user facts:
from langmem import create_memory_store, create_memory_manager
# 1. Create a memory store instance
store = create_memory_store()
# 2. Create memory manager configured for semantic extraction
manager = create_memory_manager(
"anthropic:claude-3-5-sonnet-latest",
store=store,
namespace=("users", "user_42", "memories")
)
# 3. Process conversation turns and extract facts
conversation = [
{"role": "user", "content": "I live in Berlin and always test using pytest."},
{"role": "assistant", "content": "Understood! I'll use pytest for your tests."}
]
manager.invoke(conversation)
# 4. Search extracted memories
memories = store.search(("users", "user_42", "memories"), query="testing framework")
for mem in memories:
print(mem.value)