Skip to main content

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.
Catch this to handle any SDK-related error:

CLIConnectionError

Raised when unable to connect to Claude Code.
This is a base class for connection-related errors. Usually you’ll see its subclass CLINotFoundError instead.

CLINotFoundError

Raised when Claude Code CLI is not found or not installed.
str
default:"Claude Code not found"
Error message.
str | None
Path where the CLI was expected, if known.

Example

Common Causes

  • Claude Code CLI not installed
  • CLI not in system PATH
  • Incorrect cli_path in ClaudeAgentOptions

Solutions

  1. Install Claude Code:
  2. Specify custom CLI path:

ProcessError

Raised when the CLI process fails.
str
required
Error message.
int | None
CLI process exit code.
str | None
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.
str
required
The line that failed to parse (truncated to 100 chars in message).
Exception
required
The underlying JSON decode exception.

Example

Common Causes

  • CLI version mismatch
  • Corrupted output
  • CLI bug
  • Mixing stdout and stderr

Solutions

  1. Update Claude Code CLI to latest version
  2. Check for CLI warnings/errors
  3. Report issue if persistent

MessageParseError

Raised when unable to parse a message from CLI output.
str
required
Error message.
dict[str, Any] | None
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