Skip to main content

Error Recovery

Robust error handling is critical for production AI applications. The AI SDK provides multiple patterns for error detection, handling, and recovery.

Error Types

AI SDK Error Classes

The AI SDK provides specific error classes:

Common Error Scenarios

Retry Strategies

Built-in Retry

Use the maxRetries option:

Custom Retry Logic

Implement exponential backoff:

Selective Retry

Retry only on specific errors:

Fallback Models

Fall back to alternative models:

Streaming Error Recovery

Handling Stream Errors

Partial Results on Error

Resumable Streams

Tool Call Error Handling

Validating Tool Inputs

Tool Execution Errors

RSC Error Recovery

Streamable UI Errors

Client-Side Error Boundaries

Graceful Degradation

Timeout Handling

AbortSignal

Timeout Wrapper

Logging and Monitoring

Error Tracking

Best Practices

  1. Use specific error checks: Use ErrorClass.isInstance() instead of instanceof
  2. Implement retry logic: Add exponential backoff for transient failures
  3. Set timeouts: Prevent indefinite hangs with AbortSignal
  4. Validate inputs: Catch errors early with schema validation
  5. Provide fallbacks: Have alternative models or cached responses
  6. Log errors: Track failures for debugging and monitoring
  7. Handle partial results: Save progress in streaming scenarios
  8. Use error boundaries: Isolate UI errors in React applications

Next Steps