Skip to main content
The Mem0 AI SDK Provider is a library developed by Mem0 to integrate with the Vercel AI SDK. This library brings enhanced AI interaction capabilities to your applications by introducing persistent memory functionality.
Mem0 AI SDK Provider v3.0.0 supports Vercel AI SDK v6 (LanguageModelV3 / ProviderV3). If you are upgrading from v2.x, see the AI SDK v6 migration guide.

Overview

  1. Offers persistent memory storage for conversational AI
  2. Enables smooth integration with the Vercel AI SDK v6
  3. Ensures compatibility with multiple LLM providers (OpenAI, Anthropic, Google, Groq, Cohere)
  4. Supports structured message formats for clarity
  5. Facilitates streaming response capabilities
  6. Attaches Mem0 memories as sources in responses for programmatic access

Setup and Configuration

Install the SDK provider and AI SDK:

Dependencies

@mem0/vercel-ai-provider bundles ai, all @ai-sdk/* provider packages, and @ai-sdk/provider as regular dependencies: you do not need to install them separately. The install command above (npm install @mem0/vercel-ai-provider ai@^6) is sufficient. The only true peer dependency is zod (optional):
  • zod v3+ (^3.0.0): required only if you use Zod schemas in tool definitions

Getting Started

Setting Up Mem0

  1. Get your Mem0 API Key from the Mem0 Dashboard.
  2. Initialize the Mem0 Client in your application:
    Note: The openai provider is set as default. Consider using MEM0_API_KEY and OPENAI_API_KEY as environment variables for security.
    Note: The mem0Config is optional. It is used to set the global config for the Mem0 Client (eg. user_id, agent_id, app_id, run_id etc).
  3. Add Memories to Enhance Context:

Standalone Features

For standalone features, such as addMemories, retrieveMemories, and getMemories, you must either set MEM0_API_KEY as an environment variable or pass it directly in the function call.
getMemories will return raw memories in the form of an array of objects, while retrieveMemories will return a response in string format with a system prompt ingested with the retrieved memories.

1. Basic Text Generation with Memory Context

2. Combining OpenAI Provider with Memory Utils

3. Structured Message Format with Memory

4. Streaming Responses with Memory Context

5. Generate Responses with Tools Call

6. Get Sources from Memory

generateText and streamText responses include Mem0 memories as a source, giving you programmatic access to the memories that influenced the response:
The same can be done for streamText as well.

7. File Support with Memory Context

Mem0 AI SDK supports file processing with memory context. Here’s an example of analyzing a PDF file:
Note: File support is available with providers that support multimodal capabilities like Google’s Gemini models. The example shows how to process PDF files, but you can also work with images, text files, and other supported formats.

Supported LLM Providers

ProviderConfiguration Value
OpenAIopenai
Anthropicanthropic
Google / Geminigoogle or gemini
Groqgroq
Coherecohere
Note: You can use either google or gemini as the provider value for Google Gemini models. Both map to the @ai-sdk/google package internally.

Configuration Options

Mem0ConfigSettings

These options can be passed per-request when creating a model instance:
OptionTypeDescription
user_idstringUser identifier for memory scoping
agent_idstringAgent identifier
app_idstringApplication identifier
run_idstringRun/session identifier
metadataobjectCustom metadata for memories
filtersobjectFilters for memory search
inferbooleanEnable inference-based retrieval
top_knumberNumber of memories to retrieve (default: 10)
thresholdnumberRelevance threshold for search
rerankbooleanEnable reranking of results
pagenumberPage number for pagination
page_sizenumberResults per page
mem0ApiKeystringMem0 API key; overrides the MEM0_API_KEY env var
hoststringCustom Mem0 API base URL for self-hosted deployments

Key Features

  • createMem0(): Initializes a new Mem0 provider instance implementing ProviderV3.
  • retrieveMemories(): Retrieves memory context for prompts as a formatted system prompt string.
  • getMemories(): Get memories from your profile in array format.
  • addMemories(): Adds user memories to enhance contextual responses.
  • searchMemories(): Searches memories and returns the raw results array (semantic search rather than the full retrieval pipeline).

Migrating from v2.x

If you’re upgrading from @mem0/vercel-ai-provider v2.x:
  1. Upgrade AI SDK: npm install ai@^6 and update all @ai-sdk/* provider packages to ^3.x
  2. Remove deprecated params: Remove org_id, project_id, output_format, filter_memories, async_mode, enable_graph from your config
  3. Remove graph memory: All graph-related options (enable_graph, graph prompts) have been removed. Graph memory is now a project-level setting on the Mem0 Platform
  4. Update imports: LanguageModelV2Prompt is now LanguageModelV3Prompt if you import types directly

Best Practices

  1. User Identification: Use a unique user_id for consistent memory retrieval.
  2. Memory Cleanup: Regularly clean up unused memory data.
  3. Sources: Access result.sources to inspect which memories influenced the response.
    Note: We also have support for agent_id, app_id, and run_id. Refer Docs.

Conclusion

Mem0’s Vercel AI SDK enables the creation of intelligent, context-aware applications with persistent memory and seamless integration.

OpenAI Agents SDK

Build agents with OpenAI SDK and Mem0

Mastra Integration

Create intelligent agents with Mastra framework
Using Mem0? Star us on GitHub to help more developers discover memory for AI apps.