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, {
    text: last,
    role: "user",
    observedAt: new Date().toISOString(),
    idempotencyKey: `chat-${crypto.randomUUID()}`,
  });

  return result.toDataStreamResponse();
}

TS SDK items are flattened and camelCase

experience() takes an ExperienceItem with camelCase fields (text, role, observedAt, idempotencyKey, …) and the SDK builds the snake_case wire envelope; passing content / context is a type error and throws at runtime. See the TypeScript SDK.

See also

On this page