2026-02-13 14:09:17 -08:00
|
|
|
# Headless mode reference
|
2025-09-17 04:01:15 +09:00
|
|
|
|
2026-02-13 14:09:17 -08:00
|
|
|
Headless mode provides a programmatic interface to Gemini CLI, returning
|
|
|
|
|
structured text or JSON output without an interactive terminal UI.
|
2025-09-17 04:01:15 +09:00
|
|
|
|
2026-02-13 14:09:17 -08:00
|
|
|
## Technical reference
|
2025-09-17 04:01:15 +09:00
|
|
|
|
2026-02-13 14:09:17 -08:00
|
|
|
Headless mode is triggered when the CLI is run in a non-TTY environment or when
|
2026-03-10 01:43:14 +05:30
|
|
|
providing a query with the `-p` (or `--prompt`) flag.
|
2025-09-17 04:01:15 +09:00
|
|
|
|
2026-02-13 14:09:17 -08:00
|
|
|
### Output formats
|
2025-09-17 04:01:15 +09:00
|
|
|
|
2026-02-13 14:09:17 -08:00
|
|
|
You can specify the output format using the `--output-format` flag.
|
2025-09-17 04:01:15 +09:00
|
|
|
|
2026-02-13 14:09:17 -08:00
|
|
|
#### JSON output
|
2025-09-17 04:01:15 +09:00
|
|
|
|
2026-02-13 14:09:17 -08:00
|
|
|
Returns a single JSON object containing the response and usage statistics.
|
2025-09-17 04:01:15 +09:00
|
|
|
|
2026-02-13 14:09:17 -08:00
|
|
|
- **Schema:**
|
|
|
|
|
- `response`: (string) The model's final answer.
|
|
|
|
|
- `stats`: (object) Token usage and API latency metrics.
|
|
|
|
|
- `error`: (object, optional) Error details if the request failed.
|
2025-09-17 04:01:15 +09:00
|
|
|
|
2026-02-13 14:09:17 -08:00
|
|
|
#### Streaming JSON output
|
2025-09-17 04:01:15 +09:00
|
|
|
|
2026-02-13 14:09:17 -08:00
|
|
|
Returns a stream of newline-delimited JSON (JSONL) events.
|
2025-09-17 04:01:15 +09:00
|
|
|
|
2026-02-13 14:09:17 -08:00
|
|
|
- **Event types:**
|
|
|
|
|
- `init`: Session metadata (session ID, model).
|
|
|
|
|
- `message`: User and assistant message chunks.
|
|
|
|
|
- `tool_use`: Tool call requests with arguments.
|
|
|
|
|
- `tool_result`: Output from executed tools.
|
|
|
|
|
- `error`: Non-fatal warnings and system errors.
|
2026-03-10 12:29:18 -07:00
|
|
|
- `result`: Final outcome with aggregated statistics and per-model token usage
|
|
|
|
|
breakdowns.
|
2025-09-17 04:01:15 +09:00
|
|
|
|
2026-02-13 14:09:17 -08:00
|
|
|
## Exit codes
|
2025-09-17 04:01:15 +09:00
|
|
|
|
2026-02-13 14:09:17 -08:00
|
|
|
The CLI returns standard exit codes to indicate the result of the headless
|
|
|
|
|
execution:
|
2025-09-17 04:01:15 +09:00
|
|
|
|
2026-02-13 14:09:17 -08:00
|
|
|
- `0`: Success.
|
|
|
|
|
- `1`: General error or API failure.
|
|
|
|
|
- `42`: Input error (invalid prompt or arguments).
|
|
|
|
|
- `53`: Turn limit exceeded.
|
2025-09-17 04:01:15 +09:00
|
|
|
|
2026-02-13 14:09:17 -08:00
|
|
|
## Next steps
|
2025-09-17 04:01:15 +09:00
|
|
|
|
2026-02-13 14:09:17 -08:00
|
|
|
- Follow the [Automation tutorial](./tutorials/automation.md) for practical
|
|
|
|
|
scripting examples.
|
|
|
|
|
- See the [CLI reference](./cli-reference.md) for all available flags.
|