Skip to main content

LangChain + Memanto

LangChain Add persistent, cross-session memory to your LangChain agents and chains using Memanto. LangChain built-in memory classes reset between runs. Memanto plugs in as a custom memory backend that stores and retrieves context semantically so your chains remember what matters, even days later.

How It Works

You drop MemantoMemory in wherever LangChain expects a BaseMemory. It handles session activation, storing new messages, and injecting recalled context into your prompts.
The Moorcheh API key (MOORCHEH_API_KEY) is configured on the Memanto server, not in your LangChain code. The only credential the client sends is X-Session-Token.

Prerequisites

  • Python 3.8+
  • Moorcheh API key configured on the Memanto server
  • Memanto server running locally

Install

Step 1: Start Memanto Server

Step 2: Create the Memory Class

Create memanto_memory.py:

Step 3: Use in a Chain

Create agent.py:

Step 4: Run

Using with LCEL (LangChain Expression Language)

Inject recalled memory directly into an LCEL pipeline:

Using Memanto’s Built-in Answer (Optional)

For cases where you want a direct, grounded response from memory without routing through your chain, Memanto exposes an answer endpoint that uses its native RAG model. No external LLM call is made on your side. This is useful as a quick lookup tool — for example, answering a simple factual question about a user before deciding whether to invoke the full chain.
You can also use this inside an LCEL chain as a conditional step — call memanto_answer first, and only invoke the full LLM if the memory answer is empty:
When to use answer vs recall
  • Use recall (via load_memory_variables) when your LLM should reason over the raw memories itself.
  • Use answer when you want a ready-made response from memory, or to short-circuit the chain for simple factual lookups.

Persistent Memory Across Sessions

Memories stored via save_context survive process restarts and are available in future sessions for the same agent_id:

Next Steps