I Gave Claude Code a Memory of Its Mistakes. A Test Worked Better.
In short
- My coding agents wrote 5,196 one-sentence lessons into their commits. Almost none reached the agent that needed one.
- I tried three ways of showing an agent the right lesson at the right time. The best one was useful about a quarter of the time, and I turned it off.
- What worked was smaller: when an agent writes a rule it has written before, it has to write a test for it.
A coding agent starts every session knowing nothing about the last one. I run Claude Code, Anthropic’s coding agent, across several projects, usually many sessions at once. OpenAI’s Codex worked in the same repositories in October and November and wrote 257 of the lessons, and a Gemini agent joined on November 30. A bug one session fixes on Monday, another session writes again on Thursday.
So I tried to give them a memory. Every time an agent fixes something, it writes down the rule it learned. Later, an agent about to make the same mistake gets shown that rule first. If that worked, each bug would only have to be fixed once.
It mostly didn’t work. This is what I built, what I measured, and the one piece I kept.
What a lesson looks like
A lesson is one sentence at the end of a commit message, written so it would make sense in another codebase. Here are three.
A seeded sample is only reproducible if the index space it draws from is canonically ordered.
A scheduled post is live and unindexed at the same time: the moment the date passes it serves to readers, but the sitemap and feed are build artifacts.
A seed for hidden information must come from a CSPRNG; a timestamp is a guessable seed even when the PRNG it feeds is fine.
One sentence can’t hold everything a fix taught, and it isn’t meant to. The sentence is the part that gets searched. Behind it the record keeps the rest:
- The commit message, which says what broke and how the fix was checked.
- The diff, so the next agent can see the actual change.
- The conversation that led to the fix, when there was one.
An hourly job collects them. By late September there were 5,196, about 35 new ones a day. I read 30 at random and 28 were good, reusable rules.
Writing them was working. Reading them was not.
The same rule, written again and again
This blog embeds a few live demos in iframes. The site’s security policy lists which origins a page may embed, and the browser silently drops any that aren’t listed. Locally there is no policy, so the page looks fine until it’s deployed.
Agents learned that five times in six weeks:
- June 5: “A CSP frame-src allowlist silently blocks any iframe origin not on it.”
- June 9: “External demo embeds need both page markup and matching CSP allowances on the host site.”
- June 23: “A cross-origin iframe needs both the child’s frame-ancestors to allow the parent and the parent’s CSP frame-src to allow the child.”
- June 23, a different session: “An embedded cross-origin iframe needs its exact origin in the host page’s CSP frame-src, or the embed silently fails.”
- July 14: “When adding an embedded app to a CSP-protected site, update frame-src and verify production headers.”
Each session fixed its own page and wrote the rule down. None of them saw the earlier ones.
It wasn’t one unlucky rule. A data pipeline learned twice in a week that a seeded random sample changes between runs unless its input is sorted first. On my game site, six sessions between June and August each learned that a CSS display rule overrides the HTML hidden attribute, and each patched one component.
That was the failure I wanted the memory to prevent.
Three ways to read a lesson, all broken
On September 29 I checked every path a lesson had back to an agent.
- Show it automatically. A script ran on every message I typed, searched the lessons for matching words, and put the top few in front of the agent. I graded 50 of its results by hand. 18% were useful.
- Let the agent search. Agents had a search tool. About 7% of sessions used it. It also only matched exact phrases, so a search for “supervised distillation teacher strength” returned nothing out of 5,196.
- Put the best ones in a file. A weekly job wrote each project’s top lessons into a file every session loads. It picked “top” by how often the first script had shown a lesson, which mostly meant how often it had misfired.
Better search didn’t fix it
Word matching was the obvious thing to replace. I switched to search by meaning, using a small model that runs on my laptop.
It works when the search describes a problem. “Why is my hidden panel still showing” returns the three lessons about the hidden attribute.
It doesn’t work on what I actually type to an agent. Two real messages:
- “1. yes 2. skip”
- “restart”
There is no problem in those to match. On 40 of my real messages, 4 of the 80 lessons it returned were useful. The search was fine. My messages don’t describe the problem.
Searching with the agent’s own words
The agent does describe the problem. Before its first edit it has usually written a paragraph about what it is about to do. I used that paragraph as the search.
I tested this on two weeks of past sessions first. Of the 10 strongest matches, 6 were useful. That was enough to ship it, with a rule set in advance: if fewer than half of what it shows is useful, it comes off.
It ran for two weeks and showed a lesson 77 times. About 17 were useful.
One that helped. An agent was fixing the badge that marks the last move of a finished game. The lesson it was shown: “when the thing an end-of-game marker points at is destroyed by the final move, anchor it to the previous state.”
One that didn’t. An agent was about to stop a game bot from repeating moves into a draw. It was shown a lesson about checking an engine’s score before quoting it in a blog post. Both mention engines. They have nothing else in common.
I turned it off on October 14. Two things went wrong. Ten matches was too few to trust the test. And the paragraph an agent writes before editing is often a status report to me, which says little about the code it is about to touch.
What worked: a rule written twice becomes a test
The piece I kept reminds nobody of anything.
When an agent commits a lesson, a script compares it with every earlier lesson in that project. If it is close to one already there, the agent is told the rule has been learned before and to write a test that enforces it. Comparing one rule against another is reliable in a way that comparing a chat message against a rule never was.
- The iframe rule became a build check. This blog’s build now fails, naming the page, if a post embeds an iframe from an origin the policy doesn’t list.
- The
hiddenrule became one site-wide CSS rule with a test that keeps it there. It replaced five per-component patches and fixed a live bug on the way.
A test doesn’t need the next agent to remember anything. It fails. It fails the same way whether Claude Code, Codex or Gemini wrote the change.
The whole thing is three steps, and none of them needs my setup:
- Have the agent end each bug-fix commit with one sentence stating the rule.
- On each new sentence, compare it with the earlier ones from the same project.
- On a close match, make the agent write a test for the rule before it moves on.
Where it stands in December
By November 9 there were 7,158 lessons, and agents had searched them in under 2% of that month’s sessions.
So I cut the writing to match the reading. An agent now writes a lesson only when it fixes a bug or hits a surprise. New lessons went from about 400 a week to about 60.
The repeat check still runs, and now files the task to write the test on its own. 56 rules now name a check that enforces them.
What I’d do differently
- Start with the repeat check. I collected 5,000 lessons before asking who would read them. The check only needs the rule text, and it would have caught the iframe rule on its second appearance in June.
- Write fewer lessons from the start. One per bug fix was enough. One per commit gave me a pile nobody searched.
- Test with more than ten examples. 6 of 10 looked like 60%. Live, it was about 25%.
- Set the off switch before shipping. This one I did, and it’s why the recall script is gone and not still running at a quarter useful.
- Search from the file, not the conversation. The agent’s reply was a weak query. The file it is about to edit might be a better one. I haven’t tested that.
A related failure, where an agent sat waiting on a job for 30 hours, is in The Agent That Watched a Log File for 30 Hours.