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
Query
Send a user message and receive a stream of responses until aResult message indicates completion.
UserMessage:
ContentBlock variants:
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):
Request Types (Claude → SDK)
When tool approval is enabled, Claude sendsControlRequest messages to the SDK.
CanUseTool
Claude requests permission to use a tool.
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:
Error
Anthropic API error.
RateLimitEvent
Rate limit status.
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
