Related ToolsClaude CodeClaude

A Developer Built a Claude Code Agent That Learns from Its Own Mistakes

Claude by Anthropic
Image: Anthropic

What if your AI coding assistant remembered every mistake it made and wrote itself new rules to avoid repeating them?

That's exactly what developer Rory Teehan built. His framework turns Claude Code into what he calls a "stateful operational partner" - an agent that persists across sessions, maintains its own memory, and most notably, generates behavioral directives from its failures. After two weeks of operation, the agent had promoted 13 error patterns into active rules and accumulated 211 indexed memories.

How the Feedback Loop Works

The system runs on a straightforward cycle. Every time the agent makes an error, it gets logged with structured fields: what happened, why it happened, and what should have happened instead. A background process counts how many times each mistake pattern appears across sessions. When a pattern hits three or more occurrences, the system automatically generates a behavioral rule and writes it to the agent's persistent memory.

The key insight is specificity. Instead of logging vague failures like "didn't follow instructions," the system traces exactly which signal the agent misinterpreted. This means the auto-generated rules target precise behaviors rather than generic reminders.

These learned rules also carry more weight than the agent's initial static instructions. The logic: if something needed to be learned through repeated failure, it's probably more important than a rule someone wrote before the agent encountered real work.

The Technical Stack Is Surprisingly Simple

The whole thing runs on Claude Code's CLI (the $100/month Max plan), Supabase for PostgreSQL storage with pgvector (a vector database extension for finding similar memories), and Ollama for generating embeddings locally at zero API cost. Total infrastructure runs about $300/month with no Docker, Kubernetes, or custom servers.

A few patterns stand out for anyone building persistent agents:

  • Session resumption cuts token usage by roughly 80%. The agent does a full boot once daily, then uses --resume mode for subsequent sessions.
  • Atomic task claiming via Postgres prevents duplicate work when multiple agent sessions run in parallel.
  • Circuit breakers shut down autonomous jobs after three consecutive failures and fire an alert.
  • Hybrid memory retrieval combines importance-based and similarity-based recall, so the agent pulls up memories that are both relevant and significant.

The agent's identity lives in three persistent files: SOUL.md (who it is), USER.md (who you are), and HARNESS.md (what it can do). The learned behavioral directives layer on top of these at boot time.

A Pattern Worth Watching, Not a Product

Teehan is clear this is an architecture reference, not a turnkey product. The repository includes schemas, templates, and 1,200 lines of architecture documentation, but things like messaging integrations and daemon implementations are documented as patterns rather than shipped as production code.

The more interesting takeaway is the design pattern itself. Most AI agent frameworks treat each session as a blank slate, or at best, stuff a static system prompt with every possible instruction upfront. The idea of letting an agent earn its own rules through observed failure is a more natural approach to building reliable AI workflows - and it's something anyone using Claude Code, Cursor, or similar tools could adapt without waiting for the model providers to build it in.