Skip to main content
For the full source code and advanced implementation details, see the official LangChain Integration repository section..

Overview

Integrating MemMachine with LangChain implements the BaseMemory interface, allowing you to use MemMachine as a persistent memory backend. This enables:
  • Persistent Memory: Conversations and context persist across multiple sessions.
  • Semantic Search: Retrieve relevant memories based on semantic similarity.
  • Context Scoping: Automatic filtering by user_id, agent_id, and session_id.
  • Episodic & Semantic Memory: Access to both conversation history and extracted knowledge.

Configuration

The integration can be managed via environment variables or constructor parameters.
1

Install Dependencies

Install the core LangChain framework along with the MemMachine client:
2

Initialize MemMachine Memory

Initialize the memory class with your project configuration.
3

Integrate with a Chain

Pass the memory instance to your LangChain ConversationChain or LLMChain.

Advanced Usage

Custom Prompt with Memory Context

You can use a PromptTemplate to explicitly include both conversation history and semantic facts (extracted context).

Direct Memory Operations

For granular control, you can interface with the underlying memory storage directly.
Pro Tip: Use the search_limit parameter (default: 10) in the constructor to control the number of memory fragments retrieved during each interaction.

Requirements

  • MemMachine Server: Must be reachable at the MEMORY_BACKEND_URL.
  • LLM API Key: An OPENAI_API_KEY (or equivalent) must be configured in your environment or within the MemMachine configuration.yml.
  • Python: 3.10 or higher.
  • Framework: langchain and memmachine.