MemorySync
Getting Started

LangChain Memory

Give any LangChain chain or agent persistent memory with two packages: langchain-memorysync for Python and memorysync-langchain for JavaScript. Both work on langchain-core 0.3.x and 1.x.

What the integration provides

PieceWhat it does
MemorySyncChatMessageHistoryA BaseChatMessageHistory implementation, so RunnableWithMessageHistory persists transcripts in MemorySync instead of process memory.
Context providerOne call that returns a grouped, prompt-ready context block from hierarchical recall, for personalising a system prompt.
Agent toolsFive structured memory tools for agents, covered in LangChain Tools.

Install

pip install langchain-memorysync

Set MEMORYSYNC_API_KEY in the environment, or pass the key explicitly. Keys come from the dashboard or npx memorysync-cli init.

Persist chat history

Wrap any runnable with RunnableWithMessageHistory and hand it a MemorySync-backed history per session. Every turn is written at the moment it happens and read back in order, so a crashed process resumes mid-conversation.

from langchain_core.runnables.history import RunnableWithMessageHistory
from langchain_memorysync import MemorySyncChatMessageHistory
chain_with_history = RunnableWithMessageHistory(
chain,
lambda session_id: MemorySyncChatMessageHistory(
session_id=session_id,
user_id="user-123",
),
input_messages_key="input",
history_messages_key="history",
)
chain_with_history.invoke(
{"input": "My name is Ada."},
config={"configurable": {"session_id": "thread-42"}},
)

Turns are stored verbatim through episodic ingestion, so short answers like "yes" survive rather than being filtered as low-value. The full LangChain message — tool calls included — is serialised into metadata and reconstructed exactly on read.

Sessions and users

ParameterMeaning
session_idThe conversation thread. Reads and clear() touch only this thread.
user_idThe end user the turns belong to. Defaults to the session id; pass a stable user id to share memory across that user’s sessions.
max_messagesCap on how many trailing messages a read returns, so a long transcript cannot blow the prompt budget. Writes are unaffected.
read_onlyBlocks writes and clear() for safe transcript inspection.

Prompt-ready recall context

For personalisation beyond the current thread, the context provider returns a grouped, type-labelled block built by hierarchical recall — ready to place in a system prompt. It returns an empty string when the user has no relevant memories, because a new user is a normal state, not an error.

from langchain_memorysync import MemorySyncContextProvider
provider = MemorySyncContextProvider(user_id="user-123")
context = provider.get_context("What should I cook tonight?")
prompt = f"Relevant memories:\n{context}\n\nAnswer the user."

Where to go next

Was this page helpful?