Cookbook
Small, working patterns you can paste and adapt.
1. Per-end-user memory (container tags)#
def store_for(user_id, text):
mem.add(text, collection="app", container_tag=f"user-{user_id}")
def recall_for(user_id, query):
return mem.search(query, collection="app",
container_tag=f"user-{user_id}", limit=5)
One collection, isolated per user at query time — no cross-user leakage, no per-user collection sprawl.
2. Recall-first agent loop#
def answer(user_id, message):
hits = recall_for(user_id, message)
context = "\n".join(f"- {h['text']}" for h in hits)
reply = llm(f"Known about this user:\n{context}\n\nUser: {message}")
mem.remember(message, collection="app", sync=False) # learn in the background
return reply
Search before answering; remember after — async, so it never adds
latency to the reply.
3. Nightly SharePoint sync#
# /etc/cron.d/longmem-sharepoint (state file makes re-runs incremental)
0 2 * * * app cd /opt/connectors && GRAPH_TENANT_ID=… GRAPH_CLIENT_ID=… \
GRAPH_CLIENT_SECRET=… SHAREPOINT_SITE=https://contoso.sharepoint.com/sites/kb \
LONGMEM_API_KEY=mv_… python3 import_sharepoint.py --collection kb
The id+eTag state file means only new or changed files import each night.
4. Any source → memory with dlt#
import dlt
from dlt_longmem import longmem
pipe = dlt.pipeline(destination=longmem(collection="crm", text_field="notes"))
pipe.run(sql_database(table="accounts")) # or notion / slack / gdrive sources
5. Profile as system prompt (cached per session)#
_profiles = {}
def system_prompt(user_id):
if user_id not in _profiles: # 1 LLM call per session, not per msg
_profiles[user_id] = mem.profile(f"user-{user_id}")["profile"]
return f"You are a helpful assistant.\nAbout this user: {_profiles[user_id]}"
6. Infinite chat (memory proxy — zero glue)#
Point any OpenAI client at the proxy: relevant memories are injected as context and the user's turn is remembered automatically — no search/inject/store code of your own.
from openai import OpenAI
client = OpenAI(base_url="https://rememberos.ai/v1/proxy", api_key="mv_…")
r = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "what theme does Alex prefer?"}],
extra_body={"collection": "app", "container_tag": "user-alex"}, # scope to one user
)
print(r.choices[0].message.content) # answered from memory
curl -s https://rememberos.ai/v1/proxy/chat/completions \
-H "Authorization: Bearer mv_…" -H "Content-Type: application/json" \
-d '{"messages":[{"role":"user","content":"what theme does Alex prefer?"}],
"collection":"app","container_tag":"user-alex"}'
OpenAI-compatible response plus a longmem field
(memories_used, stored). store defaults to true so
memory grows every turn; set it false for read-only calls.
7. Keep shared memory clean (intelligent memory)#
As agents pour memories into one shared collection, it drifts: near-duplicates pile up, old facts go stale, some matter more than others. The intelligent-memory ops keep it tidy — here is a maintenance pass plus the two recall helpers, all via the SDK.
# preview duplicates before deleting anything (dry run is the default)
preview = mem.dedup(collection="team", threshold=0.9, dry_run=True)
print(preview["duplicate_count"], "near-duplicates across", len(preview["clusters"]), "clusters")
# protect the ones that must never be consolidated away, then consolidate for real
mem.pin(important_id, collection="team") # pinned memories are always the keeper
mem.dedup(collection="team", threshold=0.9, dry_run=False)
# retire memories nothing has touched in 90 days (pinned ones are skipped; restorable)
mem.archive_stale(collection="team", older_than_days=90, dry_run=False)
# recall helpers
neighbours = mem.related(memory_id, collection="team", limit=5) # "more like this"
for c in mem.contradictions(collection="team"): # what changed over time
print("now:", c["current"]["text"], "— was:", c["superseded"]["text"])
Run the dedup + archive pass on a schedule (nightly cron) and your shared memory stays focused without anyone curating it by hand. Everything here is also a plain REST call — see the Intelligent memory reference.