Add Semantic Recall with Mem0
Turn Maya's explicit record into searchable memory while keeping scope, review, and deletion visible.
Why semantic search changes retrieval
Yesterday's exact database lookup cannot answer “How should we update Maya?” unless code knows the exact fact wording. Semantic retrieval finds related meaning, so “send me updates by email” can recall Maya's email-summary preference.
Mem0's documentation describes a memory layer that extracts, stores, and searches memories. Treat its extraction as a proposal generator, not an authority: your application still owns tenant scope and retention.
Add and search one scoped memory
Mem0's user_id groups a user's memories. Use a tenant-qualified value rather than bare maya, so identical user IDs across tenants cannot collide.
from mem0 import Memory
memory = Memory()
scope = "acme:maya"
memory.add(
"Maya said she prefers email summaries for support updates.",
user_id=scope,
metadata={"tenant_id": "acme", "source": "user stated"},
)
results = memory.search("How should we send Maya an update?", user_id=scope)
print(results)
Inspect results before using it in a reply. APIs and provider configuration vary; pin an SDK version and verify against Mem0's Python quickstart in your environment.
Put limits around automatic extraction
Automatic memory can save effort but can also preserve a guess, an obsolete instruction, or sensitive content. Define an allowlist and expiry before accepting extracted facts.
ALLOWED_KINDS = {"communication_preference", "account_constraint"}
def accept_memory(kind: str, text: str, source: str) -> bool:
return kind in ALLOWED_KINDS and source == "user stated" and len(text) < 240
assert accept_memory("communication_preference", "Maya prefers email", "user stated")
assert not accept_memory("secret", "Maya's token is ...", "user stated")
This filter is example policy, not protection against prompt injection by itself. Run it before persistence, record reviewer or automation version, and delete rejected candidates rather than leaving them searchable.
Evaluate recall, not vibes
A memory system fails quietly when it returns irrelevant facts with confidence. Build fixtures: Maya's update query must retrieve her preference; a different tenant's query must not; a request for deletion must stop retrieval after erase.
def contains(results, phrase: str) -> bool:
return any(phrase.lower() in item["memory"].lower() for item in results)
maya_results = memory.search("send Maya an update", user_id="acme:maya")
assert contains(maya_results, "email summaries")
other_results = memory.search("send Maya an update", user_id="other-tenant:maya")
assert not contains(other_results, "email summaries")
Day 03 consumes these fixtures to compare products that remember graph progress, session transcripts, editable blocks, or a temporal knowledge graph. None replaces your tenant filter.