Integrate

Vercel AI SDK memory without stuffing the transcript

Use the AI SDK for model calls, tools, and streams. Use a separate memory service for the durable facts that should follow a user into the next request, route, or deployment. Retrieve a small scoped context before generation, write only meaningful events after it, and keep authorization outside the prompt.

Updated October 30, 202611 min readAI Memory field guide
A luminous TypeScript memory pipeline connecting a serverless agent to scoped memory shards.

What does memory mean in the Vercel AI SDK?

The AI SDK gives TypeScript applications a consistent interface for models, tools, agents, streaming, and UI integration. It supports several memory patterns, including provider-defined memory tools, external memory providers, and a custom tool that calls your own storage layer. The SDK does not remove the product decision about which facts should persist, who may retrieve them, or when they must be deleted.

In a Next.js app, an AI SDK request usually contains current messages and execution state. Durable memory is a separate contract: send an event after it happens, extract the useful facts, merge changes, enforce user and tenant scope, then retrieve only the context needed for the next call.

How should an AI SDK app separate session history from durable memory?

Keep the active transcript for request execution and keep durable memory for facts that remain useful after the transcript is gone. The split prevents a long chat from becoming the only source of personalization and gives the application a clear place to correct or delete what the agent has learned.

LayerWhat it ownsSuggested lifetime
Current messagesThe turns needed to answer this requestOne request or short conversation window
Agent execution stateTool calls, loop state, and resumable workflow dataUntil the run completes or its workflow expires
Durable memoryPreferences, decisions, constraints, stable factsPolicy-driven per user and tenant
Source of truthPermissions, billing, inventory, account statusThe application database; never model memory

Treat the AI SDK message history as working context. Do not infer that a fact is safe to preserve forever merely because it appeared in the prompt.

How do you retrieve memory before a Vercel AI SDK model call?

Resolve the user, agent, and tenant from the authenticated server session, retrieve a small context block, then pass that block to the model with a clear boundary. Never accept a user ID or tenant ID from the browser as the authorization source for the memory query.

Next.js route handlertypescript
import { generateText } from 'ai';
import { openai } from '@ai-sdk/openai';
import { Memory } from '@aimemory/sdk';

const memory = new Memory({ apiKey: process.env.AI_MEMORY_KEY });

export async function answer(input: {
  prompt: string;
  userId: string;
  tenantId: string;
}) {
  const context = await memory.search({
    query: input.prompt,
    userId: input.userId,
    agentId: 'web-assistant',
    metadata: { tenant: input.tenantId },
    limit: 6,
  });

  const contextText = context.memories
    .map((item) => `- ${item.text}`)
    .join('\n');

  return generateText({
    model: openai(process.env.AI_MODEL!),
    system: [
      'You are a product assistant.',
      'Use the memory below only as user context.',
      '<memory>',
      contextText,
      '</memory>',
    ].join('\n'),
    prompt: input.prompt,
  });
}

How should streaming AI SDK responses write memory?

Write after the response is available, not before the model has done the work. With streaming, place the write in the SDK finish callback, await it when the deployment permits, and use application-level error handling so a failed memory write does not pretend the stream failed too.

Store the meaningful event rather than the entire visible response. A support run might produce a confirmed plan change, a delivery constraint, and a customer preference. Most of the remaining prose is disposable.

  • Pass the same authenticated user and tenant IDs used for retrieval.
  • Record the route or integration as metadata so failures can be traced.
  • Do not write credentials, raw payment data, or hidden system prompts.
  • Make retries idempotent when the stream finish callback can be retried.
Finish callbacktypescript
import { streamText } from 'ai';

const result = streamText({
  model: openai(process.env.AI_MODEL!),
  system: systemPrompt,
  prompt: userMessage,
  onFinish: async ({ text }) => {
    await memory.add({
      messages: [
        { role: 'user', content: userMessage },
        { role: 'assistant', content: text },
      ],
      userId,
      agentId: 'web-assistant',
      metadata: { tenant: tenantId, route: '/chat' },
    });
  },
});

What belongs in Vercel AI SDK memory?

Remember information that changes a future answer or action. Do not remember everything simply because the AI SDK makes it easy to send messages to another endpoint.

StoreDo not store
A stable preference such as “weekly summaries on Friday”A temporary comment such as “I am reading this now”
A decision that supersedes an older planA speculative answer the user has not accepted
A constraint that affects future workA secret, API key, password, or authentication token
A source-linked fact about the accountPermissions or billing state that must come from the database
A correction and its historyThe entire high-token transcript without a retention reason

How should serverless AI SDK deployments protect memory scopes?

Keep the memory API key on the server and derive scopes from trusted authentication and tenant routing. A serverless function should be stateless about authorization even when the memory layer is stateful. Every write and read should carry the same tenant boundary, and failed scope resolution should stop the request rather than fall back to a shared identity.

Use short cache lifetimes for retrieved memory and never cache one user’s context under a route-level key. If a plan changes, invalidate the relevant source-of-truth data and write a correction instead of relying on prompt instructions to ignore the old fact.

How do you test Vercel AI SDK memory before production?

Test the route handler as an authorization boundary, not only as a model wrapper. The useful cases prove that session context and durable memory remain separate, that two users cannot share memories, and that a corrected fact wins on the next request.

ScenarioExpected result
A user starts a new chat on a different routeThe stable preference is retrieved without replaying the old transcript
Two tenants use the same agent IDNo foreign tenant memory appears in either prompt
A plan changes from monthly to annualThe newer fact wins while the old fact remains auditable
A user requests deletionThe memory is absent from retrieval and export
The memory service times outThe chat degrades honestly and the write is retried or reported
A stream is retriedDuplicate events do not create contradictory memories

Frequently asked questions

Does the Vercel AI SDK have built-in long-term memory?

The AI SDK documents several memory approaches, including provider-defined tools, external memory providers, and custom tools. The application still needs to decide what is durable, enforce scopes, and own correction and deletion.

Can I use AI Memory with any Vercel AI SDK model provider?

Yes. AI Memory is model-neutral. Build the memory query in server code before the AI SDK model call and write the meaningful event afterward.

Should I store every chat message in memory?

No. Keep active messages for the request, but persist only durable facts, decisions, preferences, constraints, and useful episodes. Storing everything increases cost and makes deletion harder to reason about.

How do I stream a response without losing the memory write?

Use the AI SDK finish callback to send the completed interaction to memory. Add retries and an idempotency key when duplicate finish callbacks are possible.

Can the browser call AI Memory directly?

Keep the API key and trusted scope resolution on the server. A browser request can supply the user message, but it must not choose another user or tenant scope.

What should I test before deploying an AI SDK memory integration?

Test cross-session recall, route changes, tenant isolation, correction, deletion, streaming retries, memory-service failure, token size, and latency with your own evaluation conversations.

Add memory around the AI SDK call

Start with one authenticated chat route, retrieve one scoped context block, and prove that a corrected fact survives into the next session.