Skip to content

Improve token usage recording #4566

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

antonpirker
Copy link
Member

@antonpirker antonpirker commented Jul 9, 2025

Update token usage recording to work if the LLM is calling them prompt_tokens or input_tokens. Same for completion_tokens and output_tokens. Records also cached and reasoning tokens usage.

Because the signature of a helper function was changed, other AI integrations also have changes.

Copy link

codecov bot commented Jul 9, 2025

❌ 101 Tests Failed:

Tests completed Failed Passed Skipped
24112 101 24011 5814
View the top 3 failed test(s) by shortest run time
tests.integrations.anthropic.test_anthropic::test_add_ai_data_to_span_with_input_json_delta
Stack Traces | 0.067s run time
.../integrations/anthropic/test_anthropic.py:814: in test_add_ai_data_to_span_with_input_json_delta
    assert span._measurements.get("ai_prompt_tokens_used")["value"] == 10
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
E   TypeError: 'NoneType' object is not subscriptable
tests.integrations.anthropic.test_anthropic::test_add_ai_data_to_span_with_input_json_delta
Stack Traces | 0.078s run time
.../integrations/anthropic/test_anthropic.py:814: in test_add_ai_data_to_span_with_input_json_delta
    assert span._measurements.get("ai_prompt_tokens_used")["value"] == 10
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
E   TypeError: 'NoneType' object is not subscriptable
tests.integrations.huggingface_hub.test_huggingface_hub::test_streaming_chat_completion[True-False-True]
Stack Traces | 0.085s run time
.../integrations/huggingface_hub/test_huggingface_hub.py:137: in test_streaming_chat_completion
    assert span["measurements"]["ai_total_tokens_used"]["value"] == 10
           ^^^^^^^^^^^^^^^^^^^^
E   KeyError: 'measurements'

To view more test analytics, go to the Test Analytics Dashboard
📋 Got 3 mins? Take this short survey to help us improve Test Analytics.

@antonpirker antonpirker changed the base branch from antonpirker/openai-overhaul to antonpirker/data-instead-of-measurements July 10, 2025 08:01
Base automatically changed from antonpirker/data-instead-of-measurements to master July 10, 2025 13:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant