Skip to content

fix(google): normalize token usage extraction across ADK and GenAI instrumentation - #1465

Open
himanshu-xyz-1 wants to merge 1 commit into
AgentOps-AI:mainfrom
himanshu-xyz-1:fix/adk-gemini-token-usage-normalization
Open

himanshu-xyz-1 wants to merge 1 commit into
AgentOps-AI:mainfrom
himanshu-xyz-1:fix/adk-gemini-token-usage-normalization

Conversation

@himanshu-xyz-1

Copy link
Copy Markdown

Problem

Closes #1418.

Google ADK and Gemini API telemetry payloads emit token counts using varying attribute keys (input_tokens/output_tokens, input_token_count/output_token_count, or prompt_token_count/candidates_token_count).

When Google ADK emits gen_ai.usage.input_tokens and gen_ai.usage.output_tokens, AgentOps failed to extract the usage counts into SpanAttributes.LLM_USAGE_PROMPT_TOKENS and SpanAttributes.LLM_USAGE_COMPLETION_TOKENS. As a result, ADK Gemini spans recorded 0 tokens and $0.00 cost in trace metrics.

Solution

  • Updated _extract_llm_attributes in agentops/instrumentation/agentic/google_adk/patch.py to normalize prompt/input and completion/output token keys and calculate total_token_count if absent.
  • Updated _set_response_attributes in agentops/instrumentation/providers/google_genai/attributes/model.py to handle dictionary and object usage metadata variants consistently.
  • Added unit tests in tests/unit/instrumentation/test_google_token_usage.py covering token normalization across both ADK and GenAI instrumentation.

Testing

  • Ran unit tests: uv run pytest tests/unit/instrumentation/test_google_token_usage.py (4/4 passed).
  • Verified full instrumentation test suite: uv run pytest tests/unit/instrumentation/ (142/142 passed).

…strumentation

Google ADK and Gemini API telemetry emit token counts using varied attribute naming
(e.g., input_tokens/output_tokens or prompt_token_count/candidates_token_count).
When ADK emits input_tokens/output_tokens, spans missed token usage attributes,
causing 0 token count / cost aggregation in trace metrics.

Updated _extract_llm_attributes in google_adk and _set_response_attributes in
google_genai to normalize input/output token counts and compute total_tokens when
absent. Added unit tests verifying token usage normalization.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant