Blog

Making a coding agent learn from its mistakes

Terminal prompt icon, a bookmarked notebook and a looping arrow

You correct a coding agent on Monday and it makes the same mistake on Thursday. The correction lived in a chat, and that chat is gone. The usual answer is to give the agent a memory. I came to a different conclusion: memory solves remembering, not obeying. This post covers how I designed intern, an open source tool, why it keeps "lessons" and "rules" apart, and how it measures whether a lesson actually works.

It started with my own setup. When I fixed the same spacing bug in a prototype for the second time, I told the agent I did not want to say it again and wrote a small skill that recorded corrections. The skill wrote the rule to a file, but nobody counted whether the rule held. intern grew out of that gap. I built it in four stages on 28 November 2026 and published it to PyPI as intern-agent 0.1.0 the same day.

Remembering is not obeying

There is research that measures where memory layers stop helping. TellOnce built a diagnostic from real agent interactions and reports that agents using Mem0 still violate 57.5% of the preference checks that apply. The preference was in context, and the agent ignored it.

So I did not define intern's success as "the lesson was recalled". The measure is whether the lesson was broken while it applied. Each lesson carries two numbers: exposures, every time the lesson entered the agent's context, and violations, every time the same mistake was caught again. A lesson broken in the same session where it was shown is flagged separately, because that is the real compliance failure. The repeat rate is violations over exposures, and the target is zero.

The flow has five steps; intern runs the first four itself, and the approval in the last one is yours:

  1. Capture: your corrections ("no, use pnpm", "yine olmadı"), a tool call you rejected, a file you reverted, a command that failed three times with the same error, the agent's own friction, review findings.
  2. Distill: an LLM turns each signal into a lesson: context, what was done, what to do instead, why, and the scope where it applies (paths, tools, intents).
  3. Recall: only lessons that match the work at hand, right before the prompt or the tool call; at most 3 lessons and 600 tokens, each once per session.
  4. Measure: exposures and violations are counted, and you can pay for a blind replay.
  5. Promote: a lesson that keeps getting broken is proposed as a binding rule, which turns on only after you approve it in your own terminal.

I kept capture cheap on purpose: the hook looks for correction signals with plain pattern matching, makes no LLM call, and costs milliseconds. A pattern match only produces a candidate; the model in the end-of-session distill pass decides whether it was a real correction. The false positive rate is logged locally and intern stats shows it.

Lessons and rules are separate layers

A lesson is soft: it shows up as an example in relevant work, and the agent can still decide otherwise. A rule is hard: a check that runs before the tool call (hook-check) or a line in an instruction file. I split them for three reasons.

The first is cost: if every lesson became a rule, the instruction file would turn into a long list read in every session. An evaluation of AGENTS.md found that repository-level context files do not generally improve task success, while raising inference cost by more than 20% on average. In intern an instruction line is the last resort. A lesson arrives only when its scope matches, and within the budget.

The second is error margin: distillation is done by a model, and the model can be wrong. A bad lesson, at worst, is a useless example in context. A bad rule blocks a legitimate command. So intern writes and recalls soft lessons on its own, but promotion to a rule always reaches you as a proposal.

The third is trust: if the agent could approve its own rules, the whole thing would be pointless. approve-rule, approve and reject ask you to type the id on /dev/tty and refuse while an agent environment variable is set. Ids of unapproved lessons are also kept in integrity.json, so deleting the untrusted: true line from a lesson file grants nothing.

The promotion threshold is two violations, and one for lessons in the security, data loss, outbound and deploy categories. The example lesson in the docs comes from a mistake I made myself: trimming test output with tail and pushing with &&. The pipe returns tail's exit code, so a failing test suite still pushes. The lesson says to use set -o pipefail or read the exit code separately, and since it sits in the deploy category, a single repeat turns it into a rule proposal.

Outside content gets its own boundary, because a lesson born from a web page, an issue body or MCP output is a way to plant instructions in the agent's memory from outside. MINJA shows exactly that: injecting malicious records into an agent's memory through queries alone. In intern, a lesson that comes from a turn which read outside content is saved as untrusted, is never recalled until you run intern approve, and never counts toward a rule.

Time is part of the layering too: lesson weight decays with a 365-day half-life. A soft lesson shown for 90 days and never broken moves to the archive. It is not deleted; it just drops out of recall. A lesson that is no longer needed stops taking up context.

Measuring whether a lesson holds

Counters are cheap but indirect. A violation is counted when distill links a new event to an existing lesson. A call stopped by a rule is not a violation; it is logged as a separate "blocked" row. Every count lives in one append-only log (stats.jsonl), and the numbers in a lesson file are a snapshot rewritten from that log. intern report shows exposures, violations, compliance and pending rule proposals.

For a more direct answer there is intern eval. It builds cases from the real events the lesson was born from: the situation just before the mistake, with the same tool and files. Each case is asked twice, once with the lesson and once without, in random order. A judge model then gets the mistake and both answers as A and B. It does not know which one had the lesson; it only decides whether the mistake was repeated.

Three details are there to keep the result honest:

  • If an answer names the lesson's title or id, the judge could tell which arm had it. That case is recorded as "unclear" for both arms and the judge is not called.
  • The result is a repeat rate per arm with 95% intervals (Wilson per arm, Newcombe for the difference). A run with fewer than ten judged cases gets a "few cases" warning.
  • Every case costs three model calls. The run stops before any call that would push it over the budget, using the cost of the last call as the estimate for the next.

There is a limit, and the code states it itself: these are repair cases, the same task again. Whether the lesson carries over to a new task is not what this measures, and the result file says so. An honest note from my side as well: intern went onto my own machine two days ago. I have no field data to show for this post yet; the only measurement I can point to is a 273-test suite that passes in CI on Python 3.9 through 3.13.

Where the testing effort went was telling. Of the 14 commits before the 0.1.0 tag, 13 are fixes, and nearly all of them concern how the guard reads shell commands: $'...' quoting, nested command strings, heredoc readers, rm resolving to gnurm on Ubuntu 26.04. Because the guard reads commands as text, it can be dodged (int"ern", for instance). I wrote that down plainly in the security doc. The real boundary is the agent's own permission settings; intern doctor finds the missing deny rules and prints the JSON to add, without changing any setting itself.

The decisions behind it

Looking back, these are the decisions that set the direction:

  1. Agent-independent core: lessons are plain markdown files you can read, edit, git diff and delete. The first adapter is the Claude Code plugin, then Codex CLI and any agent that reads AGENTS.md. Codex hooks cannot answer "ask", so a call that would be asked about is denied there with the reason.
  2. Zero dependencies: Python 3.9 and the standard library only. 3.9 is what ships with macOS, so there is nothing extra to install and no supply chain surface.
  3. No embeddings: the recall score multiplies scope match, BM25 text similarity and time weight. A vector database would add a dependency, and scope matching already does most of the work.
  4. Local project lessons: a repository's .intern/ folder is not recalled until you run intern trust. A repo you clone cannot push lessons onto you.

All of these come from one principle: whatever the agent writes is a proposal, and whatever binds needs a person's approval. If you are correcting the same thing in your agent for the third time, start with pip install intern-agent and run intern doctor. My other tools are on the projects page, and I described a similar "evidence before publishing" gate for this blog in the blog writer skill post.

AIClaude CodeTesting