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

Putting memory in the reply loop

Every call so far has been made by hand. An assistant with memory does two things around every reply, and the order of them is the whole design.

What an assistant does per message
Search before replying. Ask the store what is already known about this person and this question.Step 1 of 3

Search first, add last. Add first and the assistant retrieves the sentence the customer typed one line ago and solemnly reads it back to them.

The assistant, unchanged

This is the lookup from lesson 1, with one more entry. It knows nothing about memory and it is not going to.

python
from pretend_mem0 import memory

HANDBOOK = {
    "late": "Sorry about that. I can chase order A17 for you.",
    "update": "I can send you updates about your order.",
    "refund": "Refunds take five working days.",
}

def answer(said):
    for word, reply in HANDBOOK.items():
        if word in said.lower():
            return reply
    return "I can help with orders and refunds."

The three lines that wrap it

python
def assistant(shop, user, said):
    known = shop.search(said, filters={"user_id": user})["results"]
    reply = answer(said)
    if known:
        reply += " I have on file: " + known[0]["memory"] + "."
    shop.add(said, user_id=user, metadata={"source": "chat"})
    return reply

Search, then reply, then add. The reply is still the same dictionary lookup; what changed is what the assistant knows while making it. Save both halves as shop.py.

Example
from shop import assistant, memory

shop = memory()
print(assistant(shop, "ravi", "I prefer email updates, not SMS."))
print(assistant(shop, "ravi", "Can you send me an update?"))

This is the conversation from lesson 1, and the second answer is different now. The assistant looked up what it knew about updates, found what Ravi said a moment ago, and used it.

Why the memory goes in the reply and not around it

Here the memory is appended to the sentence, which is crude on purpose: it makes what happened visible. With a real model the memories go into the prompt as context and the model writes one natural reply. The retrieval is identical; only what you do with the result changes.

Search with the customer's message, not a summary of it. The message is the best description you have of what they want right now, and an embedder is built to match text against text. Rewriting it into a query first usually loses the words that would have matched.
Try it yourself
  • Swap the two calls so add runs before search, and read the second reply.
  • Add a third turn and see which memory comes back.
  • Store with a run_id as well, and check that a new session starts clean.

Little by little, you're building something great.