Skip to contentSkip to page list

Docs

Gemini CLI Monitoring with OpenTelemetry

Monitor the Gemini CLI with OpenTelemetry. Send sessions, cost and traces to LangWatch with the LangWatch command-line tool.

On this page

Gemini CLI reports every session over OpenTelemetry. The LangWatch command-line tool connects that report to your workspace, so you can read the cost of each session, the trace of each turn, and the prompts behind both.

There are two ways to set it up:

  • langwatch gemini starts Gemini CLI and sets up the connection on the first run. Use it on a machine you work on.
  • langwatch instrument gemini writes the same setup and exits. Use it on a server, or when the telemetry must go to a team project.

Before you start

  • Install Gemini CLI.
  • Install the LangWatch command-line tool: npm install -g langwatch
  • Create an account at app.langwatch.ai, or run your own instance with npx @langwatch/server.

Run Gemini CLI through the wrapper

example.shBash
langwatch login --devicelangwatch gemini

The first run asks one question:

AnswerWhat it does
Using a Gemini subscriptionGemini CLI keeps your own plan and your own login. LangWatch receives telemetry only.
Using an API keyGemini CLI calls travel through the LangWatch AI Gateway with a virtual key. LangWatch receives telemetry and applies your budget and your policies.

LangWatch remembers the answer, so later runs do not ask again. Everything else is unchanged: langwatch gemini accepts the same flags and the same subcommands as gemini, and it returns the same exit code.

For the gateway path, ~/.gemini/settings.json must have security.auth.selectedType set to "gemini-api-key". With "gemini-oauth" selected, Gemini CLI uses its cached login and ignores the key, so the calls bypass the gateway; the wrapper prints a warning in that case. The telemetry path works with either.

Set up a machine without starting Gemini CLI

langwatch instrument gemini writes a gemini function into your shell start file (~/.zshrc, ~/.bashrc, or ~/.config/fish/config.fish), which applies the LangWatch settings to that tool and to no other command. A plain gemini run in a new terminal then sends its telemetry to LangWatch.

example.shBash
langwatch instrument gemini

Open a new terminal after the command, or read the start file again, so the function is in place.

The shell function needs zsh, bash or fish. On Windows, start Gemini CLI with langwatch gemini each time instead.

Choose where the telemetry goes

Pass one scope flag, and one only:

CommandDestination
langwatch instrument geminiYour personal workspace
langwatch instrument gemini --project <id-or-slug>A team project. See Team projects.
langwatch instrument gemini --key <ingest-key>The project that owns the key, with no login. See Headless machines and CI.
langwatch instrument gemini --personalBack to your personal workspace

The wrapper accepts the same flags: langwatch gemini --project acme-app pins the project and then starts Gemini CLI. --project and --personal are LangWatch flags; LangWatch removes them from the command line before Gemini CLI reads it.

Remove the setup

example.shBash
langwatch logout

This deletes every connection the command-line tool wrote, for every tool.

What you get in LangWatch

Open your personal page at /coding-agents, or open the project you pinned Gemini CLI to.

  • Sessions: one row per Gemini CLI session, with the context it carried and how long it worked.
  • Traces: one trace per turn, with the model, the tokens and the timing of each step, and each tool call with its details.
  • Cost: the amount per session and per turn.
  • Content: the prompt of each turn.

Your agent can read all of this back. See Explore your usage with your own agent.

Content and privacy

The setup sets GEMINI_TELEMETRY_LOG_PROMPTS=true, which is what puts the text of your prompts on the wire; without it, Gemini CLI reports the prompt as a length and no text. It also sets GEMINI_TELEMETRY_TRACES_ENABLED=true, which adds the tool call details to the spans.

Set GEMINI_TELEMETRY_LOG_PROMPTS=false in your own environment to keep prompt text off. To keep content out of storage organization-wide, use the data privacy policy. Who can open a stored session's content is covered on Privacy.

Route through the LangWatch AI Gateway

Everything above is telemetry: Gemini CLI keeps your own plan and your own login. Choosing Using an API key on the first wrapper run instead routes the model calls themselves through the LangWatch AI Gateway with a virtual key, which adds budgets and model policies on top of the same telemetry.

The wrapper starts Gemini CLI with GOOGLE_GEMINI_BASE_URL set to the gateway URL, and GEMINI_API_KEY and GOOGLE_API_KEY set to your virtual key. Gemini CLI prepends /v1beta/models/<model>:generateContent to the base URL itself, so set the base URL with no path. It sends the key on the x-goog-api-key header, which the gateway accepts along with Authorization: Bearer. To route a plain gemini run with a service virtual key, set the same variables:

example.shBash
export GOOGLE_GEMINI_BASE_URL="https://gateway.langwatch.ai"export GEMINI_API_KEY="vk-lw-..."export GOOGLE_API_KEY="vk-lw-..."gemini

The gateway forwards every /v1beta/ request to Gemini unchanged, including :streamGenerateContent, :countTokens and cachedContents, and replaces the virtual key with your Gemini credential on the way out. The model name comes from the URL path, so use the bare Gemini model name, for example gemini-2.5-flash.

Troubleshooting

For more failure modes see CLI debug.

Manual OpenTelemetry setup

Use this when you cannot install the LangWatch command-line tool. The block below mirrors the variables the setup writes for you:

example.shBash
export GEMINI_TELEMETRY_ENABLED=trueexport GEMINI_TELEMETRY_TARGET=localexport GEMINI_TELEMETRY_USE_COLLECTOR=trueexport GEMINI_TELEMETRY_TRACES_ENABLED=trueexport GEMINI_TELEMETRY_OTLP_PROTOCOL=httpexport GEMINI_TELEMETRY_OTLP_ENDPOINT=https://app.langwatch.ai/api/otelexport GEMINI_TELEMETRY_LOG_PROMPTS=trueexport OTEL_TRACES_EXPORTER=otlpexport OTEL_EXPORTER_OTLP_PROTOCOL=http/jsonexport OTEL_EXPORTER_OTLP_ENDPOINT=https://app.langwatch.ai/api/otelexport OTEL_EXPORTER_OTLP_HEADERS="Authorization=Bearer ik-lw-your-ingest-key"export OTEL_RESOURCE_ATTRIBUTES="service.name=gemini-cli"gemini

The header takes an ingest key (ik-lw-...), which can create traces and no other requests. Keep a full project API key (sk-lw-...) out of the configuration of a coding agent: it can read and change your project data, and a coding agent has no use for that.

For a self-hosted instance, replace the endpoint host with the address of your instance and keep the /api/otel path. The exporters append the per-signal paths on their own.

Documentation