New:Upstash has a remote MCP server

How to Add Memory to an AI Agent with Redis

Cahid Arda OzCahid Arda Oz
10 min read

In order to add memory to an AI agent, you need to store things outside the model and load them into the prompt with every turn. When the model responds to your message, you need to write new things back to memory. For most agents, short-term memory (what you talked about most recently) and long-term memory (what you know about the user) are the most important.

Redis is a great choice for both short-term memory and long-term memory because the agent is reading from memory on every turn, and Redis serves data from memory (Upstash also persists it to disk, so long-term facts survive restarts). Redis has lists and hashes built in. It can also automatically delete a key after it’s been around for some amount of time.

A memory layer that works well in production does five things for the agent:

  • Saves facts without extra code. Lasting facts like "The user is vegetarian." get stored as the user mentions them.
  • Finds memories by meaning. A question about "dietary restrictions" finds "The user is vegetarian." even though they share no words. This needs semantic search, which needs embeddings.
  • Adds the right memories to the prompt. Before each call, the facts that match the new message go into the context, so the model doesn't have to ask for them.
  • Keeps each user's memory separate. Every read and write is keyed by the user ID from your auth.
  • Fits into the framework. It plugs into the agent framework you already use, with a few lines in the route.

Upstash AgentKit handles all 5 steps for the Vercel AI SDK, TanStack AI and Vercel Eve, with no need for an embedding model, a vector database or an external memory service. It computes embeddings, performs semantic search, stores facts via the model and automatically includes relevant memories in the prompt. We will build each step manually using standard Redis to show how it works.

What memory means for an AI agent

A call to a language model is a function that takes some text and returns more text. It doesn’t store any state between calls. Everything the agent “remembers” is text that the agent passes to subsequent calls. Thus, memory is just a store of data plus the code that decides what to retrieve on each turn.

The two kinds differ in what they hold and how long they last:

MemoryWhat it holdsRedis typeHow long it lasts
Short-termThe last messages of one conversationListUntil the session goes quiet for an hour
Long-termFacts about the user: name, preferences, decisionsHashUntil the user or the app deletes them

The hour is the TTL (time to live), which is the number of seconds until Redis automatically deletes the key.

Five decisions behind every agent memory

Every memory system, whether hand-written or a service, answers the same five questions. It’s those answers that differentiate them:

  1. What gets stored? The recent conversation, short facts about the user, or both.
  2. Who decides what to remember? The model can call a save tool when it hears something lasting. The app can save every user message. Or a second model call can pull facts out of each conversation, which is what Mem0 does on every conversation you send it.
  3. How does the agent find the right memories? It can load all of them, search by keyword, search by meaning with embeddings, or do both at once.
  4. How do memories reach the model? The app can add them to the prompt before the call, or the model can call a recall tool when it thinks it needs one.
  5. Whose memory is it? Every read and write is scoped to one user, and that user ID has to come from your auth, never from the request.

The steps below build the simplest working answer to each question with plain Redis commands. After that, we add semantic search, and look at how Upstash AgentKit answers the same five questions for the Vercel AI SDK, TanStack AI and Vercel Eve.

Step 1: Keep the recent conversation

Redis lists work well for short term memory (i.e. conversation). We can append new messages, and then trim the list so that we only have the most recent messages. Keeping the size of the list small is important because we want the prompt to be small.

The code below uses @upstash/redis which is a library for sending commands over HTTP to Redis. The library is great for serverless functions and edge functions since there's no connection to manage. It uses the UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKEN environment variables to connect to a Redis instance. To get them, sign up for Upstash and create a free Redis database; both values are on the database page.

// history.ts
import { Redis } from "@upstash/redis";
 
const redis = Redis.fromEnv();
 
export type Message = { role: "user" | "assistant"; content: string };
 
export async function loadHistory(userId: string, sessionId: string) {
  return redis.lrange<Message>(`chat:${userId}:${sessionId}`, 0, -1);
}
 
export async function appendHistory(userId: string, sessionId: string, ...messages: Message[]) {
  const key = `chat:${userId}:${sessionId}`;
  await redis
    .pipeline()
    .rpush(key, ...messages)
    .ltrim(key, -20, -1) // keep the last 20 messages
    .expire(key, 60 * 60) // delete after 1 hour without a new message
    .exec();
}

When we append messages to a session, we do three things in a single request: push the new messages, trim the list down to the last 20 messages, and reset the expiration to 1 hour. If a session is left inactive for 1 hour, it will expire on its own.

Step 2: Save lasting facts about the user

Long-term memory is anything you want to survive beyond the end of a session: the user’s name, their diet, where they live, a decision they’ve made. A Redis hash per user is an ideal data structure for long-term memory: each piece of information can be stored as a different field, so that when a new value is set for diet it overwrites the old value, rather than adding to it.

// facts.ts
import { Redis } from "@upstash/redis";
 
const redis = Redis.fromEnv();
 
export async function loadFacts(userId: string) {
  return (await redis.hgetall<Record<string, string>>(`facts:${userId}`)) ?? {};
}
 
export async function saveFacts(userId: string, facts: Record<string, string>) {
  if (Object.keys(facts).length > 0) await redis.hset(`facts:${userId}`, facts);
}

The whole hash gets loaded by the agent each turn, which means it’s one command no matter the number of fields.

Step 3: Read and write memory on every turn

So during each turn we need to load both memories, call the model, and then store any newly produced messages and facts.

// agent.ts
import { loadFacts, saveFacts } from "./facts";
import { appendHistory, loadHistory, type Message } from "./history";
import { ask } from "./llm";
 
export async function turn(userId: string, sessionId: string, input: string) {
  const [history, facts] = await Promise.all([loadHistory(userId, sessionId), loadFacts(userId)]);
 
  const userMessage: Message = { role: "user", content: input };
  const reply = await ask(
    `You are a helpful assistant. Known facts about the user: ${JSON.stringify(facts)}`,
    [...history, userMessage],
  );
 
  await appendHistory(userId, sessionId, userMessage, { role: "assistant", content: reply });
  const extracted = await ask(
    'Extract lasting facts about the user (name, preferences, decisions) from this message. Reply with only a flat JSON object, no code fences, like {"diet":"vegetarian"}, or {} if there are none.',
    [userMessage],
  );
  await saveFacts(userId, JSON.parse(extracted));
 
  return reply;
}

We can use a second small call to determine if something is a fact, without altering the response. This means that a production agent can do this in the background after answering.

ask is the only part tied to a model provider, and any LLM SDK works. With the Anthropic SDK it looks like this:

// llm.ts
import Anthropic from "@anthropic-ai/sdk";
import type { Message } from "./history";
 
const client = new Anthropic();
 
export async function ask(system: string, messages: Message[]) {
  const res = await client.messages.create({ model: "claude-haiku-5-5", max_tokens: 4096, system, messages });
  return res.content.find((block) => block.type === "text")?.text ?? "";
}

Ada introduces herself in one session, then asks for dinner ideas in a new one. The demo uses top-level await, so set "type": "module" in package.json:

// demo.ts
import { turn } from "./agent";
import { loadFacts } from "./facts";
 
await turn("ada", "session-1", "Hi, I'm Ada. I'm vegetarian and I live in Lisbon.");
console.log(await loadFacts("ada"));
console.log(await turn("ada", "session-2", "Suggest a dinner place for me tonight."));

The second session starts with an empty history, and the reply still uses her facts:

{ diet: 'vegetarian', location: 'Lisbon', name: 'Ada' }
Good evening, Ada! I can't check live hours or availability, so please verify before you go. Here's a suggestion:
 
**Cantinho Vegetariano**, a long-running vegetarian restaurant in central Lisbon, ...

When a user has more facts than fit in the prompt, the agent searches memory with the new message and loads only the best matches. Keyword search misses facts that use different words. Semantic search finds them by meaning, using embeddings. Upstash Redis Search does both in one query and computes the embeddings itself, and AgentMemory from @upstash/agentkit-sdk uses it:

import { AgentMemory } from "@upstash/agentkit-sdk";
import { Redis } from "@upstash/redis";
 
const memory = new AgentMemory({ redis: Redis.fromEnv() });
const userId = "ada";
 
await memory.add({ userId, text: "The user is vegetarian." });
await memory.add({ userId, text: "The user lives in Lisbon." });
await memory.add({ userId, text: "The user is allergic to peanuts." });
await memory.searchIndex.waitIndexing();
 
const hits = await memory.recall({ userId, query: "dietary restrictions", topK: 2 });
console.log(hits.map((m) => m.text));
[ 'The user is vegetarian.', 'The user is allergic to peanuts.' ]

In this case, semantic search returns both facts because they relate to the phrase “dietary restrictions”, even though those specific words aren’t used in either fact. A keyword search wouldn’t have returned anything since there’s no overlap in words.

How Upstash AgentKit handles each decision

Writing the memory loop yourself is a good way to learn it. In an app built on an agent framework, Upstash AgentKit plugs the same design into the framework's own hooks, so the route code stays small. Every adapter searches memory by meaning with embeddings computed by Upstash Redis, lets the model save facts as it goes, and keys everything by user. The AI SDK and TanStack AI adapters, and Eve's memory slot, also add the relevant memories to the prompt automatically. It ships one adapter each for the Vercel AI SDK, TanStack AI and Vercel Eve, and all three store everything in your Upstash Redis database:

DecisionVercel AI SDKTanStack AIVercel Eve
What gets storedChats with createChatHistory, facts with createAgentMemoryThreads, runs and approvals with upstashPersistence(), facts with upstashMemory()Chats and facts with the AgentKit extension
Who decides what to rememberThe model, with a save_memory toolThe model with save_memory, plus each user messageThe model, with a save tool; the memory slot also saves each user message
How memories are foundHybrid search: keyword plus meaning, embeddings computed by Upstash RedisSame hybrid searchSame hybrid search
How memories reach the modelAdded to the instructions before the first step, plus a recall toolAdded to the system prompt before each turnA recall tool with the extension, or added before each turn with the memory slot
Whose memoryuserId from your authuserId in the middleware scopeThe caller Eve's auth verified

By default, chat history and memory use the same search index. This is to make them both work with a free Upstash Redis database, which only allows one index.

Each framework has its own walkthrough with a two-conversation demo:

Keep memory safe in production

Since memory contains personal data and is accessed on every turn of the agent, there are a few things to keep in mind before you ship:

  • Do not use the user id from the request body as it is the only thing keeping different users' memory separate, as all keys use it. Get the user id from your auth session.
  • To quickly see everything that's stored about a user, put the user id immediately after the prefix in the key name, like so: chat:ada:session-1.
  • You can check what's stored using the Data Browser in the Upstash console. It shows the value of the key and the remaining TTL.
  • If you build the agent with a coding agent such as Claude Code, Cursor or Codex, connect it to the Upstash MCP server. It can then create the database, read a user's memory keys and run commands for you while you debug.
  • The cost per turn can be calculated as follows: Each turn in the above example uses at most six Redis commands (two reads, three for history, and one to store facts when there are any). On pay-as-you-go pricing ($0.20 per 100K commands), 1,000 turns will use 6,000 commands, or $0.012. The free tier gives you 500K commands per month, which is enough for about 83,000 turns.

The Data Browser in the Upstash console displays the value of the key and its remaining TTL.

When a user asks to be forgotten, the agent deletes their facts and every session they had:

// forget.ts
import { Redis } from "@upstash/redis";
 
const redis = Redis.fromEnv();
 
export async function forgetUser(userId: string) {
  const keys = [`facts:${userId}`];
  let cursor = "0";
  do {
    const [next, found] = await redis.scan(cursor, { match: `chat:${userId}:*`, count: 100 });
    keys.push(...found);
    cursor = next;
  } while (cursor !== "0");
  return redis.del(...keys);
}

In the above demo, forgetUser("ada") returns 3 for the facts hash and the two session lists.

Putting it together

An agent with memory comes down to three things:

  1. A Redis list per session for the recent conversation, trimmed to the last messages and expired when the session goes quiet.
  2. A Redis hash per user for lasting facts, loaded into the system prompt on every turn.
  3. A turn function that reads both, calls the model, and writes the new messages and facts back.

To try it, create a free Upstash Redis database and run the code above with any model. If a coding agent is building it for you, it can also get a scratch database with one POST to start-redis, which you claim in your account to keep. When the agent remembers more than fits in a prompt, AgentMemory adds hybrid recall over the same database. If you build on the Vercel AI SDK, TanStack AI or Vercel Eve, Upstash AgentKit gives you all of it ready-made: chat history, memories the model saves, and recall by meaning that adds the right facts to the prompt.