|
1 | | -# claude-code-sdk-python |
| 1 | +# Claude Code SDK for Python |
| 2 | + |
| 3 | +Python SDK for Claude Code. See the [Claude Code SDK documentation](https://docs.anthropic.com/en/docs/claude-code/sdk) for more information. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +pip install claude-code-sdk |
| 9 | +``` |
| 10 | + |
| 11 | +**Prerequisites:** |
| 12 | +- Python 3.10+ |
| 13 | +- Node.js |
| 14 | +- Claude Code: `npm install -g @anthropic-ai/claude-code` |
| 15 | + |
| 16 | +## Quick Start |
| 17 | + |
| 18 | +```python |
| 19 | +import anyio |
| 20 | +from claude_code_sdk import query |
| 21 | + |
| 22 | +async def main(): |
| 23 | + async for message in query(prompt="What is 2 + 2?"): |
| 24 | + print(message) |
| 25 | + |
| 26 | +anyio.run(main) |
| 27 | +``` |
| 28 | + |
| 29 | +## Usage |
| 30 | + |
| 31 | +### Basic Query |
| 32 | + |
| 33 | +```python |
| 34 | +from claude_code_sdk import query, ClaudeCodeOptions, AssistantMessage, TextBlock |
| 35 | + |
| 36 | +# Simple query |
| 37 | +async for message in query(prompt="Hello Claude"): |
| 38 | + if isinstance(message, AssistantMessage): |
| 39 | + for block in message.content: |
| 40 | + if isinstance(block, TextBlock): |
| 41 | + print(block.text) |
| 42 | + |
| 43 | +# With options |
| 44 | +options = ClaudeCodeOptions( |
| 45 | + system_prompt="You are a helpful assistant", |
| 46 | + max_turns=1 |
| 47 | +) |
| 48 | + |
| 49 | +async for message in query(prompt="Tell me a joke", options=options): |
| 50 | + print(message) |
| 51 | +``` |
| 52 | + |
| 53 | +### Using Tools |
| 54 | + |
| 55 | +```python |
| 56 | +options = ClaudeCodeOptions( |
| 57 | + allowed_tools=["Read", "Write", "Bash"], |
| 58 | + permission_mode='acceptEdits' # auto-accept file edits |
| 59 | +) |
| 60 | + |
| 61 | +async for message in query( |
| 62 | + prompt="Create a hello.py file", |
| 63 | + options=options |
| 64 | +): |
| 65 | + # Process tool use and results |
| 66 | + pass |
| 67 | +``` |
| 68 | + |
| 69 | +### Working Directory |
| 70 | + |
| 71 | +```python |
| 72 | +from pathlib import Path |
| 73 | + |
| 74 | +options = ClaudeCodeOptions( |
| 75 | + cwd="/path/to/project" # or Path("/path/to/project") |
| 76 | +) |
| 77 | +``` |
| 78 | + |
| 79 | +## API Reference |
| 80 | + |
| 81 | +### `query(prompt, options=None)` |
| 82 | + |
| 83 | +Main async function for querying Claude. |
| 84 | + |
| 85 | +**Parameters:** |
| 86 | +- `prompt` (str): The prompt to send to Claude |
| 87 | +- `options` (ClaudeCodeOptions): Optional configuration |
| 88 | + |
| 89 | +**Returns:** AsyncIterator[Message] - Stream of response messages |
| 90 | + |
| 91 | +### Types |
| 92 | + |
| 93 | +See [src/claude_code_sdk/types.py](src/claude_code_sdk/types.py) for complete type definitions: |
| 94 | +- `ClaudeCodeOptions` - Configuration options |
| 95 | +- `AssistantMessage`, `UserMessage`, `SystemMessage`, `ResultMessage` - Message types |
| 96 | +- `TextBlock`, `ToolUseBlock`, `ToolResultBlock` - Content blocks |
| 97 | + |
| 98 | +## Error Handling |
| 99 | + |
| 100 | +```python |
| 101 | +from claude_code_sdk import ( |
| 102 | + ClaudeSDKError, # Base error |
| 103 | + CLINotFoundError, # Claude Code not installed |
| 104 | + CLIConnectionError, # Connection issues |
| 105 | + ProcessError, # Process failed |
| 106 | + CLIJSONDecodeError, # JSON parsing issues |
| 107 | +) |
| 108 | + |
| 109 | +try: |
| 110 | + async for message in query(prompt="Hello"): |
| 111 | + pass |
| 112 | +except CLINotFoundError: |
| 113 | + print("Please install Claude Code") |
| 114 | +except ProcessError as e: |
| 115 | + print(f"Process failed with exit code: {e.exit_code}") |
| 116 | +except CLIJSONDecodeError as e: |
| 117 | + print(f"Failed to parse response: {e}") |
| 118 | +``` |
| 119 | + |
| 120 | +See [src/claude_code_sdk/_errors.py](src/claude_code_sdk/_errors.py) for all error types. |
| 121 | + |
| 122 | +## Available Tools |
| 123 | + |
| 124 | +See the [Claude Code documentation](https://docs.anthropic.com/en/docs/claude-code/security#tools-available-to-claude) for a complete list of available tools. |
| 125 | + |
| 126 | +## Examples |
| 127 | + |
| 128 | +See [examples/quick_start.py](examples/quick_start.py) for a complete working example. |
| 129 | + |
| 130 | +## License |
| 131 | + |
| 132 | +MIT |
0 commit comments