Published: 2026-05-02
URL: https://theagenticreview.substack.com/p/how-to-build-a-self-improving-agent
Most “agents” people ship today are really just prompts with a fancy harness. They run, they produce something, and the next time you fire them up they’ve forgotten everything. There’s no compounding. There’s no learning. There’s a fresh context window and a vague hope that this run will be better than the last. If you actually want a system that gets sharper with use, you need two things: a memory architecture that separates concerns, and a feedback loop that converts experience into procedure.
Most teams build the first half and skip the second. That’s why their agents plateau. Here’s the architecture I’ve landed on after building a handful of these in production. It works in Claude Code; it works in Codex with one filename change. Four memory layers, each with a different write cadence Memory is not one thing. Conflating it is the root cause of bloated context, slow agents, and instructions that quietly contradict each other. Static rules live in CLAUDE.md (AGENTS.md on Codex).
This file loads automatically every session. It’s where you put your tech stack, your project conventions, the immovable always-do-X / never-do-Y rules. Hand-edited. Short. Dense. If it grows past a couple hundred lines, you’re using it wrong. Procedural memory lives in skills — .claude/skills/<name>/SKILL.md.
Each skill is a self-contained workflow: instructions, optional scripts, reference docs. The critical property is that skills load on demand, not on every turn. Claude reads SKILL.md when the task needs it and ignores the rest.
This means you can have fifty skills without paying a token cost for the forty-eight you didn’t use this run. This is where crystallized “how we do X here” lives your runbook for a flaky deploy, your pattern for shipping a PR, your domain ontology. Episodic memory lives in learnings.md and task-history.md. Append-only during runs, consolidated periodically.
This layer is what makes the system honest with itself: a record of what actually happened, what failed, what the fix turned out to be. Without it, the agent keeps making the same mistakes. Semantic memory lives in an external vector store — Mem0, Letta, Qdrant, whatever fronted by an MCP server.
The agent searches it through a tool call instead of you trying to stuff every relevant fact into CLAUDE.md. This is how you keep static rules small while still giving the agent access to a deep history.
A closer look at semantic memory The vector store is the layer most teams build worst. They embed everything, retrieve top-k, dump it into context, and wonder why the agent gets noisier over time. A few rules that have held up: Facts go here, lessons don’t. Code, docs, design notes, past PRs, runbooks anything you’d fuzzy-search that’s vector store material. Lessons (“when X fails, try Y”) belong in learnings.md and graduate to skills.
The two layers compete if you mix them: a lesson retrieved by similarity has no priority signal, so the agent treats it as one fact among many and quietly ignores it. The agent doesn’t write to it. An ingestion pipeline does. Run a separate job that indexes code, docs, and finalized task outputs on a schedule. If you let the agent freely upsert during runs, the store fills with half-baked intermediate states and self-reinforcing loops within a week.
The agent can propose additions at wrap-up; a separate review step ingests them. Retrieve once, early. Search at the start of the task, inject results as a context block, then stop. Iterative semantic search inside an agent loop is almost always a mistake it’s slow, it spirals, and it usually means your initial query was wrong. Fix the query, not the loop. Hybrid search beats pure vector. BM25 + dense + a reranker.
The reranker matters more than the embedding model for most use cases. Filter by metadata (project, date, doc type) before the vector hop and you’ll halve the noise. This is also where Azure AI Search or Elasticsearch start earning their cost over a bare vector DB pre-filtering at scale is the actual product.
The self-improvement loop is the part that matters Here’s where most setups fall apart. People build the layers and stop. They get persistence, not improvement. To actually compound, you need four things wired together: A wrap-up step in every skill.
At the end of a task, the agent writes a structured entry to learnings.md what it tried, what failed, what the fix was, one line of lesson. Non-negotiable. If the wrap-up doesn’t happen, nothing about this run survives.
An eval harness on cron. GitHub Actions, a scheduled Cloud Run job, whatever you have. It re-runs a benchmark task suite, scores outputs against expected results, diffs against the last run. When something regresses, the diagnostic gets written back into learnings.md as actionable instructions — not “the agent failed” but “when the input contains X, try Y before Z.” The specificity is what turns a log into a lesson.
A weekly consolidation pass. Another skill whose only job is to compress old learnings into a summary, archive the raw entries, and keep the active file under three hundred lines. Skip this and your memory file slowly poisons every session with stale context. Skill promotion.
When the same lesson appears in learnings three or more times, promote it: write it up as a proper SKILL.md, delete the now-redundant log entries. This is the system literally extending its own procedural memory based on observed patterns. It’s the closest thing to actual learning your agent will do. Three traps to avoid Don’t dump everything into CLAUDE.md.
It loads every turn and burns tokens. The discipline is: rules go in CLAUDE.md, procedures go in Skills, history goes in memory files, facts go in the vector store. Each layer has a job. Don’t mix them. Don’t use stateful subagents for anything reused across sessions. Make every subagent stateless and reconstruct context from memory files at the start of each call. Anything else leaks state in subtle ways that take a week to debug usually right after a demo. Don’t ship without an eval harness.
A wrap-up step without scoring produces a diary, not learning. You need a signal that says “this run was worse than the last” otherwise the system has no way to notice regressions, and your learnings.md slowly fills with confident-sounding nonsense.
The quiet point This architecture compounds because it forces a separation between things that change at different speeds. Rules change rarely. Procedures change weekly. History accumulates daily. Facts get retrieved on demand.
When you collapse those into one file or one mechanism, every change risks corrupting everything else, and the system stops being trustworthy. Build the layers. Wire the loop. Run the evals. The agent improves on its own. my caption