Complete API reference for the Claude Code Python SDK, including all functions, types, and classes.
query()
and ClaudeSDKClient
Feature | query() | ClaudeSDKClient |
---|---|---|
Session | Creates new session each time | Reuses same session |
Conversation | Single exchange | Multiple exchanges in same context |
Connection | Managed automatically | Manual control |
Streaming Input | ✅ Supported | ✅ Supported |
Interrupts | ❌ Not supported | ✅ Supported |
Hooks | ❌ Not supported | ✅ Supported |
Custom Tools | ❌ Not supported | ✅ Supported |
Continue Chat | ❌ New session each time | ✅ Maintains conversation |
Use Case | One-off tasks | Continuous conversations |
query()
(New Session Each Time)ClaudeSDKClient
(Continuous Conversation)query()
query()
starts fresh with no memory of previous interactions.
Parameter | Type | Description |
---|---|---|
prompt | str | AsyncIterable[dict] | The input prompt as a string or async iterable for streaming mode |
options | ClaudeCodeOptions | None | Optional configuration object (defaults to ClaudeCodeOptions() if None) |
AsyncIterator[Message]
that yields messages from the conversation.
tool()
Parameter | Type | Description |
---|---|---|
name | str | Unique identifier for the tool |
description | str | Human-readable description of what the tool does |
input_schema | type | dict[str, Any] | Schema defining the tool’s input parameters (see below) |
SdkMcpTool
instance.
create_sdk_mcp_server()
Parameter | Type | Default | Description |
---|---|---|---|
name | str | - | Unique identifier for the server |
version | str | "1.0.0" | Server version string |
tools | list[SdkMcpTool[Any]] | None | None | List of tool functions created with @tool decorator |
McpSdkServerConfig
object that can be passed to ClaudeCodeOptions.mcp_servers
.
ClaudeSDKClient
query()
function works internally - it creates a client object that can continue conversations.
query()
calls@tool
decorator) and hooksMethod | Description |
---|---|
__init__(options) | Initialize the client with optional configuration |
connect(prompt) | Connect to Claude with an optional initial prompt or message stream |
query(prompt, session_id) | Send a new request in streaming mode |
receive_messages() | Receive all messages from Claude as an async iterator |
receive_response() | Receive messages until and including a ResultMessage |
interrupt() | Send interrupt signal (only works in streaming mode) |
disconnect() | Disconnect from Claude |
Important: When iterating over messages, avoid using break
to exit early as this can cause asyncio cleanup issues. Instead, let the iteration complete naturally or use flags to track when you’ve found what you need.
SdkMcpTool
@tool
decorator.
Property | Type | Description |
---|---|---|
name | str | Unique identifier for the tool |
description | str | Human-readable description |
input_schema | type[T] | dict[str, Any] | Schema for input validation |
handler | Callable[[T], Awaitable[dict[str, Any]]] | Async function that handles tool execution |
ClaudeCodeOptions
Property | Type | Default | Description |
---|---|---|---|
allowed_tools | list[str] | [] | List of allowed tool names |
max_thinking_tokens | int | 8000 | Maximum tokens for thinking process |
system_prompt | str | None | None | Replace the default system prompt entirely |
append_system_prompt | str | None | None | Text to append to the default system prompt |
mcp_servers | dict[str, McpServerConfig] | str | Path | {} | MCP server configurations or path to config file |
permission_mode | PermissionMode | None | None | Permission mode for tool usage |
continue_conversation | bool | False | Continue the most recent conversation |
resume | str | None | None | Session ID to resume |
max_turns | int | None | None | Maximum conversation turns |
disallowed_tools | list[str] | [] | List of disallowed tool names |
model | str | None | None | Claude model to use |
permission_prompt_tool_name | str | None | None | MCP tool name for permission prompts |
cwd | str | Path | None | None | Current working directory |
settings | str | None | None | Path to settings file |
add_dirs | list[str | Path] | [] | Additional directories Claude can access |
extra_args | dict[str, str | None] | {} | Additional CLI arguments to pass directly to the CLI |
can_use_tool | CanUseTool | None | None | Tool permission callback function |
hooks | dict[HookEvent, list[HookMatcher]] | None | None | Hook configurations for intercepting events |
PermissionMode
McpSdkServerConfig
create_sdk_mcp_server()
.
McpServerConfig
McpStdioServerConfig
McpSSEServerConfig
McpHttpServerConfig
Message
UserMessage
AssistantMessage
SystemMessage
ResultMessage
ContentBlock
TextBlock
ThinkingBlock
ToolUseBlock
ToolResultBlock
ClaudeSDKError
CLINotFoundError
CLIConnectionError
ProcessError
CLIJSONDecodeError
HookEvent
HookCallback
input_data
: Hook-specific input data (see hook documentation)tool_use_id
: Optional tool use identifier (for tool-related hooks)context
: Hook context with additional informationdecision
: "block"
to block the actionsystemMessage
: System message to add to the transcripthookSpecificOutput
: Hook-specific output dataHookContext
HookMatcher
Task
Input:
Bash
Input:
Edit
Input:
MultiEdit
Input:
Read
Input:
Write
Input:
Glob
Input:
Grep
Input:
NotebookEdit
Input:
WebFetch
Input:
WebSearch
Input:
TodoWrite
Input:
BashOutput
Input:
KillBash
Input:
ExitPlanMode
Input:
ListMcpResources
Input:
ReadMcpResource
Input: