LangChain and LangGraph
Use PowerMem as a persistent memory layer for LangChain and LangGraph applications.
Prerequisites
- Python 3.11+.
- PowerMem configured with your LLM provider, API key, and model.
- LangChain or LangGraph dependencies for your app.
Install
For LangChain:
pip install powermem langchain langchain-core langchain-openai
For LangGraph:
pip install powermem langgraph langchain-core langchain-openai
For the runnable healthcare support example:
cd examples/langchain
pip install -r requirements.txt
LangChain pattern
PowerMem is used beside LangChain as a durable retrieval and write-back layer:
- Create a PowerMem
Memoryinstance. - Search PowerMem before each response to load relevant context.
- Inject the retrieved context into a LangChain prompt or LCEL chain.
- Save the user/assistant exchange back into PowerMem.
Minimal shape:
from powermem import Memory, auto_config
memory = Memory(config=auto_config())
context = memory.search(
query="what does the user prefer",
user_id="user123",
limit=5,
)
memory.add(
"User prefers concise answers with examples.",
user_id="user123",
)
For a full LCEL implementation, see examples/langchain/README.md and ../examples/scenario_5_custom_integration.md.
LangGraph pattern
PowerMem works well as a graph node or helper in LangGraph workflows:
- Add a load-context node that searches PowerMem.
- Add the retrieved memories to graph state.
- Generate the response with state-aware context.
- Add a save-memory node after the response.
See the LangGraph section in ../guides/0009-integrations.md for a complete example.
Verify
- Add a probe memory for a test
user_id. - Search for the probe before invoking the chain or graph.
- Confirm the retrieved memory is included in the generated prompt/context.
- Save a new exchange and search it back.
Troubleshooting
- If search returns no results, confirm the same
user_idis used for writes and searches. - If extraction creates no memory, check LLM provider configuration and logs.
- If LangChain imports fail, install the packages listed in
examples/langchain/requirements.txt. - If embeddings fail, confirm your embedding provider and dimensions match the configured storage backend.