Overview
The SDK provides a bidirectional communication channel with Claude Code:- Input: Messages sent from the SDK to Claude
- Output: Messages received from Claude
Client Methods
| Method | Description | Request Message |
|---|---|---|
query | Send user message, receive response stream | UserMessage |
control_request | Send control request (initialize, interrupt) | ControlRequest |
control_response | Respond to tool permission requests | ControlResponse |
Query
Send a user message and receive a stream of responses until aResult message indicates completion.
UserMessage:
| Field | Type | Required | Description |
|---|---|---|---|
type | "user" | yes | Message type discriminator |
message.role | "user" | yes | Role identifier |
message.content | ContentBlock[] | yes | Array of content blocks |
session_id | string (UUID) | no | Session identifier |
Control Request
Send a control request to Claude. Payload (Initialize - enable tool approval protocol):Control Response
Respond to aControlRequest received from Claude. All responses use the subtype: "success" envelope with a nested response object.
Payload (Allow):
| Field | Type | Description |
|---|---|---|
behavior | "allow" | "deny" | Whether to allow or deny the tool use |
updatedInput | object | Tool input to use (required for allow) |
updatedPermissions | array | Permission rules to add (optional, allow only) |
message | string | Denial reason (required for deny) |
interrupt | boolean | Stop agent execution (optional, deny only) |
Request Types (Claude → SDK)
When tool approval is enabled, Claude sendsControlRequest messages to the SDK.
CanUseTool
Claude requests permission to use a tool.
| Field | Type | Description |
|---|---|---|
request_id | string | Unique ID to reference in response |
request.subtype | "can_use_tool" | Request type discriminator |
request.tool_name | string | Name of tool (Bash, Read, Write, Edit, etc.) |
request.tool_use_id | string | ID of the tool_use content block |
request.input | object | Tool-specific input parameters |
ExitPlanMode (Plan Review)
When Claude runs in plan mode, it sends this request asking the user to approve, revise, or reject the plan before executing.
Request:
AskUserQuestion (Structured Questions)
Claude can ask structured multiple-choice questions for user clarification.
Request:
HookCallback
Notification about a hook event.
McpMessage
MCP (Model Context Protocol) server communication.
SDKControlInterrupt
Claude acknowledges an interrupt request.
Output Types (Claude → SDK)
These are the message types received from Claude viareceive().
System
Initialization, status, and task messages.
Init (session start):
User
Echo of the user message sent.
Assistant
Claude’s response.
Result
Query completion.
Success:
| Field | Type | Required | Description |
|---|---|---|---|
subtype | string | yes | "success", "error_max_turns", or "error_during_execution" |
is_error | boolean | yes | Whether the result represents an error |
duration_ms | number | yes | Total duration in milliseconds |
duration_api_ms | number | yes | API call duration in milliseconds |
num_turns | number | yes | Number of agentic turns |
session_id | string | yes | Session identifier |
total_cost_usd | number | yes | Total cost in USD |
errors | string[] | no | Error messages (when is_error is true) |
permission_denials | array | no | Tools blocked due to permission denials |
uuid | string | no | Unique identifier for this message |
Error
Anthropic API error.
RateLimitEvent
Rate limit status.
| Field | Type | Required | Description |
|---|---|---|---|
status | string | yes | "allowed", "allowed_warning", or "rejected" |
resetsAt | number | no | Unix timestamp when the rate limit resets |
rateLimitType | string | no | "five_hour", "hourly", or "seven_day" |
utilization | number | no | Utilization ratio (0.0 to 1.0) |
overageStatus | string | no | "allowed" or "rejected" |
overageDisabledReason | string | no | "org_level_disabled" or "out_of_credits" |
isUsingOverage | boolean | yes | Whether overage billing is active |
ControlRequest
See Request Types section above.
ControlResponse
Acknowledgment for SDK-initiated control requests (e.g., initialize handshake).
Typical Flow
Wire Protocol
- Transport: stdin/stdout of Claude CLI process
- Format: JSON Lines (one JSON object per line, newline-delimited)
- Encoding: UTF-8
- Buffer size: 10MB recommended for stdout reader
