CortexDB Docs
Integrations

Vercel AI SDK

Add CortexDB memory to Vercel AI SDK apps with the TypeScript client.

The Vercel AI SDK integrates via the TypeScript SDK — isomorphic, so it runs in edge and Node runtimes.

Install

npm install cortexdbai

Recall + capture around a chat

import { V1Client } from "cortexdbai/v1";
import { streamText } from "ai";
import { openai } from "@ai-sdk/openai";

const client = new V1Client({
  apiUrl: "https://api-v1.cortexdb.ai",
  actor: "user:alice",
  bearer: process.env.CORTEX_TOKEN!,
});
const scope = "org:acme/user:alice";

export async function POST(req: Request) {
  const { messages } = await req.json();
  const last = messages.at(-1).content;

  const pack = await client.recall(scope, { view: "holistic", query: last, diagnostics: "none" });

  const result = streamText({
    model: openai("gpt-4o"),
    system: `Relevant memory:\n${pack.context_block ?? ""}`,
    messages,
  });

  await client.experience(scope, {
    content: { kind: "message", role: "user", text: last },
    context: { observed_at: new Date().toISOString() },
    idempotency_key: `chat-${crypto.randomUUID()}`,
  });

  return result.toDataStreamResponse();
}

TS SDK item fields are camelCase at the top level

The V1Client construction uses apiUrl / observedAt / idempotencyKey (camelCase); the content / context envelope keys inside experience stay snake_case, matching the wire shape. See the TypeScript SDK.

See also

On this page