google-gemini / google-gemini/gemini-cli

Missing usageMetadata is recorded as all-zero, indistinguishable from a real zero-token response

Open Beginner friendly
#28,683 1 comment 0 reactions 0 assignees View on GitHub
area/enterprise status/need-triage
Dominant language
TypeScript
Stars
107k
Forks
14.6k
Avg merge
2d 3h
Merged PRs (30d)
45

Description

### What happened?

When a provider response omits `usageMetadata`, the `ApiResponseEvent` constructor in `packages/core/src/telemetry/types.ts:688-695` fills all token fields with `?? 0`. The resulting zero-valued usage record is indistinguishable from a genuine zero-token response.

These zeros propagate through `logApiResponse` (`loggers.ts:307`) to `uiTelemetryService.addEvent`, so user-visible `/stats` counts the request as a real zero-token response. The same structured log records and Clearcut events carry the zeros downstream.

This conflicts with the [OpenTelemetry GenAI metrics specification](https://github.com/open-telemetry/semantic-conventions-genai/blob/main/docs/gen-ai/gen-ai-metrics.md), which states that when token counts cannot be obtained, instrumentation must not report the usage metric.

### What did you expect to happen?

When `usageMetadata` is absent, the telemetry event should either omit the usage fields entirely or include a `usage_source: "missing"` flag so downstream consumers can distinguish "no data" from "zero tokens used." The existing guard in `geminiChat.ts:1467` (`if (bufferedUsageMetadata)`) provides precedent for the skip-when-absent pattern.

### Client information

Source-level bug verified on `main` at `ac42fb0`. Constructing `ApiResponseEvent` with `usage_data=undefined` produces all-zero usage fields with no distinguishing flag. Platform: macOS.

### Anything else we need to know?

Severity: observability hygiene (no direct cost impact). Production frequency is unknown. The fix is to skip recording usage when `usage_data` is undefined, mirroring the existing guard in `geminiChat.ts:1467`.

Contributor guide

Open the contributing guide

Research direction

Start in packages/core/src/telemetry/types.ts:688-695 and trace the event through loggers.ts:307 to uiTelemetryService.addEvent. Compare the missing-data behavior with the guard in geminiChat.ts:1467. Done means an absent usageMetadata value no longer records indistinguishable zero-token usage in user-visible stats, structured logs, or Clearcut events.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
observability
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
78/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.