mem0-vercel-ai-sdk
Mem0 provider for Vercel AI SDK (@mem0/vercel-ai-provider). TRIGGER when: user mentions "vercel ai sdk", "@mem0/vercel-ai-provider", "createMem0", "retrieveMemories", "addMemories", "getMemories", "searchMemories", "mem0 vercel", "AI SDK provider", "AI SDK memory", or is using generateText/streamText with mem0. Also triggers for Next.js apps needing memory-augmented AI. DO NOT TRIGGER when: user asks about direct Python/TS SDK calls without Vercel (use mem0 skill), or CLI terminal commands (use mem0-cli skill).
- License 许可
- Apache-2.0
- 最近更新
- 2026-08-31
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- mem0-vercel-ai-sdk
- license
- Apache-2.0
- metadata
- { "tags": "vercel, ai-sdk, memory, nextjs, typescript, provider", "author": "mem0ai", "version": "1.1.0", "category": "ai-memory" }
- description
- Mem0 provider for Vercel AI SDK (@mem0/vercel-ai-provider). TRIGGER when: user mentions "vercel ai sdk", "@mem0/vercel-ai-provider", "createMem0", "retrieveMemories", "addMemories", "getMemories", "searchMemories", "mem0 vercel", "AI SDK provider", "AI SDK memory", or is using generateText/streamText with mem0. Also triggers for Next.js apps needing memory-augmented AI. DO NOT TRIGGER when: user asks about direct Python/TS SDK calls without Vercel (use mem0 skill), or CLI terminal commands (use mem0-cli skill).
- compatibility
- Node.js 18+, npm install @mem0/vercel-ai-provider, Vercel AI SDK v5 (ai package), MEM0_API_KEY + LLM provider API key
02 资源文件 5
- 许可证LICENSE10.5 KB
- 参考文档README.md3.2 KB
- 参考文档references/memory-utilities.md9.5 KB
- 参考文档references/provider-api.md9.1 KB
- 参考文档references/usage-patterns.md9.5 KB
03 SKILL.md 正文
Mem0 Vercel AI SDK Provider
Memory-enhanced AI provider for Vercel AI SDK. Automatically retrieves and stores memories during LLM calls.
Step 1: Install
npm install @mem0/vercel-ai-provider ai
Step 2: Set up environment variables
export MEM0_API_KEY="m0-xxx"
export OPENAI_API_KEY="sk-xxx" # or ANTHROPIC_API_KEY, GOOGLE_API_KEY, etc.
Get a Mem0 API key at: https://app.mem0.ai/dashboard/api-keys?utm_source=oss&utm_medium=skill-mem0-vercel-ai-sdk
Pattern 1: Wrapped Model
The wrapped model approach is the simplest. createMem0 returns a provider that wraps any supported LLM with automatic memory retrieval and storage.
import { generateText } from "ai";
import { createMem0 } from "@mem0/vercel-ai-provider";
const mem0 = createMem0();
const { text } = await generateText({
model: mem0("gpt-5-mini", { user_id: "alice" }),
prompt: "Recommend a restaurant",
});
What happens under the hood:
- The prompt is sent to Mem0 search (
POST /v3/memories/search/) to retrieve relevant memories - Retrieved memories are injected as a system message at the start of the prompt
- The underlying LLM (e.g., OpenAI gpt-5-mini) generates a response using the enriched prompt
- The conversation is stored back to Mem0 (
POST /v3/memories/add/) as a fire-and-forget async call (no await)
Pattern 2: Standalone Utilities
Use standalone utilities when you want full control over the memory retrieve/store cycle, or you want to use a provider that is already configured separately.
import { openai } from "@ai-sdk/openai";
import { generateText } from "ai";
import { retrieveMemories, addMemories } from "@mem0/vercel-ai-provider";
const prompt = "Recommend a restaurant";
// Retrieve memories -- returns a formatted system prompt string
const memories = await retrieveMemories(prompt, {
user_id: "alice",
mem0ApiKey: "m0-xxx",
});
// Generate using any provider with injected memories
const { text } = await generateText({
model: openai("gpt-5-mini"),
prompt,
system: memories,
});
// Optionally store the conversation back
await addMemories(
[
{ role: "user", content: [{ type: "text", text: prompt }] },
{ role: "assistant", content: [{ type: "text", text }] },
],
{ user_id: "alice", mem0ApiKey: "m0-xxx" }
);
Pattern 3: Streaming
Use streamText for streaming responses with memory augmentation:
import { streamText } from "ai";
import { createMem0 } from "@mem0/vercel-ai-provider";
const mem0 = createMem0();
const result = streamText({
model: mem0("gpt-5-mini", { user_id: "alice" }),
prompt: "What should I cook for dinner?",
});
for await (const chunk of result.textStream) {
process.stdout.write(chunk);
}
The wrapped model handles memory retrieval before streaming begins and stores the conversation after.
Supported Providers
| Provider | Config value | Required env var |
|---|---|---|
| OpenAI (default) | "openai" |
OPENAI_API_KEY |
| Anthropic | "anthropic" |
ANTHROPIC_API_KEY |
"google" |
GOOGLE_GENERATIVE_AI_API_KEY |
|
| Groq | "groq" |
GROQ_API_KEY |
| Cohere | "cohere" |
COHERE_API_KEY |
Select a provider when creating the Mem0 instance:
const mem0 = createMem0({ provider: "anthropic" });
const { text } = await generateText({
model: mem0("gpt-5-mini", { user_id: "alice" }),
prompt: "Hello!",
});
How It Works Internally
Wrapped model flow
User prompt
--> searchInternalMemories (POST /v3/memories/search/)
--> memories injected as system message at start of prompt
--> underlying LLM generates response (doGenerate or doStream)
--> processMemories fires addMemories as fire-and-forget (no await)
--> response returned to caller
Standalone flow
User controls each step:
1. retrieveMemories / getMemories / searchMemories -> fetch memories
2. inject into system prompt manually
3. call generateText / streamText with any provider
4. addMemories -> store new conversation to Mem0
Key Differences Between the 4 Utility Functions
| Function | Returns | Use when |
|---|---|---|
retrieveMemories |
Formatted system prompt string | Injecting directly into system parameter |
getMemories |
Raw memory array | Processing memories programmatically |
searchMemories |
Full search response (results + relations) | Need relations, scores, metadata |
addMemories |
API response | Storing new messages to Mem0 |
All four accept LanguageModelV2Prompt | string as the first argument and optional Mem0ConfigSettings as the second.
Common Edge Cases and Tips
- Always provide
user_id(oragent_id/app_id/run_id) for consistent memory retrieval. Without an entity identifier, memories cannot be scoped. - Standalone utilities require explicit API key: pass
mem0ApiKeyin the config object, or set theMEM0_API_KEYenvironment variable. - This uses Vercel AI SDK v5 (LanguageModelV2 / ProviderV2 interfaces). It is not compatible with AI SDK v3 or v4.
processMemoriesfiresaddMemoriesas fire-and-forget (.then()withoutawait). Memory storage happens asynchronously and does not block the LLM response.- The
"gemini"alias exists in the provider switch but is NOT in thesupportedProviderslist. Use"google"instead. - Custom host: set
hostin the config to point to a different Mem0 API endpoint (default:https://api.mem0.ai).
References
| Topic | File |
|---|---|
Provider API (createMem0, Mem0Provider, types) |
local / GitHub |
Memory utilities (addMemories, retrieveMemories, etc.) |
local / GitHub |
| Usage patterns and examples | local / GitHub |
Related Mem0 Skills
| Skill | When to use | Link |
|---|---|---|
| mem0 | Python/TypeScript SDK, REST API, framework integrations | local / GitHub |
| mem0-cli | Terminal commands, scripting, CI/CD, agent tool loops | local / GitHub |
04 同仓库更多 Skills
skill-installer
Install Codex skills into $CODEX_HOME/skills from a curated list or a GitHub repo path. Use when a user asks to list installable skills, install a curated skill, or install a skill from another repo (including private repos).
skill-creator
Guide for creating effective skills. This skill should be used when users want to create a new skill (or update an existing skill) that extends Codex's capabilities with specialized knowledge, workflows, or tool integrations.
openai-docs
Use for Codex models/pricing, scheduled tasks, skills, settings, setup, troubleshooting, customization, automations, and self-knowledge—including 'you,' 'your,' 'this app,' or 'this coding agent' when they refer to Codex—and for OpenAI APIs/products and ChatGPT Work. Also use for model choice/migration, prompting, SDKs, Responses, Realtime, agents, evals, and Chat/Work/Codex comparisons. Do not use for generic app/software tasks that merely mention Codex.
imagegen
Generate or edit raster images when the task benefits from AI-created bitmap visuals such as photos, illustrations, textures, sprites, mockups, or transparent-background cutouts. Use when Codex should create a brand-new image, transform an existing image, or derive visual variants from references, and the output should be a bitmap asset rather than repo-native code or vector. Do not use when the task is better handled by editing existing SVG/vector/code-native assets, extending an established icon or logo system, or building the visual directly in HTML/CSS/canvas.
skill-creator-cc
Create new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, update or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy.
prd-writing-guide
Write complete, unambiguous PRDs that development teams can implement without guesswork. Includes requirement discovery framework, structured documentation methodology, completeness checklists, and common pitfall avoidance. Use when: writing new PRDs, reviewing PRD drafts, validating requirement completeness, preparing for engineering handoff. Triggers: 'write PRD', '写PRD', '产品需求文档', '需求文档', '需求规格', '需求评审', '完善需求', 'create requirements doc', 'product requirements', 'feature spec', 'requirements document'. Anti-triggers: 'technical design doc', 'architecture design', 'implementation plan', 'API design', '架构设计', '技术方案', '实现方案', '接口设计'.