DIRTY STATIC / DISPATCH Tutorials27 NOV 2026 • 11 MIN READ
A concrete terminal hall with a console facing a caution yellow Gemini star on the wall and hazard stripes on the pillars
TutorialsINTELLIGENCE FOR THE MACHINE AGE

Use Gemini API CLI From Coding Agents

Google's Gemini API CLI, called gemini-api, is a stateless single binary built for agents with shell access. This tutorial installs it, sets a Gemini API key, verifies the setup with a dry run that makes no network call, adds the official agent skill or instruction block, and then runs text generation, file analysis, image generation, and a background Deep Research job from Claude Code, Codex, Cursor, Antigravity, or any shell.

Start reading ↓

Your coding agent already knows how to plan work and edit files. What it often cannot do is call Gemini for a second model, a generated image, a voiceover, or an answer buried inside a PDF. The Gemini API CLI closes that gap without adding SDK code to your project. The agent runs a shell command, Gemini does the model work, and a file path, a transcript, or an interaction ID comes back.

Google is clear about what this tool is not. It is a stateless API client, not an interactive coding agent like the separate Gemini CLI product. You keep the agent you already use. This binary is a tool that agent calls, and the same commands also work in a plain terminal, a script, and a CI pipeline. Every feature runs on the Interactions API.

This guide follows the official Gemini API CLI documentation and the project README on GitHub, checked on November 27, 2026. It claims no live run with a real key. The dry run step below is how you verify your own setup without spending a call.

What You Will Build or Learn

You will end with a coding agent that can call Gemini on demand. It will be able to generate text, create or edit images, make speech, music, and video, analyze PDFs and other files, transcribe recordings, and launch a managed agent such as Deep Research in the background while it keeps working on other tasks.

You will also learn the four design choices that make this CLI safe for agents. It can describe itself offline with --usage and --schema, so an agent need not guess flags from training data. It writes media to disk and prints only the path. It offers dry runs and structured output. And it keeps no hidden state, so each step in a longer workflow is explicit.

For related agent tooling, see our tutorial on connecting the ngrok MCP server to coding agents and our guide to batch tool calls with Claude Code Mode.

A concrete workshop machine turning folders and a PDF into an image file and a transcript scroll, with caution yellow output trays One binary in, files and answers out. The agent keeps the paths and keeps building. Illustration: Dirty Static

Before You Begin

Know the cost shape first. The CLI itself is free and open source under Apache 2.0, per the project. Calls bill to your API key under normal Gemini API pricing. A dry run makes no network call and bills nothing, which is why this tutorial leans on it for setup checks.

One more beta warning, in Google’s own framing. The docs call gemini-api an open source beta release that evolves alongside the Interactions API, and they note that breaking changes can occur between minor versions. Pin a specific release version in agent setups and CI pipelines instead of always taking the newest build.

Step 1: Install the Binary

Action: install gemini-api and put it on your PATH. On macOS or Linux, run the official install script. To pin a release, set GEMINI_VERSION to a release tag, as the docs show.

macOS or Linux · Terminal

curl -fsSL https://raw.githubusercontent.com/google-gemini/gemini-api-cli/main/scripts/install.sh | bash

On Windows, use PowerShell:

Windows · PowerShell

iwr -useb https://raw.githubusercontent.com/google-gemini/gemini-api-cli/main/scripts/install.ps1 | iex

With Go installed, you can build it instead:

Terminal

go install github.com/google-gemini/gemini-api-cli/cmd/gemini-api@latest

Expected result: the gemini-api command resolves in a new shell. Prebuilt archives from GitHub Releases are the fourth route, and the docs recommend them when you want an exact version inside a Docker image or a CI job.

Step 2: Set Your Key and Verify With a Dry Run

Action: create an API key in Google AI Studio, then make it available to your shell and to the subprocess environment your agent uses.

Terminal

export GEMINI_API_KEY="YOUR_API_KEY"

On a workstation, you can save the key instead with the configure command, which stores it in the macOS Keychain, the Linux Secret Service, or Windows Credential Manager. Add the global --no-keyring flag if you want it in ~/.config/gemini-api/config.yaml instead. Then check which credential is active and preview a request offline.

Terminal

gemini-api configure --api-key "YOUR_API_KEY"
gemini-api whoami
gemini-api generate "Confirm CLI setup in one sentence." --dry-run

Expected result: whoami shows the active configuration and where the credential came from. The dry run validates the inputs, redacts credentials, and previews the HTTP request without a network call. Remove --dry-run when you are ready for a real call. Google makes one human only point in the help text. A key must be created manually by the user in Google AI Studio, so an agent that finds no key should stop and ask you to create one.

Step 3: Teach Your Agent About the CLI

Action: install the official skill so your agent knows when to reach for gemini-api and how to use its flags, schemas, and output formats.

Terminal

npx skills add google-gemini/gemini-skills --skill gemini-api-cli

In Antigravity, the docs also offer the Gemini API plugin, which bundles this skill with the other official Gemini skills and the Docs MCP server. If your setup does not support skills, add the fallback block from the docs to AGENTS.md, CLAUDE.md, .cursorrules, or your system prompt. The block tells the agent to run gemini-api --help or gemini-api --usage to choose a command, to run gemini-api <command> --schema before building a --body payload, to preview complex calls with --dry-run -o json, to pass --stream=false -o json for a single JSON result, and to capture output paths and interaction IDs when chaining work.

Expected result: you can prompt the agent in plain words, for example to build a landing page, generate a hero illustration and a short welcome voiceover with Gemini, and start a background Deep Research job on competing onboarding flows. The agent picks the commands itself.

Step 4: Generate Text and Analyze a File

Action: run the two most common calls directly once, so you know what the agent will produce. Text generation streams to stdout by default.

Terminal

gemini-api generate "Write a one-paragraph welcome message for new users of a note-taking app."
gemini-api analyze -i ./docs/quarterly-report.pdf "Summarize the key findings in three bullets."

Expected result: the first command prints text. The second prints an answer about the file. The analyze command accepts PDFs, images, audio, video, CSV and text files, and public YouTube URLs, and you can pass -i more than once to compare files, such as before and after screenshots. Local files sent inline cap at 14 MB of raw data, or 20 MB after base64 encoding. For larger files, upload once and reuse the returned URI:

Terminal

FILE_URI=$(gemini-api files upload ./recordings/all-hands.mp4 --wait)
gemini-api analyze -i "$FILE_URI" "List action items with owners and timestamps."

The Files API deletes uploaded files after 48 hours, per the docs, so treat an upload as a working copy and not as storage.

Step 5: Generate Media and Capture the Path

Action: ask for an image and capture the path the command prints. This pattern is the heart of the design. The file lands on disk, the agent gets a short path string, and no base64 blob enters its context.

Terminal

HERO_PATH=$(gemini-api image "Friendly robot mascot in a sunlit workshop, space on left for headline" --aspect-ratio 16:9 --image-size 2K --out ./public/images/hero.jpg)
gemini-api image "Add a winter scarf and soft snowfall outside the window" -i "$HERO_PATH" --out ./public/images/hero-winter.jpg

Expected result: two JPEG files on disk and their paths in your shell. The same shape covers speech, music, and video. The tts command writes 24 kHz mono WAV, with Kore as the default voice and a --multi-speaker option for two voices. The music command writes an audio file, and video writes an MP4 and waits until it is done unless you pass --async to get an interaction ID at once. Transcription follows the media pattern too, writing md, text, json, or srt output to disk.

Step 6: Launch a Managed Agent in the Background

Action: delegate a longer research task and keep working. Pass --background and --stream=false so the command returns an interaction ID at once instead of holding a stream open.

Terminal

RESEARCH_ID=$(gemini-api agent run "Compare open-source vector index benchmarks and cite primary papers." --agent deep-research-preview-04-2026 --background --stream=false --jq '.id')
gemini-api agent status "$RESEARCH_ID" -o json

Expected result: the first command prints an ID, and the status command checks progress or fetches the finished result. Run gemini-api agent cancel <id> to stop a job. The agent command group also creates, lists, and deletes custom agent definitions, and separate command groups manage sandbox environments, stored credentials, webhooks, and scheduled triggers for managed agents.

Verification

Run these checks in order. gemini-api whoami shows an active credential and its source. A generate call with --dry-run previews a valid request and makes no network call. A real generate call prints text, and an image call writes a file whose path you can open. A background agent run returns an interaction ID, and its status call returns structured JSON when you pass -o json.

Finish with the exit code check, because scripts and agents branch on it. The docs define 0 as success, 1 as a runtime or API error, 2 as a usage or validation error, and 3 as an authentication or authorization failure. A setup that returns 3 has a key problem, not a prompt problem, and that distinction saves real debugging time.

Troubleshooting

ProblemLikely causeFix
Exit code 3 on every callNo key is set, or the key is invalidCreate a key in Google AI Studio, export GEMINI_API_KEY for the shell and the agent subprocess, or save it with the configure command, then check with gemini-api whoami
Exit code 2 on a body payloadA flag and --body set the same key, or a flag is unknownRun gemini-api <command> --schema to check valid fields and remove the duplicate key so the conflict stops failing the call
The agent invents flags that do not existThe agent is working from stale training dataHave it run gemini-api --usage for the full command tree and gemini-api <command> --help before it builds the call
A large file is rejected inlineInline inputs cap at 14 MB raw, or 20 MB after base64 encodingUpload with gemini-api files upload --wait and pass the returned files/<id> URI as the input instead
A JSON parse step breaks on a streaming commandStreaming commands need a single response object for JSON outputAdd --stream=false when you combine a streaming command with -o json or --jq, as the docs require
A call worked last month and fails after an updateThe beta CLI can change between minor versionsPin a release with GEMINI_VERSION on install, or use a prebuilt archive, and test the pinned version before you roll it out
You need live voice or video in both directionsThe CLI covers standard requests, streaming responses, and async polling onlyUse the Live API with a Google GenAI SDK for that work, as the docs direct

Next Steps

Add one workflow at a time and keep each one boring. A good second step is transcription for meeting recordings, since the output is a file your agent can quote and file away. A good third step is a standing Deep Research pattern, where the agent launches a background job at the start of a task and reads the result near the end.

The larger lesson is about shape, not brand. Agents work best with tools that describe themselves, return paths and IDs instead of blobs, and fail with codes a script can read. When a new CLI for agents gets those three things right, wiring it into Claude Code, Codex, or Cursor stops being a project and becomes a fifteen minute setup.

Facts checked against the Gemini API CLI documentation and the project README on November 27, 2026. Version notes, limits, and agent names are Google published values and can change in a beta, so confirm them in the docs before a wider rollout. This procedure was verified against those documents and is not presented as a personal test run.

Sources

■

END OF TRANSMISSION

Stay curious. Question everything.

CONTINUE THE DESCENT

MORE FROM DIRTY STATIC ↘