Only persist what cannot be re-derived from current work
What You’ll Learn
- The difference between session memory and persistent memory
- How to decide what’s worth remembering across sessions
- How to implement a file-based memory store
The Problem
After a session ends, everything the agent learned is gone. Next session starts from zero. The agent re-learns preferences, re-discovers conventions, re-reads the same files.
The Solution
A memory system that persists cross-session knowledge. But with one rule: only persist what cannot be re-derived from current work.
Memory types:
+------------------+ +------------------+ +------------------+
| Project memory | | User memory | | Session memory |
| (deps, patterns) | | (preferences) | | (current state) |
+------------------+ +------------------+ +------------------+
How It Works
-
A
MemoryStorebacked by JSON files on disk. -
The agent can
read_memory(key)andwrite_memory(key, value). -
Rules prevent garbage: memory only for preferences, conventions, decisions — not for things that can be re-read from the filesystem.
class MemoryStore:
def __init__(self, path: Path):
self.path = path
self.data = json.loads(path.read_text()) if path.exists() else {}
def get(self, key: str) -> str:
return self.data.get(key, "")
def set(self, key: str, value: str):
self.data[key] = value
self.path.write_text(json.dumps(self.data, indent=2))
What Changed From s08
| Component | Before (s08) | After (s09) |
|---|---|---|
| Cross-session | Nothing | Persistent memory store |
| Preferences | Re-learned | Remembered across runs |
| Decisions | Lost | Archived as memory |
Try It
cd learn-claude-code
python agents/s09_memory_system.py
Set a memory: this project uses pytest for testingSet a memory: I prefer snake_case over camelCase- Exit, restart, and
What do you remember about this project?
Key Takeaway
Memory gives direction; current observation gives truth. Only persist what cannot be re-derived from current work.