Exception Types
Agent Errors
Model Errors
Model errors typically come from the underlying provider SDK (e.g.,openai.APIError). Common cases:
MCP Errors
See MCP Error Handling for the full MCP error hierarchy.Agent-Level Retries
Configure retries directly on the agent:Model-Level Retries
Configure retries on the model itself for finer control:Retry Middleware
For the most control, useRetryMiddleware:
ConnectionError, TimeoutError, and OSError with exponential backoff.
Tool Error Handling
When a tool raises an exception, the error message is sent back to the model. The model can then decide to retry the call, try a different approach, or inform the user:divide(10, 0), it receives the error "Cannot divide by zero" and can adjust its approach.
Catching Errors in Application Code
Streaming Error Handling
During streaming, errors arrive asRunErrorEvent:
Best Practices
- Set appropriate timeouts — Don’t let requests hang indefinitely
- Use retries for transient errors — Network issues are temporary
- Raise clear errors in tools — The model uses error messages to self-correct
- Log errors — Use
LoggingMiddlewarefor visibility into failures - Test error paths — Use
MockModelwithside_effectto simulate failures