Token budget for the default sliding window. Ignored when conversation_memory is provided.
tokenizer
Tokenizer \| None
None
Custom tokenizer. Falls back to the built-in counter.
on_evict
Callable \| None
None
Callback for evicted turns. Ignored when conversation_memory is provided.
persistent_store
MemoryEntryStore \| None
None
Store for long-term facts.
conversation_memory
ConversationMemory \| None
None
Custom conversation backend. Overrides the default sliding window.
graph
GraphIndexer \| None
None
Keeps a knowledge graph in step with the store (see Knowledge Graph).
extractor
MemoryExtractor \| None
None
Turns recent turns into facts on remember() (e.g. LLMExtractor).
consolidator
MemoryConsolidator \| None
None
Decides ADD/UPDATE/DELETE/NONE for each fact (e.g. LLMConsolidator). Without one every fact is added.
extract_window
int
10
Cap on the not-yet-remembered turns (the newest) the extractor sees per remember().
remember_every
int
1
after_turn() runs remember() every N completed turns; 0 = only on an explicit call.
callbacks
list[MemoryCallback] \| None
None
Observers for on_extraction / on_consolidation.
Property
Type
Description
conversation
ConversationMemory
The underlying conversation memory instance.
conversation_type
str
Returns "sliding_window", "summary_buffer", or the class name.
persistent_store
MemoryEntryStore \| None
The persistent store, if configured.
Method
Returns
Description
add_user_message(content)
None
Add a user turn.
add_assistant_message(content)
None
Add an assistant turn.
add_system_message(content)
None
Add a system turn.
add_tool_message(content)
None
Add a tool turn.
add_fact(content, tags, memory_type, metadata)
MemoryEntry
Store a persistent fact with content-hash deduplication. Raises StorageError without a store.
get_relevant_facts(query, top_k=5)
list[MemoryEntry]
Search persistent store. Returns [] without a store.
get_all_facts()
list[MemoryEntry]
Return all persistent entries.
delete_fact(entry_id)
bool
Delete a fact by ID. Returns False if not found.
update_fact(entry_id, content)
MemoryEntry \| None
Update fact content. Returns None if not found.
remember()
list[tuple[MemoryOperation, MemoryEntry \| None]]
Extract facts from the turns not yet remembered (at most extract_window, the newest) and consolidate them into the store; returns the operations applied (DELETE = soft delete). Empty without extractor/store/new turns. Errors propagate, and the turns stay for the next attempt.
after_turn()
list[tuple[MemoryOperation, MemoryEntry \| None]]
Called by Agent after each completed turn; runs remember() every remember_every turns. Failures are logged, never raised.
get_context_items(priority=7)
list[ContextItem]
Assemble context items. Facts at priority 8, conversation at given priority.
The entity graph moved to anchor.graph and now spans memory and documents: see Knowledge Graph. MemoryManager(graph=GraphIndexer(...)) keeps it in step with the persistent store.
The update phase of the mem0 paper: one model call decides ADD / UPDATE / DELETE / NONE per new fact against the most similar memories. Opt-in; the provider is injected.
With it, similarity is cosine and a fact whose best cosine against the store is below new_threshold is ADD without a call. Without it, similarity is word overlap (recency breaks ties).
new_threshold
float
0.3
Cosine below which a fact is novel (only with embed_fn). In [0.0, 1.0].
top_k
int
5
Most similar memories per fact shown to the model.
max_candidates
int
20
Cap on memories shown to the model; a fact none of whose top_k made the cap is ADD without a call.
max_cache_size
int
1000
LRU cache of embeddings, keyed by text.
Method
Returns
Description
consolidate(new_entries, existing)
list[tuple[MemoryOperation, MemoryEntry \| None]]
Exact duplicates → NONE without a call; empty existing → ADD without a call; otherwise one call. UPDATE keeps the target id, recomputes content_hash, records metadata["previous_content"]; DELETE returns the target invalidated (MemoryEntry.invalidate); unknown update target → ADD, unknown delete target → ignored, a fact the model leaves out → ADD; a malformed or failed answer → ADD for every fact, with a warning.
Store to prune. Must support list_all_unfiltered().
decay
MemoryDecay \| None
None
Decay function. Without it, only expiry pruning runs.
callbacks
list[MemoryCallback] \| None
None
Callbacks notified of pruning events.
retention
timedelta \| None
None
Keep expired entries this long before deleting them — an invalidated (soft-deleted) fact stays readable through list_all_unfiltered as history. None deletes on the first collection.
Build entries from user function output. Raises ValueError if "content" missing.
Example
fromanchorimportCallbackExtractor,MemoryTypefromanchor.models.memoryimportConversationTurndefmy_extractor(turns):return[{"content":f"Discussed: {turns[0].content[:40]}","tags":["topic"]}]extractor=CallbackExtractor(extract_fn=my_extractor)turns=[ConversationTurn(role="user",content="Tell me about Python async")]entries=extractor.extract(turns)print(entries[0].content)# "Discussed: Tell me about Python async"print(entries[0].memory_type)# MemoryType.SEMANTIC
Only turns with these roles are read; no call when none remain (tool turns are noise).
default_type
MemoryType
MemoryType.SEMANTIC
Type when the model gives none or an unknown one.
Method
Returns
Description
extract(turns)
list[MemoryEntry]
One self-contained fact per entry (content, optional tags, memory_type), deduplicated within the batch, source_turns = the turns read. A malformed or failed answer returns [] with a warning.