Agents

Memory

Memory lets your agent save information and recall it later. Without memory, every conversation starts fresh. With memory, your agent builds context over time, recalls previous interactions, and adapts to the user.

Three Approaches

You can add memory to your agent with the AI SDK in three ways, each with different tradeoffs:

ApproachEffortFlexibilityProvider Lock-in
Provider-Defined ToolsLowMediumYes
Memory ProvidersLowLowDepends on memory provider
Custom ToolHighHighNo

Provider-Defined Tools

Provider-defined tools are tools where the provider specifies the tool's inputSchema and description, but you provide the execute function. The model has been trained to use these tools, which can result in better performance compared to custom tools.

Anthropic Memory Tool

The Anthropic Memory Tool gives Claude a structured interface for managing a /memories directory. Claude reads its memory before starting tasks, creates and updates files as it works, and references them in future conversations.

import { anthropic } from '@ai-sdk/anthropic';
import { ToolLoopAgent } from 'ai';

const memory = anthropic.tools.memory_20250818({
  execute: async action => {
    // `action` contains `command`, `path`, and other fields
    // depending on the command (view, create, str_replace,
    // insert, delete, rename).
    // Implement your storage backend here.
    // Return the result as a string.
  },
});

const agent = new ToolLoopAgent({
  model: 'anthropic/claude-haiku-5.5',
  tools: { memory },
});

const result = await agent.generate({
  prompt: 'Remember that my favorite editor is Neovim',
});

The tool receives structured commands (view, create, str_replace, insert, delete, rename), each with a path scoped to /memories. Your execute function maps these to your storage backend (the filesystem, a database, or any other persistence layer).

When to use this: you want memory with minimal implementation effort and are already using Anthropic models. The tradeoff is provider lock-in, since this tool only works with Claude.

Memory Providers

Another approach is to use a provider that has memory built in. These providers wrap an external memory service and expose it through the AI SDK's standard interface. Memory storage, retrieval, and injection happen transparently, and you do not define any tools yourself.

Letta

Letta provides agents with persistent long-term memory. You create an agent on Letta's platform (cloud or self-hosted), configure its memory there, and use the AI SDK provider to interact with it. Letta's agent runtime handles memory management (core memory, archival memory, recall).

pnpm add @letta-ai/vercel-ai-sdk-provider
import { lettaCloud } from '@letta-ai/vercel-ai-sdk-provider';
import { ToolLoopAgent } from 'ai';

const agent = new ToolLoopAgent({
  model: lettaCloud(),
  providerOptions: {
    letta: {
      agent: { id: 'your-agent-id' },
    },
  },
});

const result = await agent.generate({
  prompt: 'Remember that my favorite editor is Neovim',
});

You can also use Letta's built-in memory tools alongside custom tools:

import { lettaCloud } from '@letta-ai/vercel-ai-sdk-provider';
import { ToolLoopAgent } from 'ai';

const agent = new ToolLoopAgent({
  model: lettaCloud(),
  tools: {
    core_memory_append: lettaCloud.tool('core_memory_append'),
    memory_insert: lettaCloud.tool('memory_insert'),
    memory_replace: lettaCloud.tool('memory_replace'),
  },
  providerOptions: {
    letta: {
      agent: { id: 'your-agent-id' },
    },
  },
});

const stream = agent.stream({
  prompt: 'What do you remember about me?',
});

See the Letta provider documentation for full setup and configuration.

Mem0

Mem0 adds a memory layer on top of any supported LLM provider. It automatically extracts memories from conversations, stores them, and retrieves relevant ones for future prompts.

pnpm add @mem0/vercel-ai-provider
import { createMem0 } from '@mem0/vercel-ai-provider';
import { ToolLoopAgent } from 'ai';

const mem0 = createMem0({
  provider: 'openai',
  mem0ApiKey: process.env.MEM0_API_KEY,
  apiKey: process.env.OPENAI_API_KEY,
});

const agent = new ToolLoopAgent({
  model: mem0('gpt-6-astra', { user_id: 'user-123' }),
});

const { text } = await agent.generate({
  prompt: 'Remember that my favorite editor is Neovim',
});

Mem0 works across multiple LLM providers (OpenAI, Anthropic, Google, Groq, Cohere). You can also manage memories explicitly:

import { addMemories, retrieveMemories } from '@mem0/vercel-ai-provider';

await addMemories(messages, { user_id: 'user-123' });
const context = await retrieveMemories(prompt, { user_id: 'user-123' });

See the Mem0 provider documentation for full setup and configuration.

Supermemory

Supermemory is a long-term memory platform that adds persistent, self-growing memory to your AI applications. It provides tools that handle saving and retrieving memories automatically through semantic search.

pnpm add @supermemory/tools
import { supermemoryTools } from '@supermemory/tools/ai-sdk';
import { ToolLoopAgent } from 'ai';

const agent = new ToolLoopAgent({
  model: "anthropic/claude-sonnet-5.5",
  tools: supermemoryTools(process.env.SUPERMEMORY_API_KEY!),
});

const result = await agent.generate({
  prompt: 'Remember that my favorite editor is Neovim',
});

Supermemory works with any AI SDK provider. The tools give the model addMemory and searchMemories operations that handle storage and retrieval.

See the Supermemory provider documentation for full setup and configuration.

Hindsight

Hindsight provides agents with persistent memory through five tools: retain, recall, reflect, getMentalModel, and getDocument. It can be self-hosted with Docker or used as a cloud service.

pnpm add @vectorize-io/hindsight-ai-sdk @vectorize-io/hindsight-client
import { HindsightClient } from '@vectorize-io/hindsight-client';
import { createHindsightTools } from '@vectorize-io/hindsight-ai-sdk';
import { ToolLoopAgent } from 'ai';
import { openai } from '@ai-sdk/openai';

const client = new HindsightClient({ baseUrl: process.env.HINDSIGHT_API_URL });

const agent = new ToolLoopAgent({
  model: "anthropic/claude-sonnet-5.5",
  tools: createHindsightTools({ client, bankId: 'user-123' }),
  instructions: 'You are a helpful assistant with long-term memory.',
});

const result = await agent.generate({
  prompt: 'Remember that my favorite editor is Neovim',
});

The bankId identifies the memory store and is typically a user ID. In multi-user apps, call createHindsightTools inside your request handler so each request gets the right bank. Hindsight works with any AI SDK provider.

See the Hindsight provider documentation for full setup and configuration.

MongoDB

@mongodb-developer/vercel-ai-memory provides MongoDB Atlas-backed persistent memory with five structured tiers: Session, Semantic, Procedural, Episodic, and Scratchpad. Retrieval is powered by Atlas Vector Search using any AI SDK embedding model, with automatic index creation and per-type retention policies.

pnpm add @mongodb-developer/vercel-ai-memory

This integration targets AI SDK v6 because it relies on v6 APIs such as ModelMessage, ToolLoopAgent, and isLoopFinished():

{
  "peerDependencies": {
    "ai": "^6.0.0",
    "mongodb": "^6.0.0",
    "zod": "^3.0.0"
  }
}
import { createMongoDBMemory } from '@mongodb-developer/vercel-ai-memory';
import { openai } from '@ai-sdk/openai';
import { ToolLoopAgent, isLoopFinished } from 'ai';

// Create the memory instance once at module/server level
const mongodbMemory = createMongoDBMemory({
  uri: process.env.MONGODB_URI!,
  embedder: openai.embedding('text-embedding-3-small'),
});

// Scope to a user and session per request
const agent = new ToolLoopAgent({
  model: openai('gpt-6-astra'),
  tools: mongodbMemory({ userId: 'alice', sessionId: 'sess-001' }),
  stopWhen: isLoopFinished(),
});

const result = await agent.generate({
  prompt: 'My name is Alice and I love hiking. Remember that.',
});

isLoopFinished() lets the agent keep running until the tool loop naturally finishes, which is useful when memory tools need to read and write before the final response.

Session memory supports two modes: tool-driven (the LLM decides when to read/write — good for prototypes) and hook-driven (the runtime persists every turn via prepareCall + onEnd hooks — recommended for production). The other memory tiers (semantic, procedural, episodic, scratchpad) are always LLM-controlled and selective by design.

MongoDB memory works with any AI SDK model and embedding provider, with no vendor lock-in beyond MongoDB Atlas.

When to use memory providers: these providers are a good fit when you want memory without building any storage infrastructure. The tradeoff is that the provider controls memory behavior, so you have less visibility into what gets stored and how it is retrieved. You also take on a dependency on an external service.

Redis Agent Memory

Redis Agent Memory is a managed memory layer that combines session history with long-term memories. The ai-sdk-redis-agent-memory package wraps a model you have already configured, restores the user's session and recalls relevant long-term memories before each call, and records the conversation afterwards. The model does not need memory tools. It also exposes memory tools for agents that should manage their own memory.

pnpm add @redis/vercel-ai-sdk
import { anthropic } from '@ai-sdk/anthropic';
import { createRedisAgentMemory } from '@redis/vercel-ai-sdk';
import { ToolLoopAgent } from 'ai';

// Create the memory factory once at module/server level
const memory = createRedisAgentMemory({
  serverUrl: process.env.AGENT_MEMORY_URL,
  storeId: process.env.AGENT_MEMORY_STORE_ID,
  apiKey: process.env.AGENT_MEMORY_API_KEY,
  model: anthropic('claude-sonnet-4-5'),
});

// Create a new memory model for each request, scoped to a user and session
const agent = new ToolLoopAgent({
  model: memory.model({
    scope: { userId: 'user-123', sessionId: 'session-456' },
    assistantId: 'assistant',
  }),
});

const result = await agent.generate({
  prompt: 'Remember that my favorite editor is Neovim',
});

Recalled memories and restored history are injected as bounded, untrusted context, and the user message and each text-producing step are recorded. Set historyMode: 'application' if your application already manages conversation history and you only want long-term recall.

To let the model manage memory itself, pass memory.tools({ scope }) as tools instead. This gives the model operations such as search_memories and create_memories.

The package requires AI SDK 7. It accepts configured model objects, not model-name strings. Writes are best effort, so use collision-resistant session IDs tied to your authenticated user and serialize requests for the same session.

See the Redis Agent Memory provider documentation for full setup and configuration.

Custom Tool

Building your own memory tool from scratch is the most flexible approach. You control the storage format, the interface, and the retrieval logic. This requires the most upfront work but gives you full ownership of how memory works, with no provider lock-in and no external dependencies.

There are two common patterns:

  • Structured actions: you define explicit operations (view, create, update, search) and handle structured input yourself. Safe by design since you control every operation.
  • Bash-backed: you give the model a sandboxed bash environment to compose shell commands (cat, grep, sed, echo) for flexible memory access. More powerful but requires command validation for safety.

For a full walkthrough of implementing a custom memory tool with a bash-backed interface, AST-based command validation, and filesystem persistence, see the Build a Custom Memory Tool recipe.