Headless output formats are defined in packages/core/src/output/types.ts and accepted by the CLI as text, json, and stream-json. The default can also be set with the output.format setting.
text
Human-readable, sanitized terminal output. This is the default and the safest choice when output will be displayed directly.
json
A single pretty-printed JSON object emitted at the end of the run:
| Field | Type | Meaning |
|---|---|---|
session_id | string, optional | Session identifier. |
response | string | Final response with ANSI escapes stripped. |
stats | object | Session metrics. |
error | object, optional | { type, message, code? }. |
warnings | array | Warning strings. |
m31a -p "Summarize the failing tests" --output-format json > result.jsonstream-json
Newline-delimited JSON events. Every event has type and timestamp.
type | Fields | Meaning |
|---|---|---|
init | session_id, model | Run started. |
message | role (user/assistant), content, optional delta | Conversation content. |
tool_use | tool_name, tool_id, parameters | A tool call was proposed. |
tool_result | tool_id, status (success/error), optional output, error | Tool result. |
error | severity (warning/error), message | Diagnostic event. |
result | status (success/error), optional error, stats | Final result. |
Stats fields include total_tokens, input_tokens, output_tokens, cached, input, duration_ms, tool_calls, and models keyed by model name.
m31a -p "Run the test suite" --output-format stream-json > events.jsonl
tail -n 1 events.jsonl | jq '.type, .status'Raw output
--raw-output disables sanitization, allowing ANSI sequences from model output to reach stdout. This can be dangerous with untrusted content; --accept-raw-output-risk suppresses the warning. Prefer sanitized output in automation.
Choosing a format
- Scripts that need a single answer —
json. - Progress dashboards or long-running jobs —
stream-json. - Humans reading terminal output —
text.
See Headless mode and the CI tutorial for complete examples.