Using Gemini CLI - Headless Guide
Overview
Headless mode runs Gemini CLI non-interactively from scripts and CI. It accepts prompts via flags or stdin and returns text, JSON, or streaming JSON events—ideal for automation, logging, and tool-driven workflows.
When to Use
- Automating prompts in shell scripts or CI/CD
- Needing structured output (JSON or streaming JSON) for downstream tools
- Piping files, git diffs, or logs into Gemini
- Building dashboards that monitor model/tool activity in real time
- Not for interactive chat; use normal Gemini CLI for conversational sessions
Run a Task (minimal steps)
- Confirm model and output format if unspecified (speed:
gemini-2.5-flash; depth: gemini-3-pro-preview; formats: text, json, stream-json).
- Build the command:
- Direct prompt:
gemini -p "What is machine learning?"
- From stdin:
echo "Explain this code" | gemini
- File + prompt:
cat README.md | gemini -p "Summarize this documentation" --output-format json
- Add repo context:
gemini -p "Review repository risks" --include-directories src,docs
- Optional safety/verbosity:
-m, --model <model> (e.g., gemini-2.5-flash, gemini-3-pro-preview)
--output-format json|stream-json
--yolo or --approval-mode auto_edit to auto-approve actions (use cautiously)
--debug for verbose logging
- Capture output:
- Text:
... > result.txt
- JSON:
... --output-format json | jq '.response'
- Streaming:
... --output-format stream-json > events.jsonl
Quick Reference
| Use case |
Command pattern |
| Fast Q&A (text) |
gemini -p "question" |
| Structured output for scripts |
gemini -p "query" --output-format json |
| Stream live events |
gemini -p "task" --output-format stream-json |
| Analyze file content |
`cat file |
| Review git diff |
`git diff |
| Generate commit message |
`git diff --cached |
| API doc draft |
`cat api/routes.js |
| Batch file analysis |
`for f in src/*.py; do cat "$f" |
Output Formats
- text (default): human-readable response.
- json: structured object
{ response, stats, error? } including per-model token usage, tool stats, and file line deltas—ideal for automation pipelines.
- stream-json: newline-delimited events (
init, message, tool_use, tool_result, error, result) emitted immediately for progress UIs and event-driven scripts.
Example stream pipeline:
gemini --output-format stream-json --prompt "List files" | jq -r '.type'
Common Options
-p, --prompt: set prompt (headless trigger)
--output-format text|json|stream-json
-m, --model <model>: choose Gemini model
--include-directories dir1,dir2: add repo folders to context
--debug: verbose logging
--yolo: auto-approve actions
--approval-mode <mode>: control approvals (e.g., auto_edit)
Workflow Patterns
- Code review:
git diff | gemini -p "Review for bugs and security issues" --output-format json
- Commit message:
git diff --cached | gemini -p "Write a concise commit message" --output-format json | jq -r '.response'
- Log triage:
grep "ERROR" app.log | tail -50 | gemini -p "Find root cause and fixes" > error-analysis.txt
- Release notes:
git log --oneline v1.0.0..HEAD | gemini -p "Generate release notes" --output-format json | jq -r '.response' >> CHANGELOG.md
- Usage tracking:
result=$(gemini -p "Explain this database schema" --include-directories db --output-format json)
total_tokens=$(echo "$result" | jq -r '.stats.models // {} | to_entries | map(.value.tokens.total) | add // 0')
models_used=$(echo "$result" | jq -r '.stats.models // {} | keys | join(", ") | if . == "" then "none" else . end')
tool_calls=$(echo "$result" | jq -r '.stats.tools.totalCalls // 0')
echo "$(date): $total_tokens tokens, $tool_calls tool calls (models: $models_used)" >> usage.log
echo "$result" | jq -r '.response' > schema-docs.md
Common Mistakes
- Forgetting
--output-format json when piping to jq, causing parse errors.
- Assuming pro-level depth without setting
--model; default may be gemini-2.5-flash.
- Sending both a prompt and unrelated stdin, which muddies context.
- Using
--yolo in production scripts without safeguards—can auto-accept tool actions.
- Ignoring non-zero exit codes in CI; always fail the job on errors.
Resources
Code Review Workflow
- IMPORTANT/DO NOT SKIP: Always run the command
code-review.sh --help and read the help message before using the script.
- Ask user for model preference (
gemini-2.5-flash for speed, gemini-3-pro-preview for depth; default is gemini-3-pro-preview)
- Run the code-review script (located at
scripts/code-review.sh in the skill directory): code-review.sh [--model MODEL] [--output-format json] "<request>" [files...]
- The script automatically:
- Includes BOTH staged and unstaged git changes in the review
- Uses a structured prompt from
~/.gemini/prompts/code-review.md (or $GEMINI_PROMPTS_DIR/prompts/code-review.md)
- Pipes all context to Gemini CLI via stdin with
--prompt flag
- Uses
--output-format json if specified for structured parsing
- After completion, inform user of the review results and any issues found
- For follow-up questions, run a new Gemini command with the same context or ask specific questions
Note: The script reviews ALL changes (both staged and unstaged). Users can stage specific files first if they want to review only certain changes.
Following Up
- After every
gemini command, immediately use AskUserQuestion to confirm next steps or collect clarifications.
- For follow-up analysis, run a new
gemini command with additional context or specific questions.
- Restate the chosen model and output format when proposing follow-up actions.
Error Handling
- Stop and report failures whenever
gemini --version or a gemini command exits non-zero; request direction before retrying.
- Before you use high-impact flags (
--yolo, --approval-mode auto_edit) ask the user for permission using AskUserQuestion unless it was already given.
- When output includes warnings or partial results, summarize them and ask how to adjust using
AskUserQuestion.
- Always check for non-zero exit codes in CI/CD pipelines and fail the job on errors.
Additional References
1---2name: using-gemini-cli3description: This skill enables Claude to leverage Google Gemini CLI for intelligent code analysis, refactoring, and automated editing through non-interactive command-line prompting. It provides integration with Gemini and other LLM-based coding assistants, allowing delegation of code-based tasks to external smart models via CLI invocation.4---56# Using Gemini CLI - Headless Guide78## Overview9Headless mode runs Gemini CLI non-interactively from scripts and CI. It accepts prompts via flags or stdin and returns text, JSON, or streaming JSON events—ideal for automation, logging, and tool-driven workflows.1011## When to Use12- Automating prompts in shell scripts or CI/CD13- Needing structured output (JSON or streaming JSON) for downstream tools14- Piping files, git diffs, or logs into Gemini15- Building dashboards that monitor model/tool activity in real time16- Not for interactive chat; use normal Gemini CLI for conversational sessions1718## Run a Task (minimal steps)191. Confirm model and output format if unspecified (speed: `gemini-2.5-flash`; depth: `gemini-3-pro-preview`; formats: `text`, `json`, `stream-json`).202. Build the command:21 - Direct prompt: `gemini -p "What is machine learning?"`22 - From stdin: `echo "Explain this code" | gemini`23 - File + prompt: `cat README.md | gemini -p "Summarize this documentation" --output-format json`24 - Add repo context: `gemini -p "Review repository risks" --include-directories src,docs`253. Optional safety/verbosity:26 - `-m, --model <model>` (e.g., `gemini-2.5-flash`, `gemini-3-pro-preview`)27 - `--output-format json|stream-json`28 - `--yolo` or `--approval-mode auto_edit` to auto-approve actions (use cautiously)29 - `--debug` for verbose logging304. Capture output:31 - Text: `... > result.txt`32 - JSON: `... --output-format json | jq '.response'`33 - Streaming: `... --output-format stream-json > events.jsonl`3435## Quick Reference36| Use case | Command pattern |37| --- | --- |38| Fast Q&A (text) | `gemini -p "question"` |39| Structured output for scripts | `gemini -p "query" --output-format json` |40| Stream live events | `gemini -p "task" --output-format stream-json` |41| Analyze file content | `cat file | gemini -p "prompt"` |42| Review git diff | `git diff | gemini -p "Review these changes" --output-format json` |43| Generate commit message | `git diff --cached | gemini -p "Write a concise commit message" --output-format json` |44| API doc draft | `cat api/routes.js | gemini -p "Generate OpenAPI spec" --output-format json` |45| Batch file analysis | `for f in src/*.py; do cat "$f" | gemini -p "Find bugs" --output-format json > reports/$(basename "$f").json; done` |4647## Output Formats48- **text (default):** human-readable response.49- **json:** structured object `{ response, stats, error? }` including per-model token usage, tool stats, and file line deltas—ideal for automation pipelines.50- **stream-json:** newline-delimited events (`init`, `message`, `tool_use`, `tool_result`, `error`, `result`) emitted immediately for progress UIs and event-driven scripts.5152Example stream pipeline:53```54gemini --output-format stream-json --prompt "List files" | jq -r '.type'55```5657## Common Options58- `-p, --prompt`: set prompt (headless trigger)59- `--output-format text|json|stream-json`60- `-m, --model <model>`: choose Gemini model61- `--include-directories dir1,dir2`: add repo folders to context62- `--debug`: verbose logging63- `--yolo`: auto-approve actions64- `--approval-mode <mode>`: control approvals (e.g., `auto_edit`)6566## Workflow Patterns67- **Code review:** `git diff | gemini -p "Review for bugs and security issues" --output-format json`68- **Commit message:** `git diff --cached | gemini -p "Write a concise commit message" --output-format json | jq -r '.response'`69- **Log triage:** `grep "ERROR" app.log | tail -50 | gemini -p "Find root cause and fixes" > error-analysis.txt`70- **Release notes:** `git log --oneline v1.0.0..HEAD | gemini -p "Generate release notes" --output-format json | jq -r '.response' >> CHANGELOG.md`71- **Usage tracking:** 72```73result=$(gemini -p "Explain this database schema" --include-directories db --output-format json)74total_tokens=$(echo "$result" | jq -r '.stats.models // {} | to_entries | map(.value.tokens.total) | add // 0')75models_used=$(echo "$result" | jq -r '.stats.models // {} | keys | join(", ") | if . == "" then "none" else . end')76tool_calls=$(echo "$result" | jq -r '.stats.tools.totalCalls // 0')77echo "$(date): $total_tokens tokens, $tool_calls tool calls (models: $models_used)" >> usage.log78echo "$result" | jq -r '.response' > schema-docs.md79```8081## Common Mistakes82- Forgetting `--output-format json` when piping to `jq`, causing parse errors.83- Assuming pro-level depth without setting `--model`; default may be `gemini-2.5-flash`.84- Sending both a prompt and unrelated stdin, which muddies context.85- Using `--yolo` in production scripts without safeguards—can auto-accept tool actions.86- Ignoring non-zero exit codes in CI; always fail the job on errors.8788## Resources89- Headless mode docs: https://geminicli.com/docs/cli/headless/90- Configuration guide: https://geminicli.com/docs/get-started/configuration91- Authentication: https://geminicli.com/docs/get-started/authentication9293## Code Review Workflow940. IMPORTANT/DO NOT SKIP: Always run the command `code-review.sh --help` and read the help message before using the script.951. Ask user for model preference (`gemini-2.5-flash` for speed, `gemini-3-pro-preview` for depth; default is `gemini-3-pro-preview`)962. Run the code-review script (located at `scripts/code-review.sh` in the skill directory): `code-review.sh [--model MODEL] [--output-format json] "<request>" [files...]`973. The script automatically:98 - Includes BOTH staged and unstaged git changes in the review99 - Uses a structured prompt from `~/.gemini/prompts/code-review.md` (or `$GEMINI_PROMPTS_DIR/prompts/code-review.md`)100 - Pipes all context to Gemini CLI via stdin with `--prompt` flag101 - Uses `--output-format json` if specified for structured parsing1024. After completion, inform user of the review results and any issues found1035. For follow-up questions, run a new Gemini command with the same context or ask specific questions104105**Note**: The script reviews ALL changes (both staged and unstaged). Users can stage specific files first if they want to review only certain changes.106107## Following Up108- After every `gemini` command, immediately use `AskUserQuestion` to confirm next steps or collect clarifications.109- For follow-up analysis, run a new `gemini` command with additional context or specific questions.110- Restate the chosen model and output format when proposing follow-up actions.111112## Error Handling113- Stop and report failures whenever `gemini --version` or a `gemini` command exits non-zero; request direction before retrying.114- Before you use high-impact flags (`--yolo`, `--approval-mode auto_edit`) ask the user for permission using AskUserQuestion unless it was already given.115- When output includes warnings or partial results, summarize them and ask how to adjust using `AskUserQuestion`.116- Always check for non-zero exit codes in CI/CD pipelines and fail the job on errors.117118## Additional References119- [Gemini CLI Headless Mode Documentation](https://geminicli.com/docs/cli/headless/) - Complete guide to non-interactive execution120- [Gemini CLI Configuration Guide](https://geminicli.com/docs/get-started/configuration) - Configuration options, settings files, and environment variables121- [Gemini CLI Authentication](https://geminicli.com/docs/get-started/authentication) - Setup authentication for Gemini CLI