How to Add Memory to a Vercel AI SDK Agent with Redis
streamText and generateText in Vercel AI SDK only process the messages you send to them. If a user tells the agent that "I'm vegetarian" in an earlier conversation, the agent won't be able to recall that because each request to the AI SDK models is independent.
To fix this, we need to manually store and retrieve two things: the chat history of the conversation, and the facts (long-term memory) about the user.
In this guide, we'll implement both on Upstash Redis using Upstash AgentKit.
What memory does an AI SDK agent need?
An AI SDK agent needs two kinds of external memory: both chat history to load when the user reloads the page, and long-term memory to load into each conversation with facts about the user. These live outside the model, and are loaded by your route before each call.
The two kinds behave differently:
| Chat history | Long-term memory | |
|---|---|---|
| Scope | One conversation (chatId) | One user, across all conversations |
| What gets stored | Every message, in order | Short facts ("The user is vegetarian.") |
| Who writes it | Your route, after each response | The model, through a tool call |
| How it's read | Load the whole transcript into useChat | Search for the facts that matter now |
Redis is great for both because it's fast enough to read on every turn, and one database holds the transcripts, the memories, and the search index over them.
Set up the project
AgentKit's AI SDK adapter gives you ready-made helpers for both kinds of memory. Install it with the AI SDK and a model provider:
npm install @upstash/agentkit-ai-sdk @upstash/redis ai @ai-sdk/anthropicCreate a Redis database in the Upstash Console and copy its REST URL and token into your environment. AgentKit reads them with Redis.fromEnv(), so there's nothing else to configure:
UPSTASH_REDIS_REST_URL="https://..."
UPSTASH_REDIS_REST_TOKEN="..."Store chat history
createChatHistory saves each conversation as one JSON document in Redis, keyed by user and chat ID. The chat route saves the full transcript when the stream finishes, and gives the model two memory tools at the same time:
// app/api/chat/route.ts
import { anthropic } from "@ai-sdk/anthropic";
import { createChatHistory, createMemoryTools } from "@upstash/agentkit-ai-sdk";
import {
convertToModelMessages,
createUIMessageStreamResponse,
isStepCount,
streamText,
toUIMessageStream,
type UIMessage,
} from "ai";
declare function getSessionUserId(req: Request): Promise<string>; // your auth provider
const history = createChatHistory();
export async function POST(req: Request) {
const userId = await getSessionUserId(req);
const { id: chatId, messages } = (await req.json()) as { id: string; messages: UIMessage[] };
const result = streamText({
model: anthropic("claude-haiku-5-5"),
system:
"Before you answer, call recall_memory with no query to load what you know about the user. " +
"When the user tells you a lasting fact about themselves, save it with save_memory.",
messages: await convertToModelMessages(messages),
tools: createMemoryTools({ userId }),
stopWhen: isStepCount(5),
});
return createUIMessageStreamResponse({
stream: toUIMessageStream({
stream: result.stream,
originalMessages: messages,
onFinish: async ({ messages }) => {
await history.saveChat({ userId, sessionId: chatId, messages });
},
}),
});
}saveChat replaces the whole message array each time, which matches how useChat works: the client posts the full conversation on every request. To restore a chat after a reload, load it on the server and pass it to useChat:
const chat = await history.getChat({ userId, sessionId: chatId });
// client: useChat({ id: chatId, messages: chat?.messages ?? [] })Only userId distinguishes one user's data from another's. Retrieve it from your auth session (Clerk, Auth.js, Supabase Auth) – not the request body.
The same helper lists chats for the sidebar (listChats) and implements full-text search across them (searchChats).
Give the agent long-term memory
The function createMemoryTools returns two tools for the AI SDK - save_memory, which will save a fact for the user, and recall_memory, which will get the facts back out. The model will automatically choose when to use each of these tools, based on the system prompt in the route above.
We ran two separate conversations for the same user. The first one shares two facts, and the second one, a minute later, asks a question that needs them:
import { anthropic } from "@ai-sdk/anthropic";
import { createMemoryTools } from "@upstash/agentkit-ai-sdk";
import { generateText, isStepCount } from "ai";
const userId = "user-45";
const tools = createMemoryTools({ userId });
async function ask(sessionId: string, prompt: string) {
const result = await generateText({
model: anthropic("claude-haiku-5-5"),
system:
"Before you answer, call recall_memory with no query to load what you know about the user. " +
"When the user tells you a lasting fact about themselves, save it with save_memory.",
tools,
stopWhen: isStepCount(5),
prompt,
});
const calls = result.steps.flatMap((s) => s.toolCalls.map((c) => `${c.toolName}(${JSON.stringify(c.input)})`));
console.log(`[${sessionId}] tools: ${calls.join(", ")}`);
console.log(`[${sessionId}] answer: ${result.text}\n`);
}
await ask("chat-1", "I'm vegetarian and I live in Lisbon.");
await new Promise((r) => setTimeout(r, 60_000)); // a new conversation, a minute later
await ask("chat-2", "Suggest a dinner place for me tonight.");The output, trimmed:
[chat-1] tools: recall_memory({}), save_memory({"text":"The user is vegetarian."}), save_memory({"text":"The user lives in Lisbon."})
[chat-1] answer: Thanks for letting me know. I've saved that you're vegetarian and live in Lisbon...
[chat-2] tools: recall_memory({})
[chat-2] answer: Since you're vegetarian and in Lisbon, I'd look at a few cuisines that are reliably good for it:
- Indian: Usually plenty of vegetarian curries, dal, and naan.
...The second conversation did not include any messages about diet or location, but the agent was able to retrieve this information from Redis. Each memory is stored as a JSON document in Redis at agentkit:memory:<userId>:<id>, and you can view them in the Upstash Console Data Browser.
How recall finds the right memories
recall_memory uses Upstash Redis Search to do keyword and fuzzy searches with BM25 ranking (a standard text relevance score). This doesn't use embeddings, so it's faster and doesn't require an embedding model, but it also means that search terms must share some words with the memory to get a match.
For example, when we tried a more permissive system prompt, the model called recall_memory with query "dinner food restaurant preferences". Since that query doesn't overlap with "The user is vegetarian.", the recall result was empty, and the agent asked the user where they live.
Index updates in Redis Search happen in the background. In our tests, new memories took up to ~30 seconds to appear in recall results. This is fine for use across multiple conversations, but if a memory gets created in one turn of a conversation, it might not turn up if searched for in the next turn.
Call recall_memory without any query to get all memories for that user. If an agent stores just a few dozen memories per user, retrieving all of them at the start of a conversation is the easiest approach, and is what we request in this guide's system prompt. Use a search query if a user might have more memories than the prompt can fit.
Putting it together
For most AI SDK apps, this setup is all you need: use createChatHistory to keep track of each conversation, and createMemoryTools with a prompt to recall everything to remember stuff about the user. The AgentKit docs have the full API reference, and the AgentKit repo contains a complete Next.js app with chat sidebar example.
Everything lives in your own Upstash Redis database as plain JSON documents at known keys, so you can read, change, and delete a user's memory yourself. AgentKit works in edge runtimes as well as Node.js, and the same package adds RAG search tools, rate limiting, and tool caching to the agent.