SKILL.md
SKILL.mdBrowse 6 files
1,995 tokens
7,475 bytes
Token encoding: o200k_base
Snapshot 0df3e4b
1---2name: mem0-vercel-ai-sdk3description: >4 Mem0 provider for Vercel AI SDK (@mem0/vercel-ai-provider).5 TRIGGER when: user mentions "vercel ai sdk", "@mem0/vercel-ai-provider",6 "createMem0", "retrieveMemories", "addMemories", "getMemories",7 "searchMemories", "mem0 vercel", "AI SDK provider", "AI SDK memory",8 or is using generateText/streamText with mem0. Also triggers for Next.js9 apps needing memory-augmented AI.10 DO NOT TRIGGER when: user asks about direct Python/TS SDK calls without Vercel11 (use mem0 skill), or CLI terminal commands (use mem0-cli skill).12license: Apache-2.013metadata:14 author: mem0ai15 version: "1.1.0"16 category: ai-memory17 tags: "vercel, ai-sdk, memory, nextjs, typescript, provider"18compatibility: Node.js 18+, npm install @mem0/vercel-ai-provider, Vercel AI SDK v5 (ai package), MEM0_API_KEY + LLM provider API key19---20 21# Mem0 Vercel AI SDK Provider22 23Memory-enhanced AI provider for Vercel AI SDK. Automatically retrieves and stores memories during LLM calls.24 25## Step 1: Install26 27```bash28npm install @mem0/vercel-ai-provider ai29```30 31## Step 2: Set up environment variables32 33```bash34export MEM0_API_KEY="m0-xxx"35export OPENAI_API_KEY="sk-xxx" # or ANTHROPIC_API_KEY, GOOGLE_API_KEY, etc.36```37 38Get a Mem0 API key at: https://app.mem0.ai/dashboard/api-keys?utm_source=oss&utm_medium=skill-mem0-vercel-ai-sdk39 40## Pattern 1: Wrapped Model41 42The wrapped model approach is the simplest. `createMem0` returns a provider that wraps any supported LLM with automatic memory retrieval and storage.43 44```typescript45import { generateText } from "ai";46import { createMem0 } from "@mem0/vercel-ai-provider";47 48const mem0 = createMem0();49const { text } = await generateText({50 model: mem0("gpt-5-mini", { user_id: "alice" }),51 prompt: "Recommend a restaurant",52});53```54 55What happens under the hood:561. The prompt is sent to Mem0 search (`POST /v3/memories/search/`) to retrieve relevant memories572. Retrieved memories are injected as a system message at the start of the prompt583. The underlying LLM (e.g., OpenAI gpt-5-mini) generates a response using the enriched prompt594. The conversation is stored back to Mem0 (`POST /v3/memories/add/`) as a fire-and-forget async call (no await)60 61## Pattern 2: Standalone Utilities62 63Use 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.64 65```typescript66import { openai } from "@ai-sdk/openai";67import { generateText } from "ai";68import { retrieveMemories, addMemories } from "@mem0/vercel-ai-provider";69 70const prompt = "Recommend a restaurant";71 72// Retrieve memories -- returns a formatted system prompt string73const memories = await retrieveMemories(prompt, {74 user_id: "alice",75 mem0ApiKey: "m0-xxx",76});77 78// Generate using any provider with injected memories79const { text } = await generateText({80 model: openai("gpt-5-mini"),81 prompt,82 system: memories,83});84 85// Optionally store the conversation back86await addMemories(87 [88 { role: "user", content: [{ type: "text", text: prompt }] },89 { role: "assistant", content: [{ type: "text", text }] },90 ],91 { user_id: "alice", mem0ApiKey: "m0-xxx" }92);93```94 95## Pattern 3: Streaming96 97Use `streamText` for streaming responses with memory augmentation:98 99```typescript100import { streamText } from "ai";101import { createMem0 } from "@mem0/vercel-ai-provider";102 103const mem0 = createMem0();104const result = streamText({105 model: mem0("gpt-5-mini", { user_id: "alice" }),106 prompt: "What should I cook for dinner?",107});108 109for await (const chunk of result.textStream) {110 process.stdout.write(chunk);111}112```113 114The wrapped model handles memory retrieval before streaming begins and stores the conversation after.115 116## Supported Providers117 118| Provider | Config value | Required env var |119|----------|-------------|------------------|120| OpenAI (default) | `"openai"` | `OPENAI_API_KEY` |121| Anthropic | `"anthropic"` | `ANTHROPIC_API_KEY` |122| Google | `"google"` | `GOOGLE_GENERATIVE_AI_API_KEY` |123| Groq | `"groq"` | `GROQ_API_KEY` |124| Cohere | `"cohere"` | `COHERE_API_KEY` |125 126Select a provider when creating the Mem0 instance:127 128```typescript129const mem0 = createMem0({ provider: "anthropic" });130const { text } = await generateText({131 model: mem0("gpt-5-mini", { user_id: "alice" }),132 prompt: "Hello!",133});134```135 136## How It Works Internally137 138### Wrapped model flow139 140```141User prompt142 --> searchInternalMemories (POST /v3/memories/search/)143 --> memories injected as system message at start of prompt144 --> underlying LLM generates response (doGenerate or doStream)145 --> processMemories fires addMemories as fire-and-forget (no await)146 --> response returned to caller147```148 149### Standalone flow150 151```152User controls each step:153 1. retrieveMemories / getMemories / searchMemories -> fetch memories154 2. inject into system prompt manually155 3. call generateText / streamText with any provider156 4. addMemories -> store new conversation to Mem0157```158 159## Key Differences Between the 4 Utility Functions160 161| Function | Returns | Use when |162|----------|---------|----------|163| `retrieveMemories` | Formatted system prompt **string** | Injecting directly into `system` parameter |164| `getMemories` | Raw memory **array** | Processing memories programmatically |165| `searchMemories` | Full search **response** (results + relations) | Need relations, scores, metadata |166| `addMemories` | API response | Storing new messages to Mem0 |167 168All four accept `LanguageModelV2Prompt | string` as the first argument and optional `Mem0ConfigSettings` as the second.169 170## Common Edge Cases and Tips171 172- **Always provide `user_id`** (or `agent_id`/`app_id`/`run_id`) for consistent memory retrieval. Without an entity identifier, memories cannot be scoped.173- **Standalone utilities require explicit API key**: pass `mem0ApiKey` in the config object, or set the `MEM0_API_KEY` environment variable.174- **This uses Vercel AI SDK v5** (LanguageModelV2 / ProviderV2 interfaces). It is not compatible with AI SDK v3 or v4.175- **`processMemories` fires `addMemories` as fire-and-forget** (`.then()` without `await`). Memory storage happens asynchronously and does not block the LLM response.176- **The `"gemini"` alias** exists in the provider switch but is NOT in the `supportedProviders` list. Use `"google"` instead.177- **Custom host**: set `host` in the config to point to a different Mem0 API endpoint (default: `https://api.mem0.ai`).178 179## References180 181| Topic | File |182|-------|------|183| Provider API (`createMem0`, `Mem0Provider`, types) | [local](references/provider-api.md) / [GitHub](https://github.com/mem0ai/mem0/tree/main/skills/mem0-vercel-ai-sdk/references/provider-api.md) |184| Memory utilities (`addMemories`, `retrieveMemories`, etc.) | [local](references/memory-utilities.md) / [GitHub](https://github.com/mem0ai/mem0/tree/main/skills/mem0-vercel-ai-sdk/references/memory-utilities.md) |185| Usage patterns and examples | [local](references/usage-patterns.md) / [GitHub](https://github.com/mem0ai/mem0/tree/main/skills/mem0-vercel-ai-sdk/references/usage-patterns.md) |186 187## Related Mem0 Skills188 189| Skill | When to use | Link |190|-------|-------------|------|191| mem0 | Python/TypeScript SDK, REST API, framework integrations | [local](../mem0/SKILL.md) / [GitHub](https://github.com/mem0ai/mem0/tree/main/skills/mem0) |192| mem0-cli | Terminal commands, scripting, CI/CD, agent tool loops | [local](../mem0-cli/SKILL.md) / [GitHub](https://github.com/mem0ai/mem0/tree/main/skills/mem0-cli) |193 Discovery context
Discovered by repository scan. No exact path reference found in the snapshot’s root AGENTS.md.