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
- Offers persistent memory storage for conversational AI
- Enables smooth integration with the Vercel AI SDK v6
- Ensures compatibility with multiple LLM providers (OpenAI, Anthropic, Google, Groq, Cohere)
- Supports structured message formats for clarity
- Facilitates streaming response capabilities
- Attaches Mem0 memories as sources in responses for programmatic access
Setup and Configuration
Install the SDK provider and AI SDK:Peer Dependencies
@mem0/vercel-ai-provider v3.0.0 requires:
aiv6+ (^6.0.199)@ai-sdk/providerv3+ (^3.0.10)- Provider packages at v3+:
@ai-sdk/openai@^3,@ai-sdk/anthropic@^3,@ai-sdk/google@^3,@ai-sdk/groq@^3,@ai-sdk/cohere@^3
Getting Started
Setting Up Mem0
- Get your Mem0 API Key from the Mem0 Dashboard.
-
Initialize the Mem0 Client in your application:
Note: The
openaiprovider is set as default. Consider usingMEM0_API_KEYandOPENAI_API_KEYas environment variables for security.Note: The
mem0Configis optional. It is used to set the global config for the Mem0 Client (eg.user_id,agent_id,app_id,run_idetc). -
Add Memories to Enhance Context:
Standalone Features
For standalone features, such asaddMemories,retrieveMemories, andgetMemories, you must either setMEM0_API_KEYas an environment variable or pass it directly in the function call.
getMemorieswill return raw memories in the form of an array of objects, whileretrieveMemorieswill 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:
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
| Provider | Configuration Value |
|---|---|
| OpenAI | openai |
| Anthropic | anthropic |
| Google / Gemini | google or gemini |
| Groq | groq |
| Cohere | cohere |
Note: You can use eithergeminias the provider value for Google Gemini models. Both map to the@ai-sdk/googlepackage internally.
Configuration Options
Mem0ConfigSettings
These options can be passed per-request when creating a model instance:| Option | Type | Description |
|---|---|---|
user_id | string | User identifier for memory scoping |
agent_id | string | Agent identifier |
app_id | string | Application identifier |
run_id | string | Run/session identifier |
metadata | object | Custom metadata for memories |
filters | object | Filters for memory search |
infer | boolean | Enable inference-based retrieval |
top_k | number | Number of memories to retrieve (default: 10) |
threshold | number | Relevance threshold for search |
rerank | boolean | Enable reranking of results |
page | number | Page number for pagination |
page_size | number | Results per page |
Key Features
createMem0(): Initializes a new Mem0 provider instance implementingProviderV3.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.
Migrating from v2.x
If you’re upgrading from@mem0/vercel-ai-provider v2.x:
- Upgrade AI SDK:
npm install ai@^6and update all@ai-sdk/*provider packages to^3.x - Remove deprecated params: Remove
org_id,project_id,output_format,filter_memories,async_mode,enable_graphfrom your config - 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 - Update imports:
LanguageModelV2Promptis nowLanguageModelV3Promptif you import types directly
Best Practices
-
User Identification: Use a unique
user_idfor consistent memory retrieval. - Memory Cleanup: Regularly clean up unused memory data.
-
Sources: Access
result.sourcesto inspect which memories influenced the response.Note: We also have support for
agent_id,app_id, andrun_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