LangChain

@db0-ai/langchain replaces LangChain's deprecated memory classes (BufferMemory, ConversationSummaryMemory) with persistent, scoped memory. Works locally, no platform dependencies.

npm · GitHub

Install

npm i @db0-ai/langchain

Quick start

import { createDb0 } from "@db0-ai/langchain"
import { ChatAnthropic } from "@langchain/anthropic"
import { createReactAgent } from "@langchain/langgraph/prebuilt"

const memory = await createDb0({ dbPath: "./agent.db" })

const agent = createReactAgent({
  llm: new ChatAnthropic({ model: "claude-sonnet-4-20250514" }),
  tools: memory.tools,
})

await agent.invoke({
  messages: [{ role: "user", content: "My name is Alex." }],
})

Two usage patterns

Agent tools

Include memory.tools in any ReAct agent. The model decides when to read and write memories.

const memory = await createDb0()

const agent = createReactAgent({
  llm: new ChatAnthropic({ model: "claude-sonnet-4-20250514" }),
  tools: [...yourTools, ...memory.tools],
})

The tools array contains three DynamicStructuredTool instances:

Tool Description
db0_memory_write Write a fact with content, scope, and optional tags
db0_memory_search Search memories by query text
db0_memory_list List memories, optionally filtered by scope

Chat history

Use memory.chatHistory as a drop-in replacement for LangChain's ChatMessageHistory. It stores messages in db0's scoped memory and automatically extracts facts from every message.

const memory = await createDb0({ extractFacts: true })

// Use with RunnableWithMessageHistory
const chain = new RunnableWithMessageHistory({
  runnable: yourChain,
  getMessageHistory: () => memory.chatHistory,
})

Db0ChatMessageHistory implements BaseListChatMessageHistory:

Method Description
getMessages() Return all stored messages
addMessage(msg) Store a message and extract facts
addUserMessage(text) Store a user message
addAIMessage(text) Store an AI message
clear() Clear message history

Configuration

const memory = await createDb0({
  dbPath: "./agent.db",      // default: ./db0.sqlite
  agentId: "my-agent",       // default: langchain
  userId: "user-123",        // default: "default"
  extractFacts: true,        // auto-extract from chat history
})

Session management

Create a new session (fresh conversation, same memories):

const { harness, chatHistory } = memory.newSession("session-2")

Migration from BufferMemory

If you're using LangChain's deprecated BufferMemory:

// Before
const memory = new BufferMemory()
const chain = new ConversationChain({ llm, memory })

// After
const db0 = await createDb0()
const agent = createReactAgent({
  llm,
  tools: db0.tools,
})

The key difference: BufferMemory only lasts for the current process. db0 memories persist in SQLite and survive across sessions, restarts, and deployments.