Add persistent memory to Vercel AI SDK chat apps.
Vercel AI SDK Integration
The Vercel AI SDK powers most React-based chat UIs. Wire CortexDB into your route.ts handler to recall context before the LLM call and capture turns after the stream resolves.
Install
npm install cortexdbai ai @ai-sdk/openai
LLM provider note. The Vercel AI SDK quickstart installs
@ai-sdk/openai, but CortexDB itself is LLM-agnostic. Swap for@ai-sdk/anthropic,@ai-sdk/google,@ai-sdk/groq, or any@ai-sdk/*provider —cortexdbaionly talks to the CortexDB v1 REST surface, never directly to a model provider.
API route
// app/api/chat/route.ts
import { streamText } from "ai";
import { openai } from "@ai-sdk/openai";
import { V1Client } from "cortexdbai/v1";
const cortex = new V1Client({
apiUrl: "https://api-v1.cortexdb.ai",
actor: "user:alice",
bearer: process.env.CORTEX_TOKEN!,
});
export async function POST(req: Request) {
const { messages, scope } = await req.json();
const lastUser = messages.findLast((m: any) => m.role === "user")?.content ?? "";
// 1. Recall prior context
const pack = await cortex.recall(scope, {
view: "holistic",
query: lastUser,
include: ["beliefs", "facts", "episodes"],
budgets: { max_tokens: 3000 },
});
// 2. Stream the LLM response
const result = await streamText({
model: openai("gpt-4o"),
system: `Prior context:\n${pack.context_block}`,
messages,
onFinish: async ({ text }) => {
// 3. Capture both turns when the stream completes
await cortex.experience(scope, {
modality: "conversation",
content: { kind: "message", role: "user", text: lastUser },
context: { observed_at: new Date().toISOString() },
idempotency_key: `u-${crypto.randomUUID()}`,
});
await cortex.experience(scope, {
modality: "conversation",
content: { kind: "message", role: "assistant", text },
context: { observed_at: new Date().toISOString() },
idempotency_key: `a-${crypto.randomUUID()}`,
});
},
});
return result.toDataStreamResponse();
}
Edge runtime
V1Client uses fetch + AbortController only — works on Vercel Edge and Cloudflare Workers without modification.
export const runtime = "edge";