Skip to content

Chapter 8 · Memory & State

Agents are amnesiac. Every session starts blank: the bug you debugged for an hour last Tuesday, the quirk of the payment sandbox, the fact that the user hates semicolons — all gone. Skills themselves are static: they ship knowledge you wrote before the session.

A memory pattern closes that loop: the skill instructs the agent to record durable lessons and recall them before acting — using plain files as the storage engine. Files, not databases, because files are:

  • versioned — lessons go through pull requests like code,
  • shared — a teammate's agent reads the same lessons,
  • portable — any agent, any tool, nothing to install,
  • auditable — git log .lessons/ is the history of what your agents learned.

The three scopes of memory

Scope Location Shared via Store things like…
Project memory <repo>/.lessons/ git (committed) Build quirks, sandbox behavior, team decisions, "the flaky test and why"
User memory ~/.lessons/ Nothing (personal) Your preferences, your recurring contexts, machine specifics
Scratch state tmp/, not committed Nobody Intermediate artifacts of a single run — not memory

Keep the three separate. Mixing user preferences into project memory is how .lessons/ files turn into 400 lines nobody reads.

The write/read protocol

Memory fails when it's an afterthought ("write down anything interesting"). The lessons-keeper example skill encodes it as a protocol with explicit triggers:

## Recall (before starting non-trivial work)

Run: python scripts/lessons.py list --project [--tag <topic>]
Read every returned lesson and respect it. Lessons cite their evidence —
if the evidence no longer exists in the code, the lesson may be stale:
verify before obeying, and prune it if outdated.

## Record (after finishing work)

Record a lesson when you learned something durable that a future session
would otherwise rediscover the hard way: surprising behavior, a user
correction, a workaround, a decision with its rationale.

Run: python scripts/lessons.py add --project --tag <topic> "<lesson>"

## Rules

- One idea per lesson, one line, dated automatically.
- Cite evidence: a file path, command, or error message. No evidence, no lesson.
- Never record secrets, tokens, personal data, or credentials.
- Never record transient context (what you're doing right now). That's
  conversation, not memory.
- Prefer pruning over accumulating: run prune --keep 100 when the file
  exceeds its cap.

What makes a lesson durable

Record it Don't record it
"The staging API requires X-Org-Id even for GETs (evidence: api/auth.py)" "Added a function to auth.py today"
"User prefers merge commits, never squash, on this repo (evidence: git log)" "User seemed in a hurry"
"Docker build fails behind the VPN; use --build-arg HTTPS_PROXY=…" "Tests are red right now"
"We chose Postgres over Mongo because of multi-region reads (decision + date)" The whole conversation summary

The test: would a competent teammate six months from now be glad this was in the onboarding doc? Memory is onboarding for the next session.

Hygiene is the whole game

Memory systems rot in one specific way: append-only growth. Three rules keep .lessons/ alive:

  1. Cap and prune. A hard entry cap (e.g. 100) with a prune command that keeps the newest. Pruning is the agent's job by protocol, not a chore someone remembers.
  2. Every lesson carries evidence. When the evidence disappears from the codebase, the lesson is suspect. The recall protocol says: verify, then obey or prune.
  3. Memory is advisory. Lessons are strong suggestions, not commands — code, tests, and CI win any disagreement. This one line in the SKILL.md protects you from a stale lesson hijacking a workflow (and from anyone editing lessons into prompt injection; the file goes through code review like everything else).

The storage format

Plain markdown, one line per lesson, written and parsed by a script so the format is enforced mechanically:

# Lessons — project memory (committed to git)

- [2026-08-30] [testing] The payment sandbox mocks 3DS only on Fridays deploys; treat e2e failures on other days as real bugs (evidence: .github/workflows/e2e.yml)
- [2026-08-21] [build] Docker builds need HTTPS_PROXY build-arg behind the VPN (evidence: Dockerfile)

The script (lessons.py) provides add / list / prune / count subcommands — following every script rule from chapter 6: stdlib-only, argparse with helpful --help, exit-code protocol, idempotent add (exact duplicates are refused with exit 0), project vs user scope as a flag.

Beyond files: agent-native memory

Files aren't the only memory substrate. Most agents now ship native memory (user-level memory in Claude, persistent memory directories in coding agents like ZCode, MCP memory servers). How to choose:

File memory (this chapter) Agent-native memory
Scope Project or user, your choice Usually user-level, agent-specific
Shared with team ✅ via git ❌ per-person
Works across agents ✅ any skills-compatible agent ❌ locked to one
Reviewable in PRs ✅ ❌
Structured protocol You define it Agent defines it

The pragmatic split: project lessons in files (team knowledge, versioned, agent-agnostic), personal preference in the agent's native memory (no reason to version your taste in every repo). A skill can also wrap native memory — e.g., "read the agent's memory directory first" — when you control the environment.


Chapter 9 · Composition & Advanced — skills with hooks, subagents, and MCP tools.