Build LangChain applications that remember context across sessions. Synap handles memory ingestion, retrieval, and per-user scoping; LangChain handles your chain or agent.Documentation Index
Fetch the complete documentation index at: https://docs.maximem.ai/llms.txt
Use this file to discover all available pages before exploring further.
Overview
This guide shows how to add Synap to a LangChain application to build agents that:- Maintain conversation history across sessions and processes
- Retrieve user- and organization-scoped context as part of a RAG pipeline
- Decide for themselves when to search or store memories
| Component | LangChain interface | Purpose |
|---|---|---|
SynapChatMessageHistory | BaseChatMessageHistory | Persistent chat history per conversation_id |
SynapCallbackHandler | BaseCallbackHandler | Auto-records every LLM turn — no application changes |
SynapRetriever | BaseRetriever | Semantic retriever that returns Synap memories as Documents |
SynapSearchTool, SynapStoreTool | BaseTool | Agent-callable tools for explicit memory read/write |
Setup
Install the package alongside LangChain and your model provider:.env
Basic integration
The smallest useful integration is a chain with automatic memory viaSynapCallbackHandler. Every turn is ingested without changing your chain logic:
ERROR level and never propagate to your chain — your application keeps running even if Synap is unreachable.
This is the smallest viable setup. To make the model aware of memory at inference time, combine the callback with SynapChatMessageHistory and/or SynapRetriever below.
Core concepts
Persistent chat history
SynapChatMessageHistory implements LangChain’s BaseChatMessageHistory interface. Wrap any chain with RunnableWithMessageHistory to give it conversation memory that survives restarts:
session_id maps one-to-one to a Synap conversation_id. Messages are persisted on every turn and replayed on the next invocation.
Automatic ingestion
SynapCallbackHandler covers a different need: it does not feed history into the model, it records every turn for long-term memory. Use it whenever you want a chain’s output to enrich the user’s profile and become searchable later.
chain.with_config(callbacks=[handler]).
Semantic retrieval
SynapRetriever implements BaseRetriever. Use it inside ConversationalRetrievalChain, create_retrieval_chain, or any RAG pipeline that expects a retriever:
fast | accurate | |
|---|---|---|
| Latency | 50-100ms | 200-500ms |
| Search | Vector similarity | Vector + graph + re-ranking |
| Best for | Real-time chat | Multi-entity queries |
Agent-callable memory
For agent-style chains where the model decides when memory is relevant, exposeSynapSearchTool and SynapStoreTool:
synap_search and synap_store in the model’s tool-calling schema, with descriptions that nudge the model toward calling them when context is needed.
Complete example: support assistant with memory
The following class assembles all four components into a single agent. It remembers conversation history, retrieves user-specific context, and lets the model store new facts explicitly.SynapChatMessageHistorykeeps the chain itself stateful turn-to-turn.SynapRetrieverinjects per-user long-term context as a system message — independent of the chat history.SynapCallbackHandlerpersists each turn back to Synap so future invocations can retrieve it.
Advanced patterns
Multi-tenant scoping
Every component accepts the same scoping triple —user_id, optional customer_id, and optional conversation_id:
customer_id is required for B2B Synap instances and ignored on single-tenant instances. See Memory Scopes for the full hierarchy.
Combining automatic and explicit memory
The four components compose freely. A common production setup:SynapChatMessageHistoryfor the running conversation bufferSynapCallbackHandlerfor long-term ingestion (runs in parallel with history)SynapRetrieverinjected as a system message before each turnSynapSearchToolexposed to the model for explicit lookups when retrieval misses
Tuning retrieval mode per call
SynapRetriever accepts a default mode at construction time, but the underlying sdk.conversation.context.fetch() call accepts a mode override too. For a single high-recall query inside an otherwise low-latency agent, swap the retriever’s mode temporarily:
Failure semantics
The Synap integration follows the Synap-wide contract:- Retrieval failures degrade gracefully —
SynapRetrieverreturns[]and logs an error - Callback failures degrade gracefully —
SynapCallbackHandlerlogs anERRORand your chain continues - Explicit tool calls surface failures —
SynapSearchTool/SynapStoreToolraiseSynapIntegrationErrorso the model can react
Next steps
LangGraph
Checkpointer and cross-thread store for LangGraph state graphs.
Context Fetch
The retrieval API that powers
SynapRetriever — fast vs accurate, scopes, and response shapes.Ingestion
Direct ingestion API for custom pipelines that need finer control than the callback handler.
Memory Scopes
How
user_id, customer_id, and conversation_id interact across retrievals.