◉GEMINI LABJP
●2.5 ROUTE — From today, 12/1, calls to gemini-2.5-pro and 2.5-flash are rerouted to newer models automatically●CLI 0.71.1 — Gemini CLI stable is still v0.71.1, with a 0.73 nightly published on 11/30●INTRO END — Introductory pricing for 3.8, 3.7 and 3.6 Flash ends on 12/31. 30 days left●VERTEX — Developers are asking how to run Gemini CLI with Vertex AI credentials●NEON — Gemini 4 Neon became generally available on 11/18 (paid tier only)●IMAGE TEXT — A new test shows text inside an image can sway Gemini's answer●2.5 ROUTE — From today, 12/1, calls to gemini-2.5-pro and 2.5-flash are rerouted to newer models automatically●CLI 0.71.1 — Gemini CLI stable is still v0.71.1, with a 0.73 nightly published on 11/30●INTRO END — Introductory pricing for 3.8, 3.7 and 3.6 Flash ends on 12/31. 30 days left●VERTEX — Developers are asking how to run Gemini CLI with Vertex AI credentials●NEON — Gemini 4 Neon became generally available on 11/18 (paid tier only)●IMAGE TEXT — A new test shows text inside an image can sway Gemini's answer
Articles/Dev Tools
⟐ Dev Tools/2026-03-11Advanced

Wiring Gemini CLI into Your Shell and CI — Headless Runs and Session Resume

Move past interactive use of Gemini CLI: headless runs, JSON output, session resume, approval modes, and MCP. Includes the five ways headless runs break and a measured pro/flash comparison.

Gemini CLI12headlessMCP8automation55CI9

✦ Premium Article

Type gemini, press enter, and an interactive screen quietly comes to life. Most of you have probably been here already.

What genuinely helped me, though, was realizing I could call Gemini CLI as a single line inside a shell script. Handing my morning skim of build logs across four sites over to gemini -p visibly shrank the time I spent checking them.

This article focuses on Gemini CLI as a tool rather than a chat window: headless execution, JSON output, session resume, approval modes, and MCP integration. We'll walk through the spots that tend to trip people up when they actually embed it into scripts.

Start with the correct install and auth

Gemini CLI is Google's open-source terminal agent, published on npm as @google/gemini-cli. You'll want Node.js 20 or higher — ideally the 22 LTS.

# Global install
npm install -g @google/gemini-cli
 
# Try it once without a permanent install
npx @google/gemini-cli
 
# Check the version
gemini --version

There are two ways to authenticate. On first launch, gemini prompts you to sign in with a Google account, and you can start on the free tier right away. In environments that can't show an interactive prompt — CI or scripts — passing an API key via an environment variable is the reliable path.

# API key auth (for CI and automation)
export GEMINI_API_KEY="YOUR_API_KEY"

Keep the key in .env and always add it to .gitignore. Skip this and you risk pushing your key straight into a public repository.

echo 'GEMINI_API_KEY=YOUR_API_KEY' > .env
echo '.env' >> .gitignore

Separate interactive mode from headless mode

Gemini CLI's invocations split broadly into "interactive" and "headless." Just being aware of that boundary makes the right choice obvious.

GoalCommandBehavior
Think through somethinggeminiStarts the interactive REPL
Get just the answergemini -p "..."Runs one turn and exits (non-interactive)
Feed standard inputcat file | geminiProcesses piped content
Run, then keep goinggemini -i "..."Executes, then drops into interactive mode
Machine-process the resultgemini -p "..." -o jsonEmits JSON

For automation, the keys are -p (--prompt) and -o (--output-format). -p returns a single turn to stdout without entering the REPL, and adding -o json gives you structured data that tools like jq can handle.

# Pull only the critical errors out of a log
cat build.log | gemini -p "List up to 3 fatal errors from this log, each with a guessed cause"
 
# Receive the result as JSON and pass it downstream
gemini -p "Summarize README.md in one sentence" -o json | jq -r '.response'

You pick the model with -m (--model). Aliases are provided: pro for complex reasoning, flash as a speed-and-quality balance, and flash-lite as the fastest for simple tasks. For batch work like triaging many logs, leaning on the flash family tightens both cost and latency.

# Push light classification tasks to the flash family
cat access.log | gemini -m flash -p "Group requests by country and show the top 5"
✦

Thank you for reading this far.

Continue Reading

What follows includes implementation code, benchmarks, and practical content we hope you'll find useful. This site runs without ads — server and development costs are supported entirely by members like you. If it's been helpful, we'd be truly grateful for your support.

WHAT YOU'LL LEARN
✦When to use interactive vs headless mode (gemini -p / -o json), and how to decide before wiring it into CI
✦Concrete steps to keep automation safe and reproducible with session resume, GEMINI.md, and approval modes
✦A ready-to-run pre-push hook plus a headless wrapper that absorbs empty stdin, 429s, and broken JSON
Secure payment via Stripe · Cancel anytime
✦

Unlock This Article

Get full access to the rest of this article. Buy once, read anytime. This site is ad-free — your support goes directly toward keeping it running.

or
Unlock all articles with Membership →
Share

Thank You for Reading

Gemini Lab is ad-free, supported entirely by members like you. We publish practical guides daily with implementation code, benchmarks, and production-ready patterns. If you've found it useful, we'd love to have you on board.

  • ✦Copy-paste ready implementation code
  • ✦New advanced guides published daily
  • ✦$5/mo or $10 for lifetime access
View Membership →

Related Articles

⟐ Dev Tools2026-10-29
Before You Install gemini-api: It Is Not Gemini CLI, and Where I Decided It Belongs
The Gemini API CLI (gemini-api, beta) that appeared on 10/27 is not a successor to Gemini CLI. Here is where I placed it after reading the README, what to check with --dry-run before any billing, and how to pin it in CI.
⟐ Dev Tools2026-10-24
The More MCP Servers You Add, the Closer You Get to a 400 — Trimming Gemini CLI Tools Per Task
Reports say Gemini CLI can stop with a 400 once enough MCP tools are loaded. The exact limit isn't confirmed, but deciding what each task carries with includeTools makes the failure easier to isolate and to prevent.
⟐ Dev Tools2026-10-19
I Set the MCP Timeout to 10 Minutes and It Still Cuts Off at 1 — Tracing Which Layer Decides the Value
You raised an MCP server's timeout in Gemini CLI and it still ends early. A small script lists the value from every config layer, catching unit slips and extension overrides, plus the order I check things in.