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.
| Layer | What it owns | Suggested lifetime |
|---|---|---|
| Current messages | The turns needed to answer this request | One request or short conversation window |
| Agent execution state | Tool calls, loop state, and resumable workflow data | Until the run completes or its workflow expires |
| Durable memory | Preferences, decisions, constraints, stable facts | Policy-driven per user and tenant |
| Source of truth | Permissions, billing, inventory, account status | The 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.
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.
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.
| Store | Do 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 plan | A speculative answer the user has not accepted |
| A constraint that affects future work | A secret, API key, password, or authentication token |
| A source-linked fact about the account | Permissions or billing state that must come from the database |
| A correction and its history | The 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.
| Scenario | Expected result |
|---|---|
| A user starts a new chat on a different route | The stable preference is retrieved without replaying the old transcript |
| Two tenants use the same agent ID | No foreign tenant memory appears in either prompt |
| A plan changes from monthly to annual | The newer fact wins while the old fact remains auditable |
| A user requests deletion | The memory is absent from retrieval and export |
| The memory service times out | The chat degrades honestly and the write is retried or reported |
| A stream is retried | Duplicate events do not create contradictory memories |
What is the recommended Vercel AI SDK memory architecture?
Use the AI SDK for model orchestration and streaming. Use authenticated server code to build scopes and retrieve context. Use a model-neutral memory service for extraction, merging, compression, correction, and deletion. Return the answer through the SDK without making memory availability a hidden prerequisite for every response.
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.
