Overview
The Claude Agent SDK provides a hierarchy of exception types for handling different error conditions.Error Hierarchy
ClaudeSDKError
Base exception for all Claude SDK errors.CLIConnectionError
Raised when unable to connect to Claude Code.CLINotFoundError instead.
CLINotFoundError
Raised when Claude Code CLI is not found or not installed.Error message.
Path where the CLI was expected, if known.
Example
Common Causes
- Claude Code CLI not installed
- CLI not in system PATH
- Incorrect
cli_pathinClaudeAgentOptions
Solutions
-
Install Claude Code:
-
Specify custom CLI path:
ProcessError
Raised when the CLI process fails.Error message.
CLI process exit code.
Standard error output from the CLI process.
Example
Common Causes
- Invalid CLI arguments
- Authentication failures
- API key issues
- Resource exhaustion
- CLI crashes
CLIJSONDecodeError
Raised when unable to decode JSON from CLI output.The line that failed to parse (truncated to 100 chars in message).
The underlying JSON decode exception.
Example
Common Causes
- CLI version mismatch
- Corrupted output
- CLI bug
- Mixing stdout and stderr
Solutions
- Update Claude Code CLI to latest version
- Check for CLI warnings/errors
- Report issue if persistent
MessageParseError
Raised when unable to parse a message from CLI output.Error message.
The message data that failed to parse.
Example
Common Causes
- Unexpected message format
- SDK version mismatch with CLI
- Missing required fields
- Type validation failures
Error Handling Best Practices
Specific Error Handling
Graceful Degradation
Logging Errors
Context Managers
Related Documentation
- ClaudeSDKClient - Main client class
- query() - One-shot query function
- ClaudeAgentOptions - Configuration options