Skip to main content
Definable is designed to fail fast with clear, actionable errors. This page covers the exception types, retry strategies, and patterns for building resilient agents.

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:
This retries the entire agent run on transient errors (connection failures, timeouts).

Model-Level Retries

Configure retries on the model itself for finer control:
Model retries apply to individual API calls. Agent retries wrap the entire run.

Retry Middleware

For the most control, use RetryMiddleware:
The middleware retries on 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:
If the model calls 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 as RunErrorEvent:

Best Practices

  1. Set appropriate timeouts — Don’t let requests hang indefinitely
  2. Use retries for transient errors — Network issues are temporary
  3. Raise clear errors in tools — The model uses error messages to self-correct
  4. Log errors — Use LoggingMiddleware for visibility into failures
  5. Test error paths — Use MockModel with side_effect to simulate failures