How Do You Turn Production Bugs into Permanent AI Agent Rules?
People forget lessons. Teams forget them faster. An AI agent’s rulebook doesn’t forget, as long as you write the lesson down.
In short: Many of the rules our AI agents follow exist because something went wrong once. A payment invoice that was created but never paid became a question the agent now always asks. A mobile bug caused by assuming an API returned a list became a rule: “a plural noun does not mean an array.” We treat every incident, review comment and near miss as a candidate rule, and add it to the shared agent instructions so the same mistake can’t happen twice, for anyone on the team.
The problem with lessons learned
Every engineering team has a post-mortem document somewhere that ends with “lessons learned”. Most of those lessons are forgotten within a few months.
The person who learned the lesson moves to another project. A new developer joins who wasn’t there for the incident. The fix lived in one pull request, and nobody remembers why that odd-looking line of code matters.
AI agents make this worse in one way and better in another. Worse, because an agent has no memory of last quarter’s outage; it will happily reintroduce the same bug. Better, because an agent reads its instructions every single time. If the lesson is in the instructions, it is never forgotten.
So we changed how we treat lessons. A lesson isn’t finished when it’s written in a post-mortem. It’s finished when it’s a rule an agent follows.
The loop
The important design choice is in step D. The agents’ instructions live in a shared, version-controlled repository. Updating one file updates the behaviour for the whole team on their next git pull. There’s no “my agent knows this but yours doesn’t”.
Five scars, and the rules they left
1. The invoice that was never paid
When a flow creates a payment invoice, there’s a second step that’s easy to forget: actually paying it, or confirming how it gets paid. Depending on the setup, payment might be triggered explicitly, collected automatically, or happen implicitly for zero-amount invoices.
The rule now: whenever the context generator documents a flow that creates an invoice, it must ask, “How is this invoice paid or marked as paid?”, and it refuses to document invoice creation without documenting the payment step as well.
The question gets asked every time, so the payment step can’t be left out of a specification by accident.
2. A plural noun does not mean an array
Some of the hardest bugs to catch in our mobile app came from wrong assumptions about the shape of API responses. A field called packages turned out to be a single object. A list endpoint turned out not to be paginated. Code written on those assumptions looked right and failed at runtime.
The rules now (from the mobile coding agent’s instructions, which say explicitly that every rule below is derived from a real bug):
- Never assume a response is paginated, a plain array or a single object. Read the API schema.
- A plural field name does not mean an array. Confirm it.
- Mark optional fields as optional.
// Wrong: assumed an array because the name is plural
const pkg = response.data.packages?.[0];
// Right: verified in the API schema that `package` is a single object
const pkg = response.data.package ?? null;
3. The variable that vanished
Our agents run shell commands to work across repositories. Early on, an agent would load environment variables in one command and expect them in the next. But each command runs in a fresh shell, so the variables were gone, and paths could silently resolve to the wrong place. A branch check, for example, could report the branch of the wrong repository.
The rule now: the agent’s instructions explain, in bold, that shell state does not persist between commands. Every command that touches a repository must reload its environment first, quote its paths so an empty value fails loudly, and never rely on a variable set earlier.
It’s an unglamorous rule. It also prevents an agent from making changes in the wrong place.
4. The “improvement” we had already rejected
Sometimes an agent, or a new developer, proposes a change that looks like an improvement but is actually a pattern the team tried and abandoned. Without a record of why, the same debate happens again, or worse, the change gets made.
The rule now: module overview documents include a Design Decisions table with an Alternatives Rejected column:
| Decision | Why | Alternatives rejected |
|---|---|---|
| Money stored as integer cents | Avoids rounding errors in billing | Decimal columns: inconsistent rounding across services |
| Handler owns the database commit | One transaction boundary per request | Committing in services: partial writes on failure |
(Illustrative rows.) As our own guide puts it, this column exists so an agent doesn’t “improve” something back to a pattern you already evaluated and discarded.
5. The email nobody fully specified
Notification emails seem simple until they’re wrong: the wrong branding, a missing link, an unclear subject line, a message sent to the wrong role.
The rule now: for any flow that sends an email, the agents ask about the subject, the preview text, the salutation, the recipient’s role, the body, any buttons and links, any attachments, and whether to use the customer’s branding or the default. Templates must extend a shared base layout and use shared components, never one-off HTML.
Where scars come from
Not every rule needs an outage. Our sources, roughly in order of how often they produce rules:
- Code review comments. If a reviewer has to say something twice, it becomes a rule.
- QA findings. A bug found in testing usually means a rule or question was missing from the spec.
- Production incidents. Rarer, but each one produces the most valuable rules.
- Agent failures. When an agent does something odd, we ask what instruction would have prevented it.
How to write a good scar
A rule that captures a lesson well has four properties:
- It’s specific. “Be careful with payments” is useless. “Always document how an invoice is paid” is a rule.
- It’s actionable. The agent can follow it, or ask it as a question, without interpretation.
- It has an example where it helps. A bad/good pair beats a paragraph.
- It says why. A one-line reason stops someone from deleting the rule later because it looks unnecessary.
The risk: rulebook bloat
Rules accumulate. A rulebook that only grows eventually becomes noise, and long instruction files cost time and attention on every task.
We manage this in three ways: rules are one line wherever possible, detail goes into examples rather than prose, and rules that no longer apply (because the code they protected was removed) are deleted in the same pull request.
The forward deployed angle
Forward deployed engineers see more failure modes than most, because they work across many clients and systems. Turning each failure into a rule means a lesson learned on one engagement protects the next one, instead of staying with the engineer who happened to be there. We cover this in more depth in our article on forward deployed engineering in the age of AI agents.
Key takeaways
- A lesson isn’t learned until it’s a rule your agents follow.
- Keep agent instructions in a shared, version-controlled repository so one fix updates everyone.
- Turn the riskiest lessons into questions the agent must ask, not just rules it must follow.
- Record rejected alternatives so nobody “improves” the code back into a known problem.
- Prune rules as aggressively as you add them.
Related reading: the questions our context generator asks and our AI coding agent rulebook.
Frequently asked questions
How do you turn a bug into an AI agent rule?
Find the root cause, meaning the fact nobody knew or the check nobody made, and write it as a short, specific rule, a question the agent must ask, or a bad-versus-good example. Add it to the shared agent instructions through a reviewed pull request.
What is an “Alternatives Rejected” table?
A section in a module’s design documentation listing approaches the team considered and rejected, with the reason. It stops AI agents and new developers from reintroducing patterns that were already tried and abandoned.
Won’t the rulebook become too long?
It will if you only add rules. Keep rules to one line, use examples for detail, and delete rules when the code they protect is gone.
Why are some rules written as questions?
Because some mistakes come from missing information, not wrong code. A mandatory question, such as “how is this invoice paid?”, forces the missing information into the specification before any code is written.
