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 memory subagent or raw file tools.
- Read Memory scopes before writing paths like
user/profile.md.
Setup
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.
Examples
Working Python examples with hot path, raw tool, and background path patterns: