When agents call external APIs or databases, tool execution fails unpredictably without circuit breakers.
Resilient Tool Wrapper Pattern
python
def execute_agent_tool(func, **kwargs):
try:
return {"status": "success", "data": func(**kwargs)}
except Exception as e:
return {
"status": "error",
"error_type": type(e).__name__,
"message": str(e),
"remediation_hint": "Verify argument formatting and try alternative parameters"
}By returning structured JSON errors back into the agent conversation loop rather than raising fatal Python exceptions, the LLM autonomously catches its own mistake and self-corrects on the subsequent turn.