Introduction
Memory turns agents from "goldfish" (forgetting after each conversation) into true assistants — remembering preferences, learning from mistakes, and accumulating knowledge over time.
1. Types of Memory
1.1 Short-term Memory (Working Memory)
- Conversation history in the current session
- Limited by context window
1.2 Long-term Memory
- Persistent across sessions
- Save in vector DB or database
- For example: user preferences, important facts
1.3 Episodic Memory
- Remember "episodes" — the times the agent completes the task
- Help agents learn from experience
- "Last time a user asked the same thing, I did this and it worked"
2. Implementation
class AgentMemory:
def __init__(self):
self.short_term = []
self.long_term = chromadb.Collection("long_term")
self.episodes = chromadb.Collection("episodes")
def remember(self, content, memory_type="long_term"):
if memory_type == "long_term":
self.long_term.add(documents=[content], ...)
def recall(self, query, n=5):
return self.long_term.query(query_texts=[query], n_results=n)
def save_episode(self, task, steps, outcome):
episode = f"Task: {task}\nSteps: {steps}\nOutcome: {outcome}"
self.episodes.add(documents=[episode], ...)
Summary
- 3 types of memory: short-term (conversation), long-term (facts), episodic (experiences)
- Vector DB is the backbone for long-term and episodic memory
- Summary memory helps fit long conversations into the context window
- Episodic memory helps agents improve over time
Exercises
- Implement the full AgentMemory class
- Build an agent that "remembers" user preferences across sessions
- Implement episodic memory and will the test agent improve?
- Comparison: buffer memory vs summary memory for long conversations