docs/integrations/codex.mdx
Add persistent memory to OpenAI Codex with the Mem0 plugin. Codex forgets everything between tasks — this plugin fixes that by connecting to Mem0's cloud memory layer via MCP, automatically capturing learnings at key lifecycle points, and retrieving relevant context before every response.
Before setting up Mem0 with Codex, ensure you have:
A Mem0 Platform account and API key:
m0-)OpenAI Codex access
Your API key added to your shell profile (persists across sessions):
echo 'export MEM0_API_KEY="m0-your-api-key"' >> ~/.bashrc
source ~/.bashrc
Install the full plugin including MCP server, lifecycle hooks, and SDK skill.
Add the Mem0 marketplace:
codex plugin marketplace add mem0ai/mem0
Restart Codex, open the Plugin Directory, browse the Mem0 Plugins marketplace, and install Mem0.
The fastest way to connect Codex to Mem0 — no plugin, no marketplace. Add to ~/.codex/config.toml:
[mcp_servers.mem0]
url = "https://mcp.mem0.ai/mcp"
bearer_token_env_var = "MEM0_API_KEY"
Make sure MEM0_API_KEY is exported in the shell you launch Codex from, then restart Codex.
This gives you the MCP tools but not the lifecycle hooks or SDK skill.
codex plugin marketplace upgrade # pull latest plugin versions
codex plugin marketplace remove mem0-plugins # unregister the marketplace
To update, run codex plugin marketplace upgrade to pull the latest from the Mem0 repo.
| Component | Plugin Install | MCP Only |
|---|---|---|
| MCP Server (9 memory tools) | Yes | Yes |
| Lifecycle Hooks | Yes | No |
| Mem0 SDK Skill | Yes | No |
Once installed, the following tools are available in every Codex session:
| Tool | Description |
|---|---|
add_memory | Save text or conversation history for a user/agent |
search_memories | Semantic search across memories with filters |
get_memories | List memories with filters and pagination |
get_memory | Retrieve a specific memory by ID |
update_memory | Overwrite a memory's text by ID |
delete_memory | Delete a single memory by ID |
delete_all_memories | Bulk delete all memories in scope |
delete_entities | Delete a user/agent/app/run entity and its memories |
list_entities | List users/agents/apps/runs stored in Mem0 |
When installed via the plugin marketplace, Mem0 hooks into Codex's lifecycle to automatically manage memory:
| Hook | Event | What it does |
|---|---|---|
| Session start | SessionStart | Loads prior memories and displays status banner |
| User prompt | UserPromptSubmit | Searches relevant memories before each message |
| Pre-tool | PreToolUse | Blocks MEMORY.md writes, enforces user_id/app_id on mem0 tool calls |
| Post-tool | PostToolUse | Tracks stats, scans bash errors for related memories |
| Pre-compact | PreCompact | Stores a session summary before context compaction |
# Task 1: Setting up a new service
You: Create a REST API for the notifications service using Express and TypeScript.
# Codex searches memories, finds user preferences from prior tasks.
# After completing the task, Mem0 stores:
# - Decision: "Notifications service uses Express + TypeScript + Zod validation"
# - Convention: "All API routes follow /api/v1/{resource} pattern"
# - Preference: "User prefers explicit error types over generic catch-all"
# Task 2 (days later): Extending the service
You: Add WebSocket support for real-time notification delivery.
# Codex searches memories, retrieves the architecture decisions and conventions.
# Follows the same patterns established in the first task.
MEM0_API_KEY is set: echo $MEM0_API_KEYmem0 MCP / "tool collision" errors — You combined Option A with Option B. Remove the [mcp_servers.mem0] block from ~/.codex/config.toml; the plugin registers it automatically