adk-python icon indicating copy to clipboard operation
adk-python copied to clipboard

Live Mode is missing usage_metadata and telemetry tracing

Open emilmilow opened this issue 1 year ago • 7 comments

Describe the bug When using ADK with Gemini Live API, the usage_metadata field is always None in Event objects, making it impossible to track token usage for cost monitoring and observability. Additionally, telemetry tracing doesn't work properly for live events, with token usage data being lost instead of creating individual spans for each event.

To Reproduce Steps to reproduce the behavior:

  1. Install google-adk
  2. Create a voice agent using gemini-live-2.5-flash model:
    from google.adk.agents import Agent
    agent = Agent(
        name="voice_assistant_agent",
        model="gemini-live-2.5-flash", # or any other live model
        tools=[some_tool]
    )
    
  3. Run the agent and process live events:
    async for event in live_events:
        print(f"Usage metadata: {event.usage_metadata}")  # Always None
    
  4. CheckOpenTelemetry traces for token usage
  5. Observe that token usage is not tracked in telemetry spans

Expected behavior

  • event.usage_metadata should contain actual token usage data from the Gemini Live API response
  • Each live event should create a separate OpenTelemetry span with token usage metrics (gen_ai.usage.input_tokens, gen_ai.usage.output_tokens, gen_ai.usage.total_tokens)

emilmilow avatar Aug 04 '25 12:08 emilmilow

This is not supported yet. Will convert this to a feature request.

hangfei avatar Aug 15 '25 19:08 hangfei

This is not supported yet. Will convert this to a feature request.

@hangfei check the PR, the data is there we just need to pass it to the LlmResponse. Also telemetry is a pretty easy fix, check the pr for that as well.

emilmilow avatar Aug 18 '25 07:08 emilmilow

@hangfei Waiting for this to get merged....

ravi-incred avatar Aug 19 '25 07:08 ravi-incred

token usage is supported here: https://github.com/google/adk-python/commit/6e5c0eb6e0474f5b908eb9df20328e7da85ebed9.

@wukath to see if we need tracing and evaluate the PR.

hangfei avatar Nov 04 '25 22:11 hangfei

@emilmilow Thank you for reporting this. I understand the request is currently under review. Please keep me posted if anything else is needed. Thanks for your patience.

klateefa avatar Nov 25 '25 07:11 klateefa

Thank you for reporting this issue and for your patience!

Both usage_metadata propagation and OpenTelemetry tracing in Live Mode (run_live) have since been implemented on main:

  1. usage_metadata on Live Events:

    • GeminiLlmConnection._to_generate_content_usage_metadata and GeminiLlmConnection.receive (src/google/adk/models/gemini_llm_connection.py) now extract message.usage_metadata from the Live API stream (mapping response_token_count and response_tokens_details to GenerateContentResponseUsageMetadata) and emit it on LlmResponse.usage_metadata.
    • postprocess_live_flow (src/google/adk/live/_live_llm_flow.py) finalizes and attaches usage_metadata onto the yielded Live Event, so you can inspect event.usage_metadata directly when iterating over runner.run_live(...).
  2. OpenTelemetry Tracing in Live Mode:

    • BaseAgent.run_live (src/google/adk/agents/base_agent.py) records the invoke_agent span via _instrumentation.record_agent_invocation.
    • run_live_flow (src/google/adk/live/_live_llm_flow.py) records send_data spans via trace_send_data.
    • Live tool invocations (src/google/adk/flows/llm_flows/tools/_live_caller.py) record execute_tool spans.

We are closing this issue as resolved. If you still encounter any missing fields or have additional questions on recent releases, please feel free to reopen this issue!

DeanChensj avatar Oct 13 '26 22:10 DeanChensj

Are you satisfied with the resolution of your issue? Yes No

github-actions[bot] avatar Oct 13 '26 22:10 github-actions[bot]