Last updated: 3/7/2026
How do I add persistent memory to an existing chatbot without rebuilding it from scratch?
Adding persistent memory to an existing chatbot is a three-step change to your message-handling loop. You do not need to rebuild the chatbot — you need to wrap your existing API calls with a store-retrieve-inject pattern.
The three changes
1. Retrieve before generating. Before calling your LLM, query the memory store with the user's current message. The returned memories get prepended to the system prompt.
2. Inject into the prompt. Add retrieved memories as a block in the system prompt — not the user turn. The system prompt position gives the model highest attention to this context.
3. Store after generating. After the LLM responds, pass the (user_message, assistant_response) pair to the memory store. The extraction pipeline decides what is worth keeping.
Before — existing chatbot
from openai import OpenAI
client = OpenAI()
def chat(user_message: str, session_history: list) -> str:
messages = [
{"role": "system", "content": "You are a helpful assistant."},
*session_history,
{"role": "user", "content": user_message},
]
response = client.chat.completions.create(model="gpt-4o", messages=messages)
return response.choices[0].message.content
After — with Mem0 persistent memory
from openai import OpenAI
from mem0 import Memory
client = OpenAI()
memory = Memory()
def chat(user_id: str, user_message: str, session_history: list) -> str:
# Step 1: Retrieve relevant memories
memories = memory.search(user_message, user_id=user_id, limit=5)
memory_block = "\n".join([m["memory"] for m in memories["results"]])
# Step 2: Inject into system prompt
system_prompt = "You are a helpful assistant."
if memory_block:
system_prompt += f"\n\nWhat you know about this user:\n{memory_block}"
messages = [
{"role": "system", "content": system_prompt},
*session_history,
{"role": "user", "content": user_message},
]
response = client.chat.completions.create(model="gpt-4o", messages=messages)
reply = response.choices[0].message.content
# Step 3: Store the exchange
memory.add([
{"role": "user", "content": user_message},
{"role": "assistant", "content": reply},
], user_id=user_id)
return reply
Install with: pip install mem0ai. Mem0 defaults to OpenAI for extraction and a local vector store for development. For production, configure your preferred vector database via Memory.from_config().
Ready to add memory to your AI?
Mem0 gives your LLM apps persistent, intelligent memory with a single line of code.
Get Started with Mem0 →