Exception Types
Agent Errors
| Exception | When It’s Raised |
|---|---|
AgentError | General agent execution failure |
MaxIterationsError | Agent exceeded max_iterations in the tool-call loop |
ToolExecutionError | A tool raised an exception during execution |
Model Errors
Model errors typically come from the underlying provider SDK (e.g.,openai.APIError). Common cases:
| Error | Cause | Recommended Action |
|---|---|---|
AuthenticationError | Invalid API key | Check your API key |
RateLimitError | Too many requests | Use retries with backoff |
APIConnectionError | Network failure | Use retries |
APITimeoutError | Request timed out | Increase timeout or use retries |
BadRequestError | Invalid request parameters | Check model parameters |
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