Get started
This guide walks you through adding persistent memory to a Cloudflare Worker. By the end, your Worker will be able to ingest conversations, recall stored knowledge, and generate profile summaries.
Before you begin, make sure you have:
- A Cloudflare account ↗
- Node.js ↗ v18 or later installed
- Wrangler installed and authenticated
A namespace is a container that scopes your memory profiles. Create one using the Cloudflare API:
curl -X POST "https://api.cloudflare.com/client/v4/accounts/{account_id}/agent-memory/namespaces" \ -H "Authorization: Bearer {api_token}" \ -H "Content-Type: application/json" \ -d '{"name": "my-app"}'The response includes the namespace name that you will use to configure your Worker binding.
{ "result": { "namespace_id": "01JSGC...", "name": "my-app", "created_at": "2026-04-21T00:00:00Z" }, "success": true, "errors": [], "messages": []}Create a new Worker project using Wrangler:
npx wrangler init memory-demoSelect the default options when prompted.
Add the agent_memory binding to your Wrangler configuration. Replace the namespace value with the namespace name from step 1.
{ "name": "memory-demo", "main": "src/index.ts", // Set this to today's date "compatibility_date": "2026-04-22", "agent_memory": [ { "binding": "MEMORY", "namespace": "<NAMESPACE_NAME>" } ]}name = "memory-demo"main = "src/index.ts"# Set this to today's datecompatibility_date = "2026-04-22"
[[agent_memory]]binding = "MEMORY"namespace = "<NAMESPACE_NAME>"Replace the contents of src/index.ts with the following code. This Worker exposes three endpoints: one to ingest a conversation, one to recall memories, and one to get a profile summary.
export default { async fetch(request, env, ctx) { const url = new URL(request.url); const userId = url.searchParams.get("user") || "default-user";
// Get the memory profile for this user const profile = await env.MEMORY.getProfile(userId);
if (url.pathname === "/ingest" && request.method === "POST") { const { messages } = await request.json();
await profile.ingest(messages);
return Response.json({ success: true }); }
if (url.pathname === "/recall") { const query = url.searchParams.get("q"); if (!query) { return Response.json( { error: "Missing ?q= parameter" }, { status: 400 }, ); }
const result = await profile.recall(query);
return Response.json({ answer: result.answer, }); }
if (url.pathname === "/summary") { const { summary } = await profile.getSummary();
return Response.json({ summary }); }
return new Response("Not found", { status: 404 }); },};export default { async fetch(request, env, ctx): Promise<Response> { const url = new URL(request.url); const userId = url.searchParams.get("user") || "default-user";
// Get the memory profile for this user const profile = await env.MEMORY.getProfile(userId);
if (url.pathname === "/ingest" && request.method === "POST") { const { messages } = (await request.json()) as { messages: any[] };
await profile.ingest(messages);
return Response.json({ success: true }); }
if (url.pathname === "/recall") { const query = url.searchParams.get("q"); if (!query) { return Response.json( { error: "Missing ?q= parameter" }, { status: 400 }, ); }
const result = await profile.recall(query);
return Response.json({ answer: result.answer, }); }
if (url.pathname === "/summary") { const { summary } = await profile.getSummary();
return Response.json({ summary }); }
return new Response("Not found", { status: 404 }); },} satisfies ExportedHandler<Env>;Start a local development server:
npx wrangler devIngest a conversation:
curl -X POST "http://localhost:8787/ingest?user=alice" \ -H "Content-Type: application/json" \ -d '{ "messages": [ { "role": "user", "content": "I prefer dark mode and use VS Code as my editor." }, { "role": "assistant", "content": "Noted. I will remember your preference for dark mode and VS Code." } ] }'Recall a memory:
curl "http://localhost:8787/recall?user=alice&q=What%20editor%20does%20the%20user%20prefer?"Get a profile summary:
curl "http://localhost:8787/summary?user=alice"When you are ready, deploy your Worker:
npx wrangler deploy- Learn how Agent Memory works, including the four memory types it extracts from conversations.
- Understand how profiles and namespaces isolate memory for different users.
- Explore the full API reference for
ingest(),recall(), andgetSummary().