The Claude Code SDK is a control protocol for embedding Claude Code in other applications — IDEs, automation scripts, CI/CD pipelines, or any host that can spawn a subprocess and communicate over stdin/stdout. Rather than exposing a library API directly, the SDK communicates with a runningDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/VineeTagarwaL-code/claude-code/llms.txt
Use this file to discover all available pages before exploring further.
claude process over a structured JSON message stream. The host process sends user messages and control requests; the CLI process streams back assistant messages, tool progress events, and result payloads.
@anthropic-ai/claude-code under the agentSdkTypes entry point. Control protocol types (prefixed SDKControl) are @alpha and subject to change.How it works
Spawn a Claude Code process
claude with --output-format stream-json and --print (non-interactive mode). Pipe its stdin and stdout into your host process.--print and instead send SDKUserMessage objects to stdin after the session initializes.Send an initialize request
control_request with subtype: "initialize" to stdin. The CLI responds with an SDKControlInitializeResponse containing available commands, models, agents, and account information.Stream messages from stdout
SDKMessage union types — assistant turns, tool progress, system events, and result summaries.Output formats
Pass--output-format to control what Claude Code writes to stdout.
| Format | Description |
|---|---|
text | Plain text responses only. Default for interactive mode. |
json | Single JSON object written at completion. Suitable for one-shot scripts. |
stream-json | Newline-delimited JSON stream. One message per line, emitted as events occur. Required for SDK use. |
Control protocol messages
The control protocol uses two top-level envelope types that flow bidirectionally over stdin/stdout.SDKControlRequest
Sent to the CLI process to configure the session or issue commands.
"control_request".control_response.subtype identifies which operation to perform.SDKControlResponse
Emitted from the CLI process in response to a control_request.
subtype is "error" and the error field contains a human-readable message.
Initialize request and response
Theinitialize request is the first control message you must send. It configures the session and returns available capabilities.
SDKControlInitializeRequest
createSdkMcpServer) to connect to this session.Agent tool during this session.SDKControlInitializeResponse
The CLI responds with the session’s current capabilities.
/compact, /cost). Each entry has name, description, and argumentHint.name, description, and an optional model."stream-json", "json", "text").User messages
Send user messages to stdin to drive the conversation forward.SDKUserMessage
"user".content can be a string or a content block array (for images and other media).null for top-level user messages.SDK message stream types
Claude Code emits a stream of JSON messages to stdout. Thetype field identifies each message.
system — session initialization
system — session initialization
subtype: "init". Contains the active model, tool list, MCP server statuses, permission mode, and session ID.assistant — model response
assistant — model response
tool_use blocks.stream_event — partial streaming tokens
stream_event — partial streaming tokens
RawMessageStreamEvent payloads. Use these to render incremental output.tool_progress — long-running tool status
tool_progress — long-running tool status
tool_name, tool_use_id, and elapsed time.result — final turn summary
result — final turn summary
subtype is "success" or one of the error subtypes."error_during_execution", "error_max_turns", "error_max_budget_usd", "error_max_structured_output_retries".system — status updates
system — status updates
subtype: "status" when the permission mode or session status changes (e.g., "compacting").Other control requests
Beyondinitialize, the control protocol exposes these operations.
subtype | Direction | Description |
|---|---|---|
interrupt | host → CLI | Interrupt the current turn. |
set_permission_mode | host → CLI | Change the active permission mode. |
set_model | host → CLI | Switch to a different model mid-session. |
can_use_tool | CLI → host | Permission request for a tool call (requires SDK permission handler). |
mcp_status | host → CLI | Get MCP server connection statuses. |
mcp_set_servers | host → CLI | Replace dynamically managed MCP servers. |
get_context_usage | host → CLI | Get context window usage breakdown. |
get_settings | host → CLI | Read the effective merged settings. |
apply_flag_settings | host → CLI | Merge settings into the flag settings layer. |
rewind_files | host → CLI | Revert file changes made since a given message. |
hook_callback | CLI → host | Deliver a hook event for an SDK-registered hook callback. |
reload_plugins | host → CLI | Reload plugins from disk. |
Session management API
For scripting scenarios, the SDK exports functions that operate on saved session transcripts stored in~/.claude/.
query — run a prompt
query — run a prompt
prompt string or AsyncIterable<SDKUserMessage> and returns an async iterable of SDKMessage.listSessions — list saved sessions
listSessions — list saved sessions
dir to scope to a specific project, or omit to list all sessions.getSessionMessages — read a transcript
getSessionMessages — read a transcript
forkSession — branch a conversation
forkSession — branch a conversation
upToMessageId to fork from a specific point.renameSession and tagSession
renameSession and tagSession
Use cases
- IDE integration
- CI/CD automation
- Headless agents
initialize request with a custom systemPrompt that describes the IDE context, then forward user messages from the editor’s chat panel. Use PreToolUse hook callbacks to intercept file edits and display diffs in the IDE’s native UI before they are applied.