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

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 we've been shipping at MentorMe is simple — try three fixes before flagging a human. And once you wire it in, the number of tasks that actually require your attention drops by 70–90%.

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. About 40% of transient failures are gone by retry two. 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. About another 30% of 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.

"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."

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, changes behavior dramatically. We tested it on a scheduled agent that used to fail once every three runs. After adding the retry prompt, it now fails once every thirty runs. Ten-fold reliability improvement from four sentences in the system prompt.

One specific pattern we use at MentorMe — the 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. This is the closest thing to real learning we've gotten out of production agents. It's not magic. It's just a JSON file that gets fatter over time.

12hr

Median weekly time saved with the C-Suite Team

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 that hit 90%+ self-healing have agents that actually run unsupervised. Teams at 50% 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. Three tiers — Foundation $299, Professional $597, Executive $2,500.

Related reading