Skip to main content
See the Claude adapter guide for a working example using this protocol with Runloop.
This document specifies the client-side API for interacting with Claude Code via the JSON Lines protocol. Official documentation: https://platform.claude.com/docs/en/agent-sdk/user-input

Overview

The SDK provides a bidirectional communication channel with Claude Code:
  • Input: Messages sent from the SDK to Claude
  • Output: Messages received from Claude
All messages are JSON Lines (newline-delimited JSON) over stdin/stdout.

Client Methods


Query

Send a user message and receive a stream of responses until a Result message indicates completion. UserMessage:
Fields: ContentBlock variants:

Control Request

Send a control request to Claude. Payload (Initialize - enable tool approval protocol):
Payload (Interrupt - stop current response):

Control Response

Respond to a ControlRequest received from Claude. All responses use the subtype: "success" envelope with a nested response object. Payload (Allow):
Payload (Allow with permissions):
Payload (Deny):
Payload (Deny with interrupt):
Response Fields:

Request Types (Claude → SDK)

When tool approval is enabled, Claude sends ControlRequest messages to the SDK.

CanUseTool

Claude requests permission to use a tool.
Fields:

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:
Response (Execute Plan):
Response (Revise):
Response (Reject):

AskUserQuestion (Structured Questions)

Claude can ask structured multiple-choice questions for user clarification. Request:
Response (Answer):
Response (Skip):

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 via receive().

System

Initialization, status, and task messages. Init (session start):
Status (e.g., compacting):
Compact Boundary:
Task Started:
Task Progress:
Task Notification:

User

Echo of the user message sent.

Assistant

Claude’s response.

Result

Query completion. Success:
Error (max turns exceeded):
Error (during execution):
Result Fields:

Error

Anthropic API error.

RateLimitEvent

Rate limit status.
RateLimitInfo Fields:

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

Version Compatibility

The protocol is unstable. Current tested version: 2.1.52