Skip to content

How to stop AI coding agents repeating mistakes

To reduce repeated mistakes, record the approach, why it failed or was rejected, and what would justify trying it again. Then instruct the coding agent to retrieve that record before editing the affected code. Selvedge provides a local MCP server and CLI for this workflow; its prior_attempts tool retrieves recorded attempts and outcomes by entity.

How do I write a rule that stops an agent repeating a known mistake?

Section titled “How do I write a rule that stops an agent repeating a known mistake?”

After setting up Selvedge, add this instruction to your agent’s project instructions:

Before editing an entity, call Selvedge's prior_attempts for its path.
Read any rejected or reverted approaches and the reasons recorded for them.
If a relevant constraint still applies, account for it in your plan.
If circumstances changed, explain the new evidence and explicitly
supersede the old decision before repeating the approach.
After the work, record the reason and outcome with log_change.
If no record is found, say so; do not assume the approach was never tried.

Use a stable entity path, such as src/cache.py::load_profile or payments.amount. A prior attempt can be an implemented approach or a proposal considered and declined. Record that distinction.

The full agent prompt block includes logging, changesets and CLI fallbacks. Instructions request this behavior; they do not guarantee compliance. Optional native lifecycle hooks can gate watched edits where supported; their capabilities and activation vary by client.

Suppose an agent proposes process-local caching for a profile lookup. An earlier session rejected it because one worker could serve stale data after another worker updated the profile. The implementation alone may not preserve that rejected alternative or its reason.

Record the rejection when the reasoning is available:

Terminal window
selvedge log src/cache.py::load_profile reject \
--reasoning "Process-local caching can serve an old profile after another worker updates it." \
--constraint "Profile updates must be visible across workers." \
--stale-when "Shared invalidation is implemented and covered by tests."

This is a hypothetical example, not a customer result. The worked rejection-and-revisit guide explains the tested command sequence, including setup.

In a new session connected to the same database, ask for the same entity before changing it:

Terminal window
selvedge prior-attempts src/cache.py::load_profile --json
selvedge blame src/cache.py::load_profile

An explicitly recorded rejection can return outcome: "rejected" with confidence: "exact". This labels the evidence for the recorded outcome. It does not prove that the earlier choice was correct or remains applicable.

An empty result means no qualifying record was found under the entity and filters. Check the database location, recorded coverage and entity path before drawing a conclusion. Selvedge does not reconstruct unrecorded reasoning.

The next action is to check whether the constraint still applies. If shared invalidation is now implemented and tested, a person or agent can record why the earlier choice should be reconsidered:

Terminal window
selvedge supersede src/cache.py::load_profile \
--reasoning "Shared invalidation now passes the multi-worker tests; reconsider the earlier rejection." \
--json

In this one-rejection example, the earlier explanation remains in the history. When multiple decisions share an entity, inspect the history and target the intended event explicitly. The record supports review; it does not decide that the new implementation is safe.

Record What it can preserve What to maintain
Project instructions Standing rules and known constraints Keep them current and ensure the agent reads them
ADRs Reasons, alternatives and consequences Maintain an index and retrieve the relevant decision
Git commits and review discussions Change context and rationale Write useful reasons and link related decisions
Selvedge events Queryable reasons and outcomes by entity Log useful records, use consistent paths and check them before editing

These approaches can complement each other. The instructions and ADR comparison explains when a maintained document is enough.

Selvedge is a local MCP server and CLI backed by SQLite. It stores the reasons and outcomes that agents or people explicitly record. The default lookup uses deterministic rules, with no LLM required; an optional fuzzy lookup ranks candidates with a local embeddings model.

With uv installed:

Terminal window
uv tool install --upgrade selvedge
selvedge setup

Follow the quickstart for your client. Verify that one saved decision is retrieved in a later session before relying on the workflow.

This workflow is part of agent memory across sessions and a decision audit trail. It preserves the stated reason for a choice, including an approach that was considered and rejected without changing code.

Follow the first-decision verification guide to test recording, retrieval in a fresh session, and use of the record separately. For supported edit checks and startup context, see agent lifecycle hooks.

How do I stop repeating the same corrections to my coding agent?

Turn a recurring correction into a maintained instruction or decision record. State the constraint and the reason, make it discoverable before the relevant edit, and verify that a new session retrieves it. Revise the record when the underlying requirement changes.

Does prior_attempts prevent every repeated mistake?

No. The relevant event must have been recorded, the query must find it, and the agent must assess and follow it. Missing records and ignored instructions remain possible.

What if the mistake happened before Selvedge was installed?

You can explicitly record a known past decision or import supported history. Distinguish a retrospective explanation from a reason captured at the time. An import cannot recover reasoning absent from its source.

Does it work with my agent?

Selvedge works with any agent that can use local stdio MCP tools or invoke the CLI. Use a setup preset or connect another compatible agent. Native lifecycle hooks are optional and vary by client.