Gemini API CLI: A Practical Guide for Coding Agents
Gemini API CLI (gemini-api) is Google's experimental stateless client for calling Gemini from coding agents, scripts, and CI. This guide explains how it differs from Gemini CLI and how to use its agent-safe flags.
View 2 referencesAllow the functional cookie to save your likes. Privacy details
What is Gemini API CLI?
Gemini API CLI is an experimental, stateless command-line client named gemini-api from Google's google-gemini/gemini-api-cli repository. It gives coding agents, shell scripts, and CI jobs direct commands for Gemini models, media generation, file analysis, transcription, and managed agents. Each invocation runs independently. The caller keeps the context and decides the next step.
This matters because Gemini API CLI is built for programs, not for a person chatting in a terminal. The official README describes it as a headless interface for agents with shell access. Media results are written to files, and only the local path is printed to stdout, so large binary payloads do not flood an agent's context window.
Gemini API CLI vs Gemini CLI: which one do you need?
The short answer is that Gemini API CLI is a stateless API client, while Gemini CLI is an interactive terminal agent. Google's Gemini CLI repository now includes a comparison table that explicitly separates the two projects. Choosing the wrong one is the most common setup mistake.
| Tool | Command | Best for | State |
|---|---|---|---|
| Gemini API CLI | gemini-api |
Giving any shell-capable agent, script, or CI job direct Gemini API commands | Stateless; caller manages IDs and context |
| Gemini CLI | gemini |
Interactive terminal work inside a coding harness | Persistent sessions and local project context |
| Antigravity CLI | agy |
Interactive multi-agent coding, Gemini 4 Argon and newer | Agent-managed sessions |
If you want Claude Code, Codex, Cursor, or your own script to generate an image, analyze a PDF, or start a background research task, Gemini API CLI is the intended tool. If you want to sit in a terminal and ask an agent to refactor a repository, use an interactive harness instead.
How do you install Gemini API CLI?
Installation is short, but the beta status is important: the README recommends pinning a release in CI because breaking changes may occur between versions. The steps below reflect the README fetched on 30 November 2026. Use the method your team can reproduce, and verify the binary with gemini-api version before adding it to automation.
- Install the binary. On macOS or Linux, run the official install script:
curl -fsSL https://raw.githubusercontent.com/google-gemini/gemini-api-cli/main/scripts/install.sh | bash
- Or use Go. The
mainbranch currently requires Go 1.26.10 or later; Go toolchain selection can fetch it automatically:
go install github.com/google-gemini/gemini-api-cli/cmd/gemini-api@latest
- Set a key. Use an environment variable for one-off or CI runs, or persist it through
gemini-api configure:
export GEMINI_API_KEY="YOUR_API_KEY"
gemini-api whoami
gemini-api generate "Hello from my agent" --dry-run
The --dry-run call validates the request and prints a preview without a network call or keychain read. It is the safest first check when wiring Gemini API CLI into an automated workflow.
What can Gemini API CLI actually do?
Gemini API CLI covers four groups of work: create, understand, manage, and advanced automation. The README lists short commands for text and multimodal generation, image and video generation, text-to-speech, music, file analysis, transcription, and managed agents. Command help is embedded in the binary, so an agent can discover current syntax with --usage instead of relying on stale training data.
| Task group | Example command | What it is for |
|---|---|---|
| Create | gemini-api generate, image, tts, music, video |
Text, image, audio, music and video output |
| Understand | gemini-api analyze, transcribe |
Read PDFs, images, audio, video, CSV, text and YouTube URLs |
| Manage | gemini-api files, models, agent, configure |
Upload files, inspect models, manage interactions and keys |
| Advanced | gemini-api environments, webhooks, triggers |
Sandboxes, callbacks and scheduled agent runs |
For example, gemini-api analyze --input product-brief.pdf "What should we build first, and why?" > plan.md writes a plan file, while gemini-api transcribe --input interview.mp4 --format srt --out interview.srt produces subtitles. Gemini API CLI returns paths and IDs so the calling agent can continue the workflow.
How do you make Gemini API CLI safe for agents?
Gemini API CLI adds machine-readable discovery and validation flags because agents cannot rely on interactive prompts or model memory for API syntax. The four most useful flags are --usage, --schema, --dry-run, and --agent-mode, all documented in the README. In practice, inspect the command tree, check the schema, preview the call, and only then execute it with structured output.
--usageprints the full command tree, flags, defaults, environment variables and config keys as KDL, directly from the binary.--schemaprints the bundled JSON Schema for commands that accept a request body, for examplegemini-api image --schema > image-schema.json.--dry-runvalidates flags and payloads, previews the request with credentials redacted, and makes no network call.--agent-mode(orGEMINI_CLI_AGENT_MODE=1) puts structured JSON errors on stderr and defaults stdout to compact TOON output.--no-interactivedisables prompts and TUI forms;--output-format jsonselects JSON explicitly.
Exit codes are also part of the contract: 0 means success, 1 runtime or API failure, 2 invalid usage or validation, and 3 authentication or authorization failure. A wrapper script can branch on those codes without parsing prose. That is a practical reason to prefer Gemini API CLI over ad-hoc REST snippets inside an agent.
How does Gemini API CLI handle big files and transcripts?
Gemini API CLI writes generated media to disk and prints only the path, with file extensions chosen from the returned MIME type. For large inputs, the README recommends uploading once with files upload --wait and reusing the returned files/<id> URI.
FILE_URI=$(gemini-api files upload interview.mp4 --wait --output-format json --jq '.name')
gemini-api analyze --input "$FILE_URI" "List the key action items with timestamps"
Uploaded files are documented with a 48-hour retention window. Gemini API CLI itself stores no local conversation state; the server-side interaction and file records manage their own retention, and your workflow keeps only the IDs it needs. Use "store": false in a custom body when you do not want a stored server-side interaction.
Can Gemini API CLI run background agents?
Yes. Gemini API CLI exposes managed agent interactions through agent run, status, and cancel. The --background flag returns an interaction ID immediately instead of holding a server-sent event stream open. That pattern keeps long research or generation jobs from blocking the host agent while leaving every decision under the caller's control.
INTERACTION_ID=$(gemini-api agent run "Research battery recycling approaches and cite sources" \
--agent deep-research-preview-04-2026 \
--background \
--jq '.id')
gemini-api agent status "$INTERACTION_ID" --stream=false --output-format json
This is the core division of labor: the host agent keeps planning, while Gemini API CLI starts a long-running Gemini-side task and hands back an ID. Multi-turn work uses previous_interaction_id inside a --body payload, so Gemini API CLI stays stateless even across related calls.
What are the limits of Gemini API CLI?
Gemini API CLI is beta software and the README warns that breaking changes may happen between releases. It is also an API client, not a coding harness: it will not manage your repository, checkpoint your edits, or decide your task plan.
The software is Apache 2.0 licensed; other repository materials are CC BY 4.0. The project states that it does not accept pull requests. Model availability, request pricing, and rate limits come from the Gemini API and your account, not from the CLI itself, so verify them in Google AI Studio before promising anything to a team.
If your workflow needs persistent chat, local file editing, or a terminal agent you talk to directly, Gemini API CLI is the wrong layer. Pair it with an agent that owns the session, and keep Gemini API CLI as the command surface for specific model calls.
Frequently asked questions about Gemini API CLI
These questions cover the practical checks people make before adopting Gemini API CLI: naming, state, CI safety, supported agents, and a low-risk first run. The answers are drawn from the official READMEs fetched on 30 November 2026 and stay within what those documents actually state.
Is Gemini API CLI the same as Gemini CLI?
No. Gemini API CLI is the stateless gemini-api client for scripts and agents. Gemini CLI is the gemini interactive terminal agent. Google's own comparison table separates them by audience and state model, so check the command name before following a tutorial.
Does Gemini API CLI remember my conversation?
No. Gemini API CLI stores no local conversation state. Each call is independent; your agent supplies context, chooses the model, and decides the next step. To continue a thread, pass previous_interaction_id in a --body payload from the earlier response.
Can I use Gemini API CLI in CI?
Yes, with care. The README explicitly targets CI jobs, but it also recommends pinning a release because beta versions may introduce breaking changes. Store GEMINI_API_KEY as a secret, use --dry-run during validation, and branch on documented exit codes.
Which agent integrations are listed?
The README names Codex, Claude Code, Antigravity, OpenClaw, Hermes, Gemini CLI and Cursor, plus any agent with shell access. Gemini API CLI also ships gemini-api install-skill to install an embedded skill into selected agents.
Where should I start?
Start with gemini-api --usage, gemini-api whoami, and a gemini-api generate "Hello" --dry-run call. Those three commands tell you what the installed binary supports, which credentials are active, and whether your request is formed correctly, without changing any files.
Bottom line
Gemini API CLI is worth adopting when an agent or pipeline needs repeatable Gemini calls without carrying Base64 media, interactive prompts, or guessed API parameters through the conversation. Keep it stateless, pin the release, and let the surrounding agent own context and decisions.