NeMo-Relay icon indicating copy to clipboard operation
NeMo-Relay copied to clipboard

[Bug]: gen_ai projection omits Gemini thinking tokens from gen_ai.usage.output_tokens

Open jyotir07 opened this issue 2 weeks ago • 3 comments

Affected area

Observability or exporters

Current behavior

The Gemini Generate Content codec stores thoughtsTokenCount separately as api_specific.thoughts_tokens, while gen_ai.usage.output_tokens is populated only from the normalized usage.completion_tokens value (candidatesTokenCount).

As a result, Gemini thinking tokens are excluded from the OpenTelemetry output token count.

For example, with:

  • prompt tokens: 10
  • candidate tokens: 5
  • thinking tokens: 20
  • total tokens: 35

The current projection reports:

  • gen_ai.usage.input_tokens: 10
  • gen_ai.usage.output_tokens: 5
  • gen_ai.usage.reasoning.output_tokens: 20

This means the output token count does not include the reasoning tokens.

Expected behavior

According to the OpenTelemetry GenAI semantic conventions, gen_ai.usage.output_tokens should include reasoning tokens, and gen_ai.usage.reasoning.output_tokens should be included in that total.

For the example above, the expected projection is:

  • gen_ai.usage.input_tokens: 10
  • gen_ai.usage.output_tokens: 25
  • gen_ai.usage.reasoning.output_tokens: 20

The canonical normalized usage fields can remain unchanged if this adjustment is made specifically in the GenAI OpenTelemetry projection.

Steps to reproduce

  1. Check out the NeMo Relay repository.

  2. Add or run a unit test in crates/observability/tests/unit/otel_projection_tests.rs.

  3. Construct an AnnotatedLlmResponse with:

    • completion_tokens: Some(5)
    • prompt_tokens: Some(10)
    • total_tokens: Some(35)
    • Gemini thoughts_tokens: Some(20) in api_specific.
  4. Run:

    cargo test -j 2 -p nemo-relay-observability gemini_thinking

  5. Observe that gen_ai.usage.reasoning.output_tokens is correctly reported as 20, but gen_ai.usage.output_tokens is reported as 5 instead of the expected 25.

The regression test currently fails with this exact mismatch.

Environment

  • NeMo Relay version or commit: de20c6c
  • OS and architecture: Windows 11, x86_64
  • Rust version: 1.96.1
  • Python version: N/A
  • Node.js version: N/A
  • Go version: N/A
  • Install/build command: cargo test -j 2 -p nemo-relay-observability gemini_thinking

The regression test was run locally on Windows 11 and failed with the expected mismatch: left: Some("5") right: Some("25")

Impact

This affects users relying on NeMo Relay's OpenTelemetry GenAI metrics for monitoring and analyzing LLM token usage.

For Gemini requests that use thinking tokens, gen_ai.usage.output_tokens underreports the actual output token count.

This can lead to inconsistent token usage metrics across providers, inaccurate dashboards, and incorrect downstream analysis for systems that rely on the standard OpenTelemetry output token attribute.

The reasoning token count is available separately, so consumers can currently work around this by adding gen_ai.usage.reasoning.output_tokens to gen_ai.usage.output_tokens for affected Gemini requests.

jyotir07 avatar Nov 17 '26 19:11 jyotir07

I'd be happy to work on a fix for this. Could someone from the maintainers confirm whether the expected OTel behaviour described above is correct, particularly whether this should be handled at the projection layer while keeping canonical usage fields unchanged?

Once confirmed, I can put together a PR.

jyotir07 avatar Nov 17 '26 19:11 jyotir07

I'd be happy to work on a fix for this. Could someone from the maintainers confirm whether the expected OTel behaviour described above is correct, particularly whether this should be handled at the projection layer while keeping canonical usage fields unchanged?

Once confirmed, I can put together a PR.

@jyotir07 confirmed, thanks. the projection should add Gemini thoughts_tokens to completion_tokens while leaving canonical usage unchanged. keep this Gemini-specific to avoid double-counting other providers. a PR is welcome.

please target release/0.11.

greysonlalonde avatar Nov 17 '26 21:11 greysonlalonde

Thanks for confirming! I'll keep canonical usage unchanged, and target release/0.11. I'll put together a PR shortly.

jyotir07 avatar Nov 18 '26 05:11 jyotir07