Error Hierarchy
All SDK errors inherit from the baseClaudeSDKError exception:
ClaudeSDKError
Base exception for all Claude SDK errors.CLIConnectionError
Raised when unable to connect to Claude Code.- Claude Code CLI is not running
- Network connectivity issues
- Invalid transport configuration
- Permission denied when starting CLI process
CLINotFoundError
Raised when Claude Code is not found or not installed.- Claude Code CLI is not installed
- CLI is not in PATH
- Invalid
cli_pathspecified in options - Insufficient permissions to execute the CLI
ProcessError
Raised when the CLI process fails.exit_code(int | None): The process exit codestderr(str | None): Error output from the process
- CLI crashed or was terminated
- Invalid CLI arguments
- Resource exhaustion (memory, disk space)
- Permission issues accessing files or directories
CLIJSONDecodeError
Raised when unable to decode JSON from CLI output.line(str): The line that failed to parseoriginal_error(Exception): The original JSON decode error
- Corrupted CLI output
- CLI version mismatch
- Incomplete or truncated messages
- CLI outputting non-JSON data
MessageParseError
Raised when unable to parse a message from CLI output.data(dict[str, Any] | None): The data that failed to parse
- Unexpected message format
- Missing required fields
- Type mismatches in message data
- SDK version incompatible with CLI version
Error Handling Patterns
Basic Error Handling
Catch all SDK errors with a single handler:Granular Error Handling
Handle different error types differently:Retry Logic
Implement retry logic for transient failures:Context Manager Error Handling
Handle errors in ClaudeSDKClient with context managers:Logging Errors
Integrate with Python’s logging system:Graceful Degradation
Provide fallback behavior when errors occur:Assistant Message Errors
In addition to exceptions,AssistantMessage objects can contain error information:
"authentication_failed"- API authentication failed"billing_error"- Billing or payment issue"rate_limit"- Rate limit exceeded"invalid_request"- Invalid request parameters"server_error"- Server-side error"unknown"- Unknown error
Complete Example
Always handle SDK errors appropriately in production code. Use specific error types for granular handling and implement retry logic for transient failures.