Original URL: https://getagenttools.com/blog/gemini-cli-headless/

# Gemini CLI headless mode: gemini -p, JSON output, login without a browser, and what gets denied

15 Nov 2026Marek.

- gemini-cli
- coding-agents
- agent-ops

Gemini CLI headless mode is `gemini -p "your prompt"`. Gemini CLI runs one task, prints the answer to stdout and exits. It also goes headless when stdin or stdout is not a terminal. This post is for people who script Gemini CLI or come from `claude -p`. I do not run Gemini CLI. Everything here comes from the docs and the v0.67.0 source, read on 6 November 2026.

 

```bash
gemini -p "summarize README.md"
git diff | gemini -p "write a commit message" --output-format json
gemini -p "list the TODOs" -m flash --skip-trust --output-format stream-json
```

  

- **Trigger.** `-p` or `--prompt`, or a pipe. In the source, `CI=true` as well.
- **Output.** `--output-format` takes `text`, `json` or `stream-json`.
- **Login.** A cached login, `GEMINI_API_KEY` or Vertex AI variables.
- **Approvals.** Anything that would ask is denied.
- **How I know.** Docs and code only. No run of my own.

 

The Claude Code side is in my [claude -p](https://getagenttools.com/blog/claude-p-headless/) post. The Codex side, with a real test, is in [codex exec](https://getagenttools.com/blog/codex-exec/). The opencode side is in [opencode run](https://getagenttools.com/blog/opencode-run/). This is the Gemini CLI side.

 

## What is Gemini CLI headless mode?

 

Gemini CLI headless mode is a run with no terminal UI. The [headless page](https://geminicli.com/docs/cli/headless/) says it starts "when the CLI is run in a non-TTY environment or when providing a query with the `-p` (or `--prompt`) flag". It returns text or JSON. Nothing waits for a key press, except one login prompt covered below.

 

In the source, `isHeadlessMode()` in `packages/core/src/utils/headless.ts` also returns true when `CI` or `GITHUB_ACTIONS` is `"true"`. The docs page does not say so.

 

A bare prompt is different. In a real terminal, `gemini "explain this"` opens the interactive UI. Pipe or redirect it and the same command runs headless. Piped stdin goes in front of the `-p` text. A headless run with no prompt and no stdin exits with code 42.

 

## How do I log in to Gemini CLI in headless mode?

 

Log in once in a normal terminal, or set an environment variable. The [authentication page](https://geminicli.com/docs/get-started/authentication/) says headless mode "will use your existing authentication method, if an existing authentication credential is cached". If not, it points to a Gemini API key or Vertex AI.

 

In `validateNonInterActiveAuth.ts` the auth type comes from settings first, then from the environment. `GEMINI_API_KEY` selects the Gemini API. `GOOGLE_GENAI_USE_VERTEXAI=true` selects Vertex AI, which also needs `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION`, or `GOOGLE_API_KEY`. With none set, the run exits with code 41.

 

The browser case bites on a server. With no cached login, `oauth2.ts` gives up under `NO_BROWSER`, under `CI`, on Linux without a display, or over SSH on macOS. The message is "Manual authorization is required but the current session is non-interactive."

 

## What does Gemini CLI --output-format json return?

 

One JSON object at the end of the run. The headless page lists `response`, `stats` and an optional `error`. The `JsonOutput` type in `packages/core/src/output/types.ts` has two more fields. `session_id` is the ID of the run. `warnings` is a list of strings. The docs leave both out.

 

`stream-json` prints one event per line: `init`, `message`, `tool_use`, `tool_result`, `error` and `result`. The `init` event carries `session_id` and `model`. The `result` event carries `status` and token stats per model.

 

The docs list four exit codes. The source defines more:

 

| Code | Per the docs | In the v0.67.0 source |
| --- | --- | --- |
| 0 | Success | Success |
| 1 | General error or API failure | Default for errors without a code |
| 41 | Not listed | Authentication error |
| 42 | Input error | Input error |
| 52 | Not listed | Config error, for example YOLO disabled by an admin |
| 44 | Not listed | Sandbox error |
| 53 | Turn limit exceeded | Turn limit exceeded |
| 54 | Not listed | Fatal tool error |
| 55 | Not listed | Untrusted folder in headless mode |
| 130 | Not listed | Cancelled |

 

Check the exit code first. I have not traced which stream carries the JSON of a failed run, so look at both stdout and stderr.

 

## Which tools can a headless Gemini CLI run use?

 

Read tools run. Shell and write tools are denied. In the source, `write.toml` has a `deny` rule marked `interactive = false` for `run_shell_command`, `write_file`, `replace`, `web_fetch` and `activate_skill`. It applies only to headless runs. `read_file`, `glob`, `grep_search` and `list_directory` stay `allow` in `read-only.toml`.

 

Per the [policy engine page](https://geminicli.com/docs/reference/policy-engine/), a tool denied by a global rule is "completely excluded from the model's memory". In the policy code, `getExcludedTools()` drops such tools from the list. So the model usually never sees them. A call that still reaches `scheduler.ts` gets an error back and the run goes on. My reading: a run can end with exit 0 and no file written. I have not seen it happen. Check the files either way.

 

To let a run write, pass `--approval-mode auto_edit` for edits or `--approval-mode yolo` for everything. `plan` is read-only. YOLO is in my [Gemini CLI YOLO mode](https://getagenttools.com/blog/gemini-cli-yolo-mode/) post. The box around the shell is in [Gemini CLI sandbox](https://getagenttools.com/blog/gemini-cli-sandbox/).

 

Folder trust comes first. Per the [trusted folders page](https://geminicli.com/docs/cli/trusted-folders/), an untrusted folder ends a headless run. The source exits with code 55. Pass `--skip-trust` or set `GEMINI_CLI_TRUST_WORKSPACE=true`.

 

## How do I resume a Gemini CLI session in headless mode?

 

Add `-r` to a `-p` run. `gemini -r latest -p "now fix it"` continues the newest session of this project. `-r 3` takes an index from `--list-sessions`. `-r <session_id>` takes the ID from the JSON output. In `nonInteractiveCli.ts` the old history loads before the new prompt.

 

The [CLI reference](https://geminicli.com/docs/cli/cli-reference/) shows `gemini -r "latest" "query"` without `-p`. In a terminal, by the source, that bare prompt opens the interactive UI. In a script, use `-p`.

 

Per `gemini.tsx`, `-r latest` with no saved sessions warns and starts fresh. A wrong ID exits with code 42. Sessions are stored per project, so resume from the same folder. File snapshots are a separate feature, see [Gemini CLI checkpointing](https://getagenttools.com/blog/gemini-cli-checkpointing/).

 

## How do I pick the model for a headless run?

 

Pass `-m`. In `config.ts` the model comes from `--model`, then `GEMINI_MODEL`, then `model.name` in settings. The default is `auto`.

 

The CLI reference maps `flash` to `gemini-2.5-flash` and `flash-lite` to `gemini-2.5-flash-lite`. In `models.ts` at v0.67.0, both resolve to newer models, depending on flags. To see the model a run used, read `model` in the `init` event.

 

## How does Gemini CLI headless compare with claude -p and codex exec?

 

All three give a script JSON output and a way to resume a session. They differ on the permission prompt nobody answers. Gemini CLI denies it by policy. `claude -p` denies it when no permission host is attached. `codex exec` never asks and lets the sandbox set the limits. The Claude Code and Codex columns come from my own posts.

 

| Job | `gemini -p` | `claude -p` | `codex exec` |
| --- | --- | --- | --- |
| Machine output | `-o json`, `-o stream-json` | `--output-format json`, `stream-json` | `--json` |
| Resume | `-r latest`, `-r <id>` | `--continue`, `--resume <id>` | `exec resume --last` |
| Unanswered prompt | Denied | Denied without a permission host | No prompts, the sandbox limits |
| Key in CI | `GEMINI_API_KEY` | `ANTHROPIC_API_KEY` with `--bare` | `CODEX_API_KEY` |

 

## Frequently asked questions

 

### Does Gemini CLI headless mode open a browser to log in?

 

Gemini CLI can ask first. With no cached login, `authConsent.ts` asks `[Y/n]` on stdin before it opens a browser. With `NO_BROWSER`, `CI` or SSH on macOS it fails instead. On a server, set `GEMINI_API_KEY` or Vertex AI variables.

 

### Can I pipe a file into Gemini CLI headless mode?

 

Yes. The automation tutorial shows `cat error.log | gemini -p "Explain why this failed"`. In the source, stdin goes first and the `-p` text follows.

 

### Why did my headless Gemini CLI run not write any files?

 

In the source, `write_file` has a deny rule for headless runs in the default approval mode. Pass `--approval-mode auto_edit` or add a policy rule.

 

### What does Gemini CLI exit code 53 mean?

 

The run hit `model.maxSessionTurns`. The default is `-1`, unlimited. The message starts "Reached max session turns for this session."

 

### Did you test this?

 

No. Gemini CLI is not installed on my machine. I read the docs and the v0.67.0 source on 6 November 2026.

 

## The verdict

 

Gemini CLI headless mode is a usable script entry point. Set the key in the environment. Pass `--skip-trust` only in a folder you trust. Check the exit code and the files. By my reading of the code, a denied write can still end in exit 0.

 

Sources, read on 6 November 2026: the Gemini CLI [headless reference](https://geminicli.com/docs/cli/headless/), [automation tutorial](https://geminicli.com/docs/cli/tutorials/automation/), [authentication](https://geminicli.com/docs/get-started/authentication/), [CLI reference](https://geminicli.com/docs/cli/cli-reference/), [trusted folders](https://geminicli.com/docs/cli/trusted-folders/), [session management](https://geminicli.com/docs/cli/session-management/) and [policy engine](https://geminicli.com/docs/reference/policy-engine/) pages. Code from [google-gemini/gemini-cli](https://github.com/google-gemini/gemini-cli) at tag v0.67.0, mainly `gemini.tsx`, `nonInteractiveCli.ts`, `headless.ts`, `oauth2.ts`, `policy-engine.ts` and the default policy files.

Put this into practice

[See the tool list](https://getagenttools.com/tools/)[Train your team](https://getagenttools.com/#training)
