Skip to documentation
Docs navigation
Docs/Add memory to a TypeScript chatbot in 5 minutes
Quick start

Add memory to a TypeScript chatbot in 5 minutes.

Install MemoGrafter, initialize the project files, migrate the database, and run a minimal MemoGrafterAgent.

Minimal agent

Use MemoGrafterAgent when you want the simplest chatbot-facing API. It handles invoke-time recall, response generation, and background ingestion.

src/index.ts
import "dotenv/config";
import {
  MemoGrafterAgent,
  OpenAIEmbedAdapter,
  OpenAILLMAdapter,
} from "memo-grafter";
// Create one agent for the chatbot session or workflow you want to remember.
const agent = new MemoGrafterAgent({
  // MemoGrafter stores graph memory in PostgreSQL.
  db: { connectionString: process.env.DATABASE_URL! },
  // The LLM generates responses; the embedder makes memory searchable by meaning.
  llm: new OpenAILLMAdapter("gpt-4o"),
  embedder: new OpenAIEmbedAdapter("text-embedding-3-small"),
});
// Prepare adapters, storage, and any pending graph state.
await agent.initialize();
// invoke() answers now and schedules background ingestion for memory later.
await agent.invoke("I am planning a Japan trip.");
await agent.invoke("I like quiet towns, bookstores, and local cafes.");
// recall() retrieves relevant structured facts from the memory graph.
const recall = await agent.recall("travel preferences");
console.log(recall.facts);
// Close database/provider resources during graceful shutdown.
await agent.close();

What happens

invoke() answers the current user message using recent raw history and any available recalled memory.
Background ingestion turns conversation turns into topic segments, topic nodes, memory nodes, and graph edges.
recall() retrieves relevant atomic facts later by meaning, confidence, lifecycle state, and token budget.
invoke()
Recall facts
LLM response
Background ingest
Graph memory