Mem0mem0ai 2.0.20 · Python 3.10+
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
24 small wins to finish your pathNext lesson

add(): what gets kept and what gets thrown away

add is the call that does the most work and shows the least. One line of conversation goes in; what comes back is a decision the model made.

Example
from pretend_mem0 import memory

shop = memory()
result = shop.add("My order A17 is late. Thanks for the help!", user_id="ravi")
for stored in result["results"]:
    print(stored["event"], "|", stored["memory"])

Two sentences went in and one memory came out. Thanks for the help! was dropped, because the model decided it was not worth keeping, which is the judgement the dictionary in lesson 2 could not make.

What comes back

add returns a dictionary with a results list, one entry per decision. Each entry carries the memory text, an event saying what happened to it, and an id you can use later.

Example
from pretend_mem0 import memory

shop = memory()
stored = shop.add("Deliver to my office reception.", user_id="ravi")["results"][0]
print(sorted(stored))
print(stored["event"])

event is ADD here. It can also be UPDATE or DELETE, because Mem0 compares what it just heard against what it already knows, which is lesson 15.

Messages, not just strings

A string is a convenience. The real input is a list of messages, which is what you have in a chat application, and it lets the model use the assistant's half of the conversation too.

Example
from pretend_mem0 import memory

shop = memory()
result = shop.add([
    {"role": "user", "content": "Where is my order?"},
    {"role": "assistant", "content": "Order A17 is arriving on Friday."},
], user_id="ravi")
for stored in result["results"]:
    print(stored["memory"])
Every argument after the messages is keyword only. shop.add("text", "ravi") raises a TypeError rather than guessing that the second string was a user id. You always write user_id="ravi".

And at least one identifier is required. Call add with no user_id, agent_id or run_id and Mem0 refuses, because a memory belonging to nobody can never be found again. Lesson 12 is about which to use.

Try it yourself
  • Add a sentence of pure politeness on its own and see whether anything is stored.
  • Call add without user_id and read the error.
  • Put a fact in the assistant's message rather than the user's and see whether it survives.

Slow is fine. Stopping is the only problem.