atomic --mode json, parse the emitted newline-delimited JSON (NDJSON) events, filter output with jq, and handle exit codes reliably.
If you only need plain text output, see Non-interactive mode. If you need to send follow-up commands to a live process or embed Atomic in Node.js, compare RPC mode and the TypeScript SDK on Choose an integration mode.
Before you begin
Before you run the commands on this page, confirm that you have:- The Atomic CLI installed (see Installation).
- At least one model provider configured and signed in (see Authentication).
- Optional:
jqinstalled if you want to run the filtering examples.
Run a JSON event stream
To run a prompt and emit every session event as one JSON object per line on stdout, run:{"type":"agent_settled","aborted":false} and the command exits with status 0.
Pass input and isolate session storage
JSON mode supports the following input and isolation options:- Piped stdin and
@fileattachments: Like Print mode, JSON mode accepts piped stdin (git diff | atomic --mode json "Review this diff") and@fileor image arguments. Atomic prepends piped stdin ahead of positional prompt text. - Extension commands as prompts: Positional prompts can invoke extension commands such as
"/mycommand". If a command queues turns throughatomic.sendMessage(), JSON mode waits for those queued turns to settle before running the next prompt or exiting. - Per-process session storage isolation: Pass
--storage <json>,--storage-file <path>, or--session-store <path>(with optional--session-prefix <prefix>), or setATOMIC_STORAGEorATOMIC_SESSIONS_STORAGE, to isolate CI or script runs without editing saved settings. See Selecting storage per process.
If a saved default
provider/model names an unsupported provider or a session storage override fails with StorageConfigError, JSON mode writes the diagnostic to stderr and exits with code 1. Stdout can contain loading events, but not a model reply. Supported providers with an unknown model or missing credentials fall back through normal model selection.Output format
Each line on stdout is one JSON object terminated by\n. Loading events (session_load_*) arrive before the session header, so match type: "session" instead of assuming the header is the first line.
With --no-session, the session header describes the in-memory session:
blueprint_build_waiting event can also arrive before the session header.
Session events follow as the turn runs:
entryId or seq fields to message_end.
Wire format for message_update
On the wire, message_update omits cumulative message snapshots to keep streaming lines compact:
- Atomic strips the top-level
messageproperty (unlike the in-memoryAgentSessionEventdelivered to SDK subscribers). Read the final message frommessage_end. - Atomic strips
assistantMessageEvent.partial. - Atomic omits
parentToolCallIdonmessage_updatelines. Each line carries onlytype,usage,assistantMessageEvent, and optionalendTurn. - When
assistantMessageEvent.typeis"toolcall_start", Atomic injectsidandtoolNamedirectly onassistantMessageEvent. - Top-level
usagecarries the latest cumulative provider-reported token usage, which can stay zero until completion if the provider reports usage only at the end. - Top-level
endTurnappears only when the provider reported an explicit end-of-turn signal (AssistantMessage.endTurn, such as OpenAI Codexend_turn).
message_update as a protocol violation and throws an error instead of emitting fabricated usage.
Event reference
Use the following sections to look up every event type emitted in JSON mode.Session and agent events
The following table lists the session and agent events emitted on stdout:
Final assistant messages and tool results include optional
durationMs timing. On tool_execution_end, durationMs measures how long execute() ran with a monotonic clock; Atomic omits it when the tool didn’t run.
PostgreSQL session storage reconnection
When PostgreSQL stores your sessions and a connection drops or refuses writes, Atomic holds the session’s ordered write queue until PostgreSQL reconnects. After 10 seconds (and every 60 seconds after that), Atomic emitssession_storage_reconnect with state: "waiting". After the write succeeds following a reported wait, Atomic emits state: "recovered":
--mode json, Atomic also writes a human-readable Warning: or Info: line to stderr while keeping stdout strictly JSON Lines (JSONL).
Blueprint build events
When resuming a session whose worktree blueprint build runs in the background (see Start the first turn while the build runs), the CLI runner emits two events outsideAgentSessionEvent:
blueprint_build_waiting can arrive before the session header when waiting on another process’s build of the same worktree. notices is empty when the build completes cleanly (in -p text mode, those warnings go to stderr as Warning: ...).
Compaction, retries, and turn completion
Keep the following lifecycle details in mind when parsing event streams:- Automatic compaction:
compaction_end.result.formis"cleared"when Atomic cleared the context window and"compacted"when a compactor model or extension replaced it. WhenwillRetryistrue, Atomic retries the interrupted turn automatically, for example after context overflow. If overflow recovery is exhausted,unresolvedOverflow: trueanderrorMessageare set; checkresultanderrorMessageindependently because both can appear when a saved boundary still exceeded the provider’s input limit. - Branch summaries versus compaction: Branch summaries use the retry policy and emit
summarization_retry_*events. Compaction doesn’t retry a failing compactor call; it clears context instead and emits nocompaction_usagewhen clearing. agent_endversusagent_settled:agent_endfinishes one low-level agent loop while automatic retries (retryPending: true), compaction recovery, or queued messages might still continue. Wait foragent_settledto track full turn-chain completion.
TypeScript AgentSessionEvent union (SDK reference)
TypeScript AgentSessionEvent union (SDK reference)
In-memory SDK events follow
AgentSessionEvent exported from @bastani/atomic. On the --mode json wire, message_update is rewritten to { type: "message_update", usage, assistantMessageEvent, endTurn? } (stripping message, assistantMessageEvent.partial, and parentToolCallId):Message types
Base messages come from@bastani/atomic-ai: UserMessage, AssistantMessage, and ToolResultMessage.
Atomic extends AgentMessage with three additional roles (see RPC protocol types and Session format for full JSON shapes):
BashExecutionMessage(role: "bashExecution")CustomMessage(role: "custom")BranchSummaryMessage(role: "branchSummary")
Exit codes and signals
The following table lists the exit codes JSON mode returns:
A second termination signal forces immediate exit. JSON mode waits for in-progress background worktree blueprint builds to settle before exiting.
Filter events with jq
To print the final assistant text after the run finishes, pipe stdout into jq:
--unbuffered to jq:
Troubleshooting
Use the following table to resolve common JSON stream issues:Next steps
Drive Atomic over RPC
Send follow-up prompts, cancel turns, and handle extension dialogs over a long-lived process.
Use the TypeScript SDK
Embed Atomic directly in Node.js with typed in-memory
AgentSessionEvent subscriptions.Non-interactive mode
Capture plain-text responses with
atomic -p in shell scripts.Choose an integration mode
Compare Print mode, JSON event streams, RPC mode, and the TypeScript SDK.