Skip to main content
This guide is for developers running Atomic in shell scripts, CI pipelines, and batch jobs that need structured event output. By the end of this page, you can run one or more prompts with 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: jq installed 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:
To run multiple prompts in sequence within the same session, pass them as separate positional arguments:
Confirm that stdout prints one JSON object per line ending with {"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 @file attachments: Like Print mode, JSON mode accepts piped stdin (git diff | atomic --mode json "Review this diff") and @file or 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 through atomic.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 set ATOMIC_STORAGE or ATOMIC_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:
When resuming a session whose worktree blueprint build is running in another process, a blueprint_build_waiting event can also arrive before the session header. Session events follow as the turn runs:
Unlike RPC mode, JSON mode doesn’t add 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 message property (unlike the in-memory AgentSessionEvent delivered to SDK subscribers). Read the final message from message_end.
  • Atomic strips assistantMessageEvent.partial.
  • Atomic omits parentToolCallId on message_update lines. Each line carries only type, usage, assistantMessageEvent, and optional endTurn.
  • When assistantMessageEvent.type is "toolcall_start", Atomic injects id and toolName directly on assistantMessageEvent.
  • Top-level usage carries the latest cumulative provider-reported token usage, which can stay zero until completion if the provider reports usage only at the end.
  • Top-level endTurn appears only when the provider reported an explicit end-of-turn signal (AssistantMessage.endTurn, such as OpenAI Codex end_turn).
Atomic treats a non-assistant message on 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 emits session_storage_reconnect with state: "waiting". After the write succeeds following a reported wait, Atomic emits state: "recovered":
In --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 outside AgentSessionEvent:
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.form is "cleared" when Atomic cleared the context window and "compacted" when a compactor model or extension replaced it. When willRetry is true, Atomic retries the interrupted turn automatically, for example after context overflow. If overflow recovery is exhausted, unresolvedOverflow: true and errorMessage are set; check result and errorMessage independently 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 no compaction_usage when clearing.
  • agent_end versus agent_settled: agent_end finishes one low-level agent loop while automatic retries (retryPending: true), compaction recovery, or queued messages might still continue. Wait for agent_settled to track full turn-chain completion.
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:
The command prints each text block from the completed assistant message, for example:
To stream text deltas as they arrive, pass --unbuffered to jq:
Confirm that text appears incrementally as the model streams tokens instead of waiting for process exit.

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.