How to Add Persistent Memory to a Chatbot with Mem0

November 14, 2026

Mem0 is a memory layer for LLM apps. It takes conversation turns, extracts the facts worth remembering, stores them in a vector-backed store, and returns the relevant ones when a new query arrives. Your chatbot stops asking for the same context twice.

The plumbing is about 20 lines. The part that decides whether memory helps or hurts is what you let it remember.

What does Mem0 actually store?

Extracted facts, not transcripts. Send it “I’m vegetarian and allergic to shellfish”, and it stores a preference like diet: vegetarian rather than the sentence.

That matters because stored facts are cheap to retrieve and easy to correct. A stored transcript is neither.

Each memory record has an id, a text field, a user or agent identifier, and metadata. You can update and delete records individually, which is what makes GDPR-style deletion feasible.

How do you set it up in Python?

Install and initialise with a vector store and an LLM for extraction:

from mem0 import Memory
m = Memory()

The defaults use OpenAI for extraction and a local vector store. For production, configure Qdrant or pgvector plus your own model keys in the config dictionary.

Two calls cover 90 percent of usage:

m.add("I work night shifts and prefer short answers", user_id="u-42")
results = m.search("formatting preferences", user_id="u-42")

add runs an extraction step, so it costs an LLM call. Do not call it on every message — call it on turn boundaries or at the end of a session.

How do you get results into the prompt?

Search first, then inject. Retrieve the top 3 to 5 memories and place them in the system prompt as a short list.

A pattern that works:

  1. Take the user’s new message.
  2. Search memories with user_id scoped to that user.
  3. If results exceed a score threshold, add a section to the system prompt: “Known about this user: …”
  4. Generate the reply.
  5. After the reply, add the turn to memory if it contained new facts.

Scoping by user_id is non-negotiable. Search without it and one user’s preferences leak into another user’s session. That is the most common and most embarrassing bug with memory layers.

What should not go into memory?

Three categories to filter actively:

  • Sensitive data. Card numbers, addresses, health details. Strip before add, or use a redaction step.
  • Transient state. “My order is stuck” is not a preference. It pollutes retrieval for months.
  • Inferred facts. “User seems frustrated” is the model’s guess, and it hardens into a false belief.

Mem0’s extraction is good but not a compliance tool. Add a pre-filter for anything regulated.

How much does it cost?

Two costs: the extraction LLM call per add, and the vector store. On small models, extraction runs to a fraction of a cent per turn. At 100,000 turns monthly, expect a real bill, plus storage.

Open-source Mem0 is free to run yourself. The hosted platform charges per request and adds an audit UI, which is worth it if you need memory inspection without building it.

Where does memory hurt?

Personalisation in low-stakes tools. A calculator app does not need to remember you have a dog.

Multi-user devices. If two people share a session and you scope by device rather than user, memories cross-contaminate badly.

Long-running assistants where preferences change. Without an update pass, a memory from January still shapes answers in December. Schedule periodic consolidation or give users a visible “what I remember” page.

Quick Summary

  • Mem0 extracts facts from conversation turns and stores them for retrieval; add and search cover most usage.
  • Always scope both calls by user_id or one user’s memories leak into another’s session.
  • Retrieve 3 to 5 memories and inject them into the system prompt; do not dump 50.
  • Filter sensitive data and transient state before storing; extraction is not a compliance layer.
  • Open-source is free to self-host; hosted pricing scales per request, and memory needs periodic consolidation.
Tags: