Skip to content

Daemon Protocol Reference

claude-p daemon [OPTIONS] drives a single long-lived interactive claude session over newline-delimited JSON (NDJSON) on stdin and stdout. It shares the one-shot driver’s child environment filtering (buildChildEnv), Claude Code 2.1 dialog engine (maybeHandleStartupDialogs), and paced bracketed paste (PasteChunker). The daemon writes one paste chunk per event-loop tick instead of calling the blocking typePrompt, so ping, interrupt, and DEC replies stay responsive during a large paste.

stdin (NDJSON) {"type":"user", ...} Queued (64 prompts / 64 MiB); pasted when idle. {"type":"ping"} Answered inline in any state. {"type":"interrupt"} Sends ESC while busy; keeps session open for next turn. EOF (stdin closed) Drains active turn + queue, terminates child, exits 0. blank line Ignored silently; no frame. daemon state loop waiting_for_ready --timeout (300 s → exit 124) · EOF → 0 idle watchdogs paused while parked busy --idle-timeout (180 s PTY) --transcript-idle-timeout (600 s) spinner-immune · exit 124 ESC → 1.5 s window → idle PermissionRequest ESC → permission_prompt_blocked → idle SessionStart dequeue Stop / ESC stdout (NDJSON) system / init Once on SessionStart + session_id transcript lines (raw) Live stream via pread Tailer {"type":"result", ...} Turn slice on confirmed Stop (150 ms) {"type":"pong","ts":…} Immediate reply to ping interrupted / error result Ends a turn or the session is_error: false on interrupted {"type":"error","error":…} Rejects one line; no turn ends
claude-p daemon keeps one interactive Claude session open across turns. ping is answered inline; interrupt cancels a busy turn without killing the session. type:"error" frames reject one input line; type:"result" frames end a turn or the session.

One JSON object per \n-terminated line, at most 16 MiB. A final line without \n is processed at EOF. Rejected lines get a {"type":"error","error":"<reason>"} protocol frame and the session continues (see Protocol error frames).

Line Daemon response
Blank or whitespace-only Ignored silently; no frame.
Valid JSON object with an unknown or missing type, or a non-object JSON value unsupported_frame_type protocol error.
Not valid JSON invalid_json protocol error.
user frame with no non-empty text content invalid_user_message protocol error.
user frame with a non-text content block unsupported_content_block protocol error.
user frame while the queue holds 64 prompts or the text would push it past 64 MiB queue_full protocol error; the prompt is dropped.
Line over 16 MiB stdin_line_too_large protocol error; the rest of the line is discarded and later lines are read normally.

Accepted shapes (checked in order):

{"type":"user","message":{"role":"user","content":"Summarize src/main.zig."}}
{"type":"user","message":{"role":"user","content":[{"type":"text","text":"Part 1. "},{"type":"text","text":"Part 2."}]}}
{"type":"user","content":"Shorthand top-level content."}

When content is an array, all blocks with "type":"text" are concatenated in order; any other block type is rejected with unsupported_content_block. Frames are appended to prompt_queue (at most 64 prompts and 64 MiB of text, including frames sent before SessionStart) and pasted incrementally (PendingPaste) whenever the daemon is .idle. An interrupt during the paste cancels it.

{"type":"ping"}

Answered with pong as soon as the main loop reads it, in any state (waiting_for_ready, idle, or busy). Never enters prompt_queue.

interrupt — cancel a busy turn without killing the session

Section titled “interrupt — cancel a busy turn without killing the session”
{"type":"interrupt"}
  • When .busy (and not already waiting on an interrupt): writes Escape (\x1b) to the PTY and starts a 1500 ms (interrupt_window_ms) window waiting for Stop.
  • If Stop arrives within 1500 ms, the turn finishes normally with a standard result frame.
  • If 1500 ms elapses without Stop, the daemon emits a structured interrupted result frame (subtype: "interrupted", error: "interrupted", is_error: false), advances turn_start_pos, sets after_interrupt = true, and returns to .idle with the session open.
  • On the next turn after an interrupt, any stale Stop from the canceled turn is ignored (stopBelongsToTurn) until the new turn shows life (UserPromptSubmit or transcript growth), and Ctrl-U (\x15) clears any restored input before pasting the next prompt.
  • Ignored when not .busy.

Closing stdin sets stdin_eof = true. The daemon finishes the active turn and drains prompt_queue, calls session.terminate(), drains final transcript bytes, and exits 0. EOF before SessionStart with nothing queued also exits 0 without waiting for startup.

Before system/init, a failure to find or start claude writes one type:"result" frame with subtype: "error", a stage field naming the step, and exits 2:

error stage values
claude_not_found resolve_claude
spawn_failed cwd, hook_settings, hook_harness, shell_command, environment, fifo_open, pty_spawn
{"type":"result","subtype":"error","error":"claude_not_found","stage":"resolve_claude","session_id":"","result":"","is_error":true,"duration_ms":0,"duration_api_ms":0,"num_turns":0,"total_cost_usd":0,"usage":{"input_tokens":0,"output_tokens":0,"cache_read_input_tokens":0,"cache_creation_input_tokens":0},"permission_denials":[]}

Invalid options (--max-turns, --fallback-model, or another rejected print-only flag, and --output-format text or json) are refused before startup with exit 2 and no frame; library callers get error.InvalidOptions.

Emitted once when SessionStart fires:

{"type":"system","subtype":"init","session_id":"550e8400-e29b-41d4-a716-446655440000"}

Raw JSONL lines tailed from Claude’s session transcript file via stream.Tailer as claude flushes them. When launched with --resume or --continue, the tailer seeks to existing EOF on first open so historical turns are skipped.

Emitted once Stop is confirmed: the daemon confirms immediately on system/stop_hook_summary (preventedContinuation: false), supersedes immediately on continuation or a later Stop, and otherwise waits a fallback confirmation window (150 ms, or 2000 ms when --extra-hook Stop=... is set), then drains and parses the tailer for up to 30 × 25 ms (exiting early on match). Prompt delivery pastes incrementally in the event loop so ping, interrupt, and DEC replies stay responsive during large prompts. Summary fields (result, usage, num_turns, etc.) are computed from [turn_start_pos..turn_end_pos) covering only that turn, falling back to Stop’s last_assistant_message if the slice cannot be parsed. Schema matches the CLI JSON result schema.

Emitted inline in reply to each ping:

{"type":"pong","ts":1763859600123}

A rejected stdin or hook-FIFO line produces one writeProtocolError frame. It ends no turn, and the daemon keeps its state:

{"type":"error","error":"invalid_json"}
error reason Cause
invalid_json Stdin line is not valid JSON.
unsupported_frame_type Valid JSON that is not an object, or has an unknown or missing type.
invalid_user_message user frame with no non-empty text.
unsupported_content_block user frame with a non-text content block.
stdin_line_too_large Stdin line over 16 MiB.
fifo_line_too_large Hook relay line over 16 MiB.
queue_full Prompt queue already holds 64 prompts or 64 MiB.

Turn and session error frames (emitErrorResult)

Section titled “Turn and session error frames (emitErrorResult)”

type:"result" is reserved for frames that end a turn or the session. Every non-success outcome of that kind uses the result envelope with an extra "error": "<reason>" field, "result": "", and zeroed counters. Tell it from a successful turn result by the error field:

{"type":"result","subtype":"interrupted","error":"interrupted","session_id":"...","result":"","is_error":false,"duration_ms":1500,"duration_api_ms":0,"num_turns":0,"total_cost_usd":0,"usage":{"input_tokens":0,"output_tokens":0,"cache_read_input_tokens":0,"cache_creation_input_tokens":0},"permission_denials":[]}
{"type":"result","subtype":"error","error":"transcript_stalled","session_id":"...","result":"","is_error":true,"duration_ms":600100,"duration_api_ms":0,"num_turns":0,"total_cost_usd":0,"usage":{"input_tokens":0,"output_tokens":0,"cache_read_input_tokens":0,"cache_creation_input_tokens":0},"permission_denials":[]}
{"type":"result","subtype":"error_during_execution","error":"permission_prompt_blocked","message":"Bash","session_id":"...","result":"","is_error":true,"duration_ms":2300,"duration_api_ms":0,"num_turns":0,"total_cost_usd":0,"usage":{"input_tokens":0,"output_tokens":0,"cache_read_input_tokens":0,"cache_creation_input_tokens":0},"permission_denials":[]}

is_error is false only for subtype: "interrupted", and true for every other subtype.

error reason Kind What happens next
prompt_not_accepted Turn failed: claude never acknowledged the prompt after 4 injections Daemon returns to idle.
stop_failure Turn failed: StopFailure hook fired (API error). The frame keeps the transcript’s usage and num_turns, has is_error: true, and result holds the hook’s error text. Daemon returns to idle.
result_unavailable Turn ended but no assistant text could be read Daemon returns to idle.
interrupted Turn canceled by an interrupt frame Daemon returns to idle.
permission_prompt_blocked PermissionRequest hook fired while busy; the daemon sent Escape. subtype: "error_during_execution", tool name in message. Daemon returns to idle.
claude_not_found, spawn_failed Startup failure (stage field) Daemon exits 2.
unexpected_startup_dialog, prompt_truncated, child_exited Session failure Daemon exits 2.
session_start_timeout, idle_timeout, transcript_stalled Watchdog Daemon exits 124.

Every watchdog timeout flushes any remaining transcript bytes, emits one structured error result frame to stdout, and terminates the child so callers never see a silent dead pipe:

Watchdog Flag / default "error" reason Exit code
Startup deadline --timeout <sec> (300; 0 disables) session_start_timeout 124
Busy PTY silence --idle-timeout <sec> (180; 0 disables) idle_timeout 124
Busy transcript stall (spinner-immune) --transcript-idle-timeout <sec> (600; 0 disables) transcript_stalled 124

Both busy watchdogs reset on idle → busy and never trip while .idle or .waiting_for_ready. A ticking Ink spinner resets idle_timeout but does not reset transcript_stalled, which requires tailer.pos to advance.

Off by default. --debug turns it on every 60 s. CLAUDE_P_PTY_TRACE=<seconds> sets the interval (with or without --debug); 0/off/false/no turns it off:

[pty-trace] state=busy pty_bytes=48210 pty_delta=1920 last_byte_age_ms=340 turn_age_ms=60012 transcript_pos=8192
  • last_byte_age_ms is -1 before the first PTY byte arrives.
  • turn_age_ms is 0 unless the daemon is busy.