memorysync-langchain
v1.1.0
Published
MemorySync integration for LangChain.js and LangGraph.js: chat message history, memory tools, recall context, and a LangGraph store.
Maintainers
Readme
memorysync-langchain
MemorySync integration for LangChain.js: persistent chat message history, structured memory tools for agents, and prompt-ready recall context. Works with @langchain/core 0.3.x and 1.x.
npm install memorysync-langchain @langchain/coreSet MEMORYSYNC_API_KEY (create a key at memorysync.io,
or run npx memorysync-cli init).
Chat message history
MemorySyncChatMessageHistory extends BaseListChatMessageHistory, so it
plugs straight into RunnableWithMessageHistory:
import { RunnableWithMessageHistory } from "@langchain/core/runnables";
import { MemorySyncChatMessageHistory } from "memorysync-langchain";
const chainWithHistory = new RunnableWithMessageHistory({
runnable: chain,
getMessageHistory: (sessionId) => new MemorySyncChatMessageHistory({ sessionId }),
inputMessagesKey: "input",
historyMessagesKey: "history",
});
await chainWithHistory.invoke(
{ input: "My name is Ada." },
{ configurable: { sessionId: "thread-42" } },
);Turns are stored verbatim through MemorySync's episodic ingestion — no extraction gates, so short turns like "yes" survive — and the full LangChain message (tool calls, kwargs) is serialised into metadata for exact reconstruction. Writes are idempotent: a retried write is recognised, never duplicated. Plain-text transcripts use the same stored shape as the Python integration, so both SDKs can read them.
const history = new MemorySyncChatMessageHistory({
sessionId: "thread-42",
userId: "customer-7", // share memory across a user's sessions
maxMessages: 30, // cap the transcript tail handed to the LLM
});Agent tools
import { createMemorySyncTools } from "memorysync-langchain";
const tools = createMemorySyncTools({ endUserId: "customer-7" });
// langchain 1.x
import { createAgent } from "langchain";
const agent = createAgent({ model, tools });Returns add_memory, search_memory, list_memories, update_memory, and
delete_memory. Tools return readable strings and never throw, so a memory
failure cannot abort an agent run. Pass readOnly: true to hand an agent only
search_memory and list_memories.
add_memory derives an idempotency key from the content — an agent that
repeats itself gets "already stored", not a duplicate.
Recall context
import { MemorySyncContextProvider } from "memorysync-langchain";
const provider = new MemorySyncContextProvider({ userId: "customer-7" });
const context = await provider.getContext("What should I cook tonight?");Returns a grouped, type-labelled context block built by MemorySync's hierarchical retrieval, ready to inject into a system prompt. Empty string — not an exception — when the user has no relevant memories yet.
Documentation
Full guide: docs.memorysync.io/integrations/langchain
