Skip to content

How to Add Persistent Memory to AI Agents with Supermemory

Problem

I built a coding assistant that reset between every session. A user would spend twenty minutes explaining their codebase structure, then close the tab and come back later. The assistant greeted them with “Hello, how can I help you?” as if they had never met.

I tried passing the full conversation history on every request. That hit token limits fast. I tried building a custom vector DB pipeline. It worked for simple cases but fell apart when user preferences changed or when I needed to track multiple users.

What I needed was a memory layer that handled extraction, storage, and retrieval automatically.

The Basic Idea

Supermemory gives you persistent memory through two core operations:

  1. client.add() — store facts, conversations, or documents
  2. client.profile() — retrieve a curated summary of what you know about a user

You can also use framework-specific middleware that does this automatically.

Direct SDK Approach

Here is the simplest way to add memory without any framework magic:

direct-memory.ts
import Supermemory from "supermemory"
const client = new Supermemory()
// Store during conversation
await client.add({
content: "User is Alex, a TypeScript developer working on auth migration",
containerTag: "user_123"
})
// Retrieve before generating response
const { profile } = await client.profile({ containerTag: "user_123" })
const systemPrompt = `You are a helpful assistant.
About the user:
${profile.static.map(f => `- ${f}`).join("\n")}
Current context:
${profile.dynamic.map(f => `- ${f}`).join("\n")}`

The containerTag is critical. It scopes all memories to a specific user or project. Without it, memories from different users leak into each other.

Framework Integration (Vercel AI SDK)

If you use Vercel AI SDK, you can wrap your model with withSupermemory():

ai-sdk-memory.ts
import { withSupermemory } from "@supermemory/tools/ai-sdk"
import { openai } from "@ai-sdk/openai"
import { generateText } from "ai"
const modelWithMemory = withSupermemory(openai("gpt-4o"), {
containerTag: "user_123",
customId: "conversation-456",
mode: "full",
addMemory: "always",
})
const result = await generateText({
model: modelWithMemory,
messages: [{ role: "user", content: "What do you know about me?" }],
})
// Agent automatically recalls: "You are Alex, a TypeScript developer..."

The middleware handles three things automatically:

  • Fetches the user’s profile before the LLM call
  • Injects it into the system prompt
  • Saves new memories after the response

What the Modes Mean

The mode option controls how much context gets injected:

  • profile — injects only the user profile summary
  • query — injects only memories relevant to the current query
  • full — injects both profile and query-relevant memories

I started with profile for simplicity, then switched to full when I needed deeper context.

What Happened When I Forgot the customId

I skipped customId on my first try. Two different conversations from the same user got merged into one memory stream. The agent started referencing topics from conversation A while the user was talking about topic B in conversation C.

Always set a customId per conversation thread. On the server, use RequestContext for dynamic thread IDs per request.

Summary

In this post, I showed how to add persistent memory to AI agents using Supermemory. The key point is that you do not need to build a vector database pipeline. With the direct SDK, you store facts with client.add() and retrieve them with client.profile(). With framework middleware, the whole process happens automatically. Either way, your agents stop forgetting and start remembering.

Final Words + More Resources

My intention with this article was to help others share my knowledge and experience. If you want to contact me, you can contact by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!

Comments