The idea
To remember across sessions, an agent must write facts somewhere durable and read them back later. Common stores: a Markdown file per user or project, a database table of facts, or a vector store searched by similarity.
Give the agent explicit tools: save_memory(fact) and search_memory(query). Decide what is worth saving (preferences, decisions, results) and what is not (passing chatter). Memories need updates and deletion too, or they go stale and contradict each other.
Memory has privacy implications: be clear with users about what is stored, keep it per user, and let them view and delete it.
Memory turns a one-off assistant into one that improves with use. It is also a source of privacy and correctness bugs if done carelessly.
Words to know
- Episodic memoryRecords of past interactions or runs.
- Semantic memoryDistilled facts, like "user prefers metric units".
Try it without code
Design a memory file
- Write the Markdown file an assistant should keep about you for coding help.
- Mark which lines would expire and when.
Use it in code
File-backed memory tools for one user.
from pathlib import Path
MEM = Path("memory/user_42.md")
MEM.parent.mkdir(exist_ok=True)
def save_memory(fact: str):
with MEM.open("a") as f:
f.write(f"- {fact.strip()}\n")
return "saved"
def read_memory():
return MEM.read_text() if MEM.exists() else "(no memories yet)"
# Put read_memory() in the system prompt at session start, and expose save_memory as a tool:
memory_tool = {"name": "save_memory",
"description": "Save a durable fact about the user or project for future sessions. Not for small talk.",
"input_schema": {"type": "object", "properties": {"fact": {"type": "string"}}, "required": ["fact"]}}Add memory to your research agent so it remembers which sources it already read for a topic across runs.
Common mistakes
- Saving everything, so memory becomes noise.
- No way to correct or delete a wrong memory.
- Sharing memory across users.