Cloudflare D1 Memory
D1MemoryAdapter stores conversations in Cloudflare D1 for Workers deployments.
Installation
- npm
- yarn
- pnpm
npm install @voltagent/cloudflare-d1
yarn add @voltagent/cloudflare-d1
pnpm add @voltagent/cloudflare-d1
Cloudflare D1 binding
Add a D1 binding to your wrangler.toml:
[[d1_databases]]
binding = "DB"
database_name = "voltagent"
database_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
Configuration (Workers)
import { openai } from "@ai-sdk/openai";
import { Agent, Memory, VoltAgent } from "@voltagent/core";
import { D1MemoryAdapter } from "@voltagent/cloudflare-d1";
import { serverlessHono } from "@voltagent/serverless-hono";
import type { D1Database } from "@cloudflare/workers-types";
import { weatherTool } from "./tools";
type Env = {
DB: D1Database;
OPENAI_API_KEY: string;
};
const createWorker = (env: Env) => {
const memory = new Memory({
storage: new D1MemoryAdapter({
binding: env.DB,
tablePrefix: "voltagent_memory",
}),
});
const agent = new Agent({
name: "Assistant",
model: openai("gpt-4o-mini"),
tools: [weatherTool],
memory,
});
const voltAgent = new VoltAgent({
agents: { agent },
serverless: serverlessHono(),
});
return voltAgent.serverless().toCloudflareWorker();
};
let cached: ReturnType<typeof createWorker> | undefined;
export default {
fetch: (request: Request, env: Env, ctx: ExecutionContext) => {
if (!cached) {
cached = createWorker(env);
}
return cached.fetch(request, env, ctx);
},
};
Configuration Options
| Option | Type | Description |
|---|---|---|
binding | D1Database | Worker D1 binding (required) |
tablePrefix | string | Table name prefix (default: voltagent_memory) |
maxRetries | number | Retry count for busy/locked DB operations (default: 3) |
retryDelayMs | number | Initial backoff delay in ms (default: 100) |
debug | boolean | Enable debug logging (default: false) |
logger | Logger | Optional logger for structured logging |
Features
Automatic Schema Creation
Tables are created automatically on first use:
${tablePrefix}_users${tablePrefix}_conversations${tablePrefix}_messages${tablePrefix}_workflow_states${tablePrefix}_steps
Conversation Storage
- Messages stored per
userIdandconversationId - All
StorageAdaptermethods supported - No automatic message pruning - all messages are preserved
Working Memory
Supports both conversation and user-scoped working memory:
import { z } from "zod";
const memory = new Memory({
storage: new D1MemoryAdapter({ binding: env.DB }),
workingMemory: {
enabled: true,
scope: "conversation", // or "user"
schema: z.object({
preferences: z.array(z.string()).optional(),
}),
},
});
See Working Memory for configuration details.
Semantic Search (Optional)
D1 provides storage only. To enable semantic search, pair it with an embedding + vector adapter (for example, AiSdkEmbeddingAdapter and InMemoryVectorAdapter).
See Semantic Search for usage.
Learn More
- Working Memory - Maintain compact context
- Semantic Search - Vector search configuration