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

  1. A MemoryStore backed by JSON files on disk.

  2. The agent can read_memory(key) and write_memory(key, value).

  3. 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

ComponentBefore (s08)After (s09)
Cross-sessionNothingPersistent memory store
PreferencesRe-learnedRemembered across runs
DecisionsLostArchived as memory

Try It

cd learn-claude-code
python agents/s09_memory_system.py
  1. Set a memory: this project uses pytest for testing
  2. Set a memory: I prefer snake_case over camelCase
  3. 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.