MentorMe
·4 min read

The Self-Healing Agent — Teaching Claude to Fix Its Own Errors

Try-three-fixes-before-flagging is a config, not a prayer. How to wire it in.

Claude Codeerror handlingMentorMe
The high country at first light
The high country at first light

Your agent runs into an error. It stops. It waits for you. You're not at your desk. Three hours pass. The task is dead.

This is how most AI agents fail in production. Not because the model is wrong. Because the error-handling assumes a human is always nearby to unstick it. That assumption is broken the moment you want to run anything overnight, on schedule, or at scale.

The self-healing agent flips this. It treats errors as an expected part of the workflow, not an exception. The rule is simple: try three fixes before flagging a human. Once you wire it in, far fewer tasks need your attention.

Here's the mental model. Every agent task has three possible outcomes — success, transient failure, or hard failure. A transient failure is something temporary — API rate limit, network blip, a tool returning a vague error, a parse error on malformed data. A hard failure is something the agent genuinely can't solve — missing credentials, a locked file, a policy block, a task that was impossible from the start. Self-healing is about telling transient failures and hard failures apart, and automatically retrying the transient ones with increasingly different strategies.

The three-fix pattern works like this. Try one — the naive retry. Run the same operation again. Many transient failures are gone by the second attempt. That's free. Try two — the adjusted retry. Change one variable. Switch from the primary API endpoint to a backup. Reduce the request size. Wait longer between attempts. Use a different model. Many more failures clear here. Try three — the alternate approach. Do the task a completely different way. If scraping failed, hit the API. If the API failed, try the RSS feed. If all reads failed, check cache. This catches most of what's left.

After three fixes fail, the agent escalates. And escalation should not be silent. A self-healing agent that quietly gives up is worse than one that never tried. The escalation should include what was attempted, which error was thrown each time, what the agent thinks the root cause is, and a draft message for the human with a suggested fix. That last part matters — you shouldn't have to diagnose the failure, the agent should hand you a starting hypothesis.

The config looks simpler than you'd expect. Most self-healing logic can live in a wrapper function around your tool calls. Wrap every external API hit in a retry function with exponential backoff. Wrap every parse operation in a try/catch that falls back to a more lenient parser. Wrap every file operation in a permission check. If you're using a framework like LangGraph, the state machine handles this for you. If you're writing raw code, it's a fifteen-line retry helper.

The prompt side of self-healing is where Claude Opus 4.7 shines. In the system prompt, tell the agent explicitly — if a tool call fails, do not stop. Try again. If it fails twice, try a different tool or approach. If it fails three times, write a summary of what you tried and escalate. This single instruction, added to any agent prompt, can change behavior a lot, because the agent stops treating the first error as the end of the task.

One pattern worth adding is error memory. When an agent succeeds after a retry, it logs the combination of error type and the fix that worked. The next time that same error appears, the agent tries the known-good fix first. Over weeks, the agent builds up a library of fixes that works better than any hard-coded retry ladder could. It is the closest thing to real learning you can get from a production agent, and it is just a JSON file that gets fatter over time.

A caution — don't self-heal everything. Some errors should fail loud. Payment processing errors. Data deletion errors. Permission changes. Anything that touches money, security, or irreversible actions should skip the retry loop entirely and go straight to human review. The rule of thumb — if the failure is safe to retry, retry. If the failure could hurt something if the retry works differently than you expected, don't.

The measurable outcome of a self-healing agent is simple. Track two numbers — total tasks attempted and tasks that required human intervention. The ratio is your self-healing rate. Teams with a high self-healing rate have agents that actually run unsupervised. Teams with a low rate are still effectively using AI as a copilot. The ratio is the thing to push.

Wrap your next three agent tool calls in a three-try retry function with exponential backoff and log every retry to a local file.

MCAO certifies what you can DO with AI, not what you know.


Keep building with MentorMe

When you are ready to build on this, these are your next steps:

Related reading

Compare MentorMe