Skip to content

Memory Operations

Fresh

Mem0 provides four core operations for managing memories: Add, Search, Update, and Delete.

Add Memory

Store user-assistant interactions and facts for later retrieval. Mem0 captures important conversational details into a searchable, structured format.

How Add Works

  1. Information extraction -- LLM pulls key facts, decisions, preferences
  2. Conflict resolution -- Checks existing memories for duplicates/contradictions
  3. Storage -- Places memories in vector storage (plus optional graph storage)

Usage

python
from mem0 import MemoryClient

client = MemoryClient(api_key="your-api-key")

messages = [
    {"role": "user", "content": "I'm planning a trip to Tokyo next month."},
    {"role": "assistant", "content": "Great! I'll remember that for future suggestions."}
]

client.add(messages, user_id="alice")
javascript
import { MemoryClient } from "mem0ai";

const client = new MemoryClient({ apiKey: "your-api-key" });

const messages = [
  { role: "user", content: "I'm planning a trip to Tokyo next month." },
  { role: "assistant", content: "Great! I'll remember that for future suggestions." }
];

await client.add(messages, { user_id: "alice", version: "v2" });
python
from mem0 import Memory

m = Memory()

messages = [
    {"role": "user", "content": "I love sci-fi movies but hate thrillers."},
    {"role": "assistant", "content": "Got it! Sci-fi recommendations coming up."}
]

# Store inferred memories (default)
result = m.add(messages, user_id="alice", metadata={"category": "movies"})

# Store raw messages without inference
result = m.add(messages, user_id="alice", infer=False)
javascript
import { Memory } from 'mem0ai/oss';

const memory = new Memory();

const messages = [
  { role: "user", content: "I like to drink coffee in the morning" }
];

const result = memory.add(messages, {
  userId: "alice",
  metadata: { category: "preferences" }
});

When to Add Memory

Store memories when agents learn:

  • New user preferences
  • Decisions or suggestions
  • Goals or completed tasks
  • New entities
  • User feedback or clarifications

Duplicate Protection

Only activates with infer=True (default). Setting infer=False stores payloads exactly as provided, bypassing deduplication. Mixing both modes for identical content creates duplicates.


Search Memory

Pose natural-language questions and retrieve relevant stored memories using semantic and filtered search.

How Search Works

  1. Query processing -- Natural language is cleaned and enriched
  2. Vector search -- Embeddings locate closest memories using cosine similarity
  3. Filtering and reranking -- Logical filters narrow candidates; rerankers fine-tune ordering
  4. Results delivery -- Formatted memories with metadata and timestamps

Usage

python
from mem0 import MemoryClient

client = MemoryClient(api_key="your-api-key")

results = client.search("What do you know about me?",
    filters={"user_id": "alice"})
javascript
import { MemoryClient } from "mem0ai";

const client = new MemoryClient({ apiKey: "your-api-key" });
const results = await client.search("Any food preferences?", {
    filters: { AND: [{ user_id: "alice" }] }
});
python
from mem0 import Memory

m = Memory()
results = m.search("Should I drink coffee or tea?", user_id="alice")

# With metadata filters
results = m.search("food preferences", user_id="alice",
    filters={"categories": {"contains": "diet"}})
javascript
import { Memory } from 'mem0ai/oss';

const memory = new Memory();
const results = memory.search("food preferences", { userId: "alice" });

Common Filter Patterns

python
# Session context
client.search("query", filters={
    "AND": [
        {"user_id": "alice"},
        {"agent_id": "chatbot"},
        {"run_id": "session-123"}
    ]
})

# Date range (Platform only)
client.search("recent memories", filters={
    "AND": [
        {"user_id": "alice"},
        {"created_at": {"gte": "2024-07-01"}}
    ]
})

# Categories (Platform only)
client.search("preferences", filters={
    "AND": [
        {"user_id": "alice"},
        {"categories": {"contains": "food"}}
    ]
})

Update Memory

Modify existing memories by changing content or metadata without deletion.

Usage

python
from mem0 import MemoryClient

client = MemoryClient(api_key="your-api-key")

client.update(
    memory_id="your_memory_id",
    text="Updated memory content",
    metadata={"category": "profile-update"},
    timestamp="2025-01-15T12:00:00Z"
)
javascript
import MemoryClient from 'mem0ai';

const client = new MemoryClient({ apiKey: "your-api-key" });

await client.update("your_memory_id", {
  text: "Updated memory content",
  metadata: { category: "profile-update" },
  timestamp: "2025-01-15T12:00:00Z"
});
python
from mem0 import Memory

memory = Memory()
memory.update(memory_id="mem_123", data="Alex now prefers decaf coffee")

Batch Update (Platform)

Update up to 1000 memories in a single call:

python
update_memories = [
    {"memory_id": "id1", "text": "Watches football"},
    {"memory_id": "id2", "text": "Likes to travel"}
]

response = client.batch_update(update_memories)

Delete Memory

Remove memories individually, in bulk, or through filter-based queries.

Usage

python
from mem0 import MemoryClient

client = MemoryClient(api_key="your-api-key")

# Single deletion
client.delete(memory_id="your_memory_id")

# Delete all for a user
client.delete_all(user_id="alice")

# Delete all for an agent
client.delete_all(agent_id="support-bot")

# Full project wipe
client.delete_all(user_id="*", agent_id="*", app_id="*", run_id="*")
javascript
import MemoryClient from 'mem0ai';

const client = new MemoryClient({ apiKey: "your-api-key" });

// Single deletion
await client.delete("your_memory_id");

// Delete all for a user
await client.deleteAll({ user_id: "alice" });

// Full project wipe
await client.deleteAll({ user_id: "*", agent_id: "*" });
python
from mem0 import Memory

memory = Memory()
memory.delete(memory_id="mem_123")
memory.delete_all(user_id="alice")

Batch Delete (Platform)

python
delete_memories = [
    {"memory_id": "id1"},
    {"memory_id": "id2"}
]

response = client.batch_delete(delete_memories)

Method Comparison

MethodPurposeIDs RequiredFilters
delete(memory_id)Exact record removalYesNo
batch_delete([...])Multiple ID purgeYesNo
delete_all(...)Entity-based forgettingNoYes

SOP Documentation Site for Mem0