Integrations/Vercel AI SDK
@db0-ai/ai-sdknpmGitHub

Persistent memory for the
Vercel AI SDK

The AI SDK is stateless. Every call starts from zero. db0 adds a memory middleware that injects relevant context before each LLM call and extracts facts after — with zero extra LLM calls.

Install
npm i @db0-ai/ai-sdk

The problem

Every call starts from scratch
The AI SDK has no built-in persistence. User preferences, names, and context vanish between requests.
Users repeat themselves
Without memory, agents ask the same questions across sessions. Users notice and lose trust.
DIY memory is fragile
Building memory on top of the AI SDK means managing storage, extraction, and injection yourself.

Three ways to use it

Pick the level of control you need. All three use the same underlying db0 harness and storage.

Middleware (automatic, invisible)

Memory is invisible to the model. Facts are extracted from user messages and assistant responses automatically, and relevant memories are injected into the system prompt.

import { createDb0 } from "@db0-ai/ai-sdk"
import { generateText } from "ai"
import { anthropic } from "@ai-sdk/anthropic"

const memory = await createDb0()

const result = await generateText({
  model: anthropic("claude-sonnet-4-20250514"),
  middleware: memory.middleware,
  prompt: "My name is Alex, I work on backend systems.",
})
Tools (agent-controlled)

The model decides when to read and write memories via three tool calls: db0_memory_write, db0_memory_search, db0_memory_list.

import { createDb0 } from "@db0-ai/ai-sdk"
import { generateText } from "ai"
import { anthropic } from "@ai-sdk/anthropic"

const memory = await createDb0()

const result = await generateText({
  model: anthropic("claude-sonnet-4-20250514"),
  tools: memory.tools,
  prompt: "What do you know about me?",
})
Both (middleware + tools)

Middleware handles background extraction. Tools give the model explicit control for intentional saves.

const result = await generateText({
  model: anthropic("claude-sonnet-4-20250514"),
  middleware: memory.middleware,
  tools: memory.tools,
  prompt: "Save this: I'm migrating from Express to Hono.",
})

How the middleware works

Two hooks in the AI SDK lifecycle. No extra LLM calls.

User message
  │
  ├─ 1. transformParams (before LLM call)
  │    ├─ Extract facts from user message
  │    ├─ Search for relevant memories
  │    └─ Inject into system prompt (within token budget)
  │
  ▼
LLM generates response
  │
  ├─ 2. wrapGenerate (after LLM call)
  │    └─ Extract facts from assistant response
  │
  ▼
Facts stored in SQLite/PostgreSQL

What you get

Works with any provider
Anthropic, OpenAI, Google, Mistral, Cohere — the middleware wraps the model, not the provider.
Zero LLM calls for memory
Extraction is rules-based. No extra API costs, no latency from memory operations.
Scoped memory
4 scopes (task/session/user/agent) with different lifetimes. User facts persist forever, session context expires.
Automatic superseding
When facts change, old versions are marked superseded. Your agent always sees the latest state.
Token-budgeted injection
Memories are packed into the system prompt within a configurable token budget. No context overflow.
Session management
Call newSession() to start a fresh conversation while keeping all accumulated memories.

Configuration

OptionDefaultDescription
dbPath./db0.sqliteSQLite file path for local storage
backend(none)Pass a PostgreSQL backend for production/edge deployments
agentIdai-sdkAgent identifier for scoping memories
userIddefaultUser identifier for per-user memory isolation
tokenBudget1500Max tokens for memory injection into system prompt
extractOnResponsetrueAutomatically extract facts from assistant responses

One line.
Your AI SDK app remembers.

$ npm i @db0-ai/ai-sdk