Skip to main content
The Claude Agent SDK provides a structured error hierarchy for handling different types of failures that can occur during interactions with Claude Code.

Error Hierarchy

All SDK errors inherit from the base ClaudeSDKError exception:

ClaudeSDKError

Base exception for all Claude SDK errors.
Use this to catch any SDK-related error:

CLIConnectionError

Raised when unable to connect to Claude Code.
Common Causes:
  • Claude Code CLI is not running
  • Network connectivity issues
  • Invalid transport configuration
  • Permission denied when starting CLI process
Example:

CLINotFoundError

Raised when Claude Code is not found or not installed.
Common Causes:
  • Claude Code CLI is not installed
  • CLI is not in PATH
  • Invalid cli_path specified in options
  • Insufficient permissions to execute the CLI
Example:

ProcessError

Raised when the CLI process fails.
Attributes:
  • exit_code (int | None): The process exit code
  • stderr (str | None): Error output from the process
Common Causes:
  • CLI crashed or was terminated
  • Invalid CLI arguments
  • Resource exhaustion (memory, disk space)
  • Permission issues accessing files or directories
Example:

CLIJSONDecodeError

Raised when unable to decode JSON from CLI output.
Attributes:
  • line (str): The line that failed to parse
  • original_error (Exception): The original JSON decode error
Common Causes:
  • Corrupted CLI output
  • CLI version mismatch
  • Incomplete or truncated messages
  • CLI outputting non-JSON data
Example:

MessageParseError

Raised when unable to parse a message from CLI output.
Attributes:
  • data (dict[str, Any] | None): The data that failed to parse
Common Causes:
  • Unexpected message format
  • Missing required fields
  • Type mismatches in message data
  • SDK version incompatible with CLI version
Example:

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:
Error Types:
  • "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.
When catching ClaudeSDKError, make sure to handle more specific error types first (like CLINotFoundError) before the base exception to avoid masking important error details.