Adapters
LangChain
Use MemexAI memory tools with LangChain in TypeScript and Python.
LangChain adapters are available for both the TypeScript and Python SDKs.
Before you start
- Choose service mode or direct Postgres mode.
- Read How MemexAI works for the tool-call-to-Postgres flow.
- Read Prompt block to understand why memory must be included in the system prompt.
- Read Memory tools to choose agentic tools or raw file tools.
- Read Memory scopes before writing paths like
user/profile.md.
TypeScript
Install
npm install @memexai/sdk langchain @langchain/coreFor direct Postgres mode:
npm install @memexai/core langchain @langchain/coreService mode
import { MemexAI } from '@memexai/sdk'
import { createLangChainTools } from '@memexai/sdk/adapters/langchain'
import { AgentExecutor, createToolCallingAgent } from 'langchain/agents'
import { ChatGoogleGenerativeAI } from '@langchain/google-genai'
import { ChatPromptTemplate } from '@langchain/core/prompts'
const memex = new MemexAI({ url: 'http://localhost:8080', apiKey: process.env.MEMEX_API_KEY! })
const memory = memex.forUser({ userId: 'user_123', actor: 'assistant' })
const tools = createLangChainTools(memory)
const system = await memory.getSystemPrompt('You are a helpful assistant with durable user memory.')
const llm = new ChatGoogleGenerativeAI({ model: 'gemini-2.5-flash' })
const prompt = ChatPromptTemplate.fromMessages([
['system', system],
['human', '{input}'],
['placeholder', '{agent_scratchpad}'],
])
const agent = await createToolCallingAgent({ llm, tools, prompt })
const executor = new AgentExecutor({ agent, tools })
const result = await executor.invoke({ input: 'Remember I prefer quiet neighborhoods.' })
console.log(result.output)Direct Postgres mode
import { createMemex } from '@memexai/core'
import { createLangChainTools } from '@memexai/core/adapters/langchain'
const memex = createMemex({ databaseUrl: process.env.DATABASE_URL! })
await memex.migrate()
const memory = memex.forUser({ userId: 'user_123', actor: 'assistant' })
const tools = createLangChainTools(memory)
const system = await memory.getSystemPrompt('You are a helpful assistant with durable user memory.')Use system in the LangChain prompt template alongside the tools.
Python
Install
pip install memexai langchain langchain-google-genaiUsage
from memexai import MemexAI
from memexai.adapters.langchain import get_langchain_tools
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain.agents import AgentExecutor, create_tool_calling_agent
from langchain_core.prompts import ChatPromptTemplate
memex = MemexAI(url="http://localhost:8080", api_key="dev-agent-key")
user = memex.for_user("user_123", actor="assistant")
tools = get_langchain_tools(user)
system = await user.get_system_prompt("You are a helpful assistant with durable user memory.")
llm = ChatGoogleGenerativeAI(model="gemini-2.5-flash")
prompt = ChatPromptTemplate.from_messages([
("system", system),
("human", "{input}"),
("placeholder", "{agent_scratchpad}"),
])
agent = create_tool_calling_agent(llm, tools, prompt)
executor = AgentExecutor(agent=agent, tools=tools)
result = await executor.ainvoke({"input": "Remember I prefer quiet neighborhoods."})
print(result["output"])
await memex.close()The Python adapter exposes all seven memory tools: memory_list, memory_read, memory_write, memory_patch, memory_smart_read, memory_search, and memory_memorize. For most assistants, start with memory_memorize and memory_search; see Memory tools for the tradeoff.