Memory Operations
FreshMem0 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
- Information extraction -- LLM pulls key facts, decisions, preferences
- Conflict resolution -- Checks existing memories for duplicates/contradictions
- Storage -- Places memories in vector storage (plus optional graph storage)
Usage
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")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" });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)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
- Query processing -- Natural language is cleaned and enriched
- Vector search -- Embeddings locate closest memories using cosine similarity
- Filtering and reranking -- Logical filters narrow candidates; rerankers fine-tune ordering
- Results delivery -- Formatted memories with metadata and timestamps
Usage
from mem0 import MemoryClient
client = MemoryClient(api_key="your-api-key")
results = client.search("What do you know about me?",
filters={"user_id": "alice"})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" }] }
});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"}})import { Memory } from 'mem0ai/oss';
const memory = new Memory();
const results = memory.search("food preferences", { userId: "alice" });Common Filter Patterns
# 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
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"
)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"
});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:
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
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="*")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: "*" });from mem0 import Memory
memory = Memory()
memory.delete(memory_id="mem_123")
memory.delete_all(user_id="alice")Batch Delete (Platform)
delete_memories = [
{"memory_id": "id1"},
{"memory_id": "id2"}
]
response = client.batch_delete(delete_memories)Method Comparison
| Method | Purpose | IDs Required | Filters |
|---|---|---|---|
delete(memory_id) | Exact record removal | Yes | No |
batch_delete([...]) | Multiple ID purge | Yes | No |
delete_all(...) | Entity-based forgetting | No | Yes |