
Build a Supervisor-Worker multi-agent system that catches worker failures, routes tasks to a fallback agent with a different strategy, and logs every recovery action. Full tested code on GitHub.
Every agentic AI demo looks the same: a clean prompt goes in, a perfect tool call comes out, everybody claps. Then you deploy it, a worker agent hits an edge case it wasn't built for, throws an exception, and your "autonomous" system falls over in front of a customer.
The pattern that separates toy agents from production agents isn't a bigger model or a longer prompt. It's what happens after something fails.
This article walks through SelfHeal, a small but genuinely production-shaped multi-agent system that implements the Supervisor-Worker pattern with automatic error recovery. When a worker agent fails, the Supervisor doesn't crash or return a raw stack trace. It catches the failure, reroutes the task to a fallback agent with a different strategy, and logs exactly how it healed itself.
Every line of code in this article was written and executed in a sandboxed environment. The full, tested code is available on GitHub at Self-Healing-Agent.
How the Supervisor-Worker Pattern Works
The core architecture is straightforward. A Supervisor agent receives a task and assigns it to a primary Worker agent. If the Worker succeeds, the Supervisor returns the result. If the Worker throws an exception, the Supervisor catches it, logs the error, and assigns the same task to a fallback Worker that uses a different approach.
The fallback Worker has a modified system prompt that tells it to avoid the failure mode the primary Worker hit. If the fallback also fails, the Supervisor logs both failures and returns a structured error message. No raw stack trace reaches the caller.
The Code Structure
The system has three components:
Worker agents – each has a system prompt and a set of tools. The primary Worker uses a standard approach. The fallback Worker uses a more conservative strategy.
Supervisor agent – receives the task, calls the primary Worker, catches exceptions, and routes to the fallback if needed.
The Supervisor's core loop looks like this:
def run_task(task):
try:
result = primary_worker.run(task)
return result
except Exception as e:
log_error(task, e)
try:
result = fallback_worker.run(task)
log_recovery(task, e)
return result
except Exception as e2:
log_error(task, e2)
return {"status": "failed", "errors": [str(e), str(e2)]}
Why This Matters for Production Deployments
Most agent frameworks treat failure as an edge case. In production, failure is the baseline. APIs go down. Models return malformed JSON. Tools timeout. A system that cannot recover from these failures is not production-ready.
The SelfHeal pattern adds roughly 30 lines of orchestration code and eliminates the most common failure mode in multi-agent systems: the unhandled exception that kills the entire pipeline.
Testing the Recovery
In the sandboxed test, the primary Worker was given a task that required a tool call to an API endpoint that returned a 500 error. The Worker threw an exception. The Supervisor caught it, logged "Primary worker failed: HTTP 500 from pricing API", and routed the task to the fallback Worker with a prompt that said "Use cached data instead of live API calls." The fallback returned a result using cached pricing data. The Supervisor logged "Recovery successful: fallback worker returned cached result."
The full test run, including the error logs and recovery trace, is in the GitHub repository.
The Takeaway
Building agents that recover from failure is not harder than building agents that assume success. It just requires a Supervisor that listens for exceptions and a fallback that knows what went wrong. The code is on GitHub. The pattern works. The next step is adding retry logic with exponential backoff and a circuit breaker for persistent failures.
Drafted by a large language model from the source reporting linked above, then screened by automated publishing checks. It is not read by a journalist before publication. Some articles cite our Alpha Score. Verify prices and figures against the original source. Educational coverage, not personalized advice.