0Pricing
AI Agents · Lesson

Detecting and Recovering from Tool Errors

When a tool 500s, return the error to the model so it can try a different approach instead of crashing.

Tools Fail. Plan For It.

Every real tool fails sometimes:

  • Network timeouts
  • Rate limits
  • Bad arguments from the model
  • External service downtime
  • Invalid auth

Production agents must recover gracefully.

Always Return, Never Raise

Inside the agent loop, catch all tool errors and return them as content. Never let an exception kill the loop:

def safe_dispatch(tool_call):
    try:
        args = json.loads(tool_call.function.arguments)
        return TOOLS[tool_call.function.name](**args)
    except json.JSONDecodeError:
        return {'error': 'Arguments are not valid JSON.'}
    except KeyError:
        return {'error': f'Unknown tool: {tool_call.function.name}'}
    except Exception as e:
        return {'error': f'{type(e).__name__}: {e}'}

All lessons in this course

  1. ReAct: Reason + Act Pattern
  2. Implementing ReAct from Scratch
  3. Common Tool Sets (Web, Calculator, RAG)
  4. Detecting and Recovering from Tool Errors
← Back to AI Agents