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

Merged
merged 5 commits into from
Jul 15, 2025
Merged

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.

This PR does not change behavior, just prepare the ground for future changes to the AI integrations.

Copy link

codecov bot commented Jul 9, 2025

Codecov Report

Attention: Patch coverage is 86.00000% with 7 lines in your changes missing coverage. Please review.

Project coverage is 80.72%. Comparing base (657c2b1) to head (520c3d7).
Report is 1 commits behind head on master.

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
sentry_sdk/ai/monitoring.py 60.00% 2 Missing and 2 partials ⚠️
sentry_sdk/integrations/openai.py 91.66% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4566      +/-   ##
==========================================
- Coverage   80.72%   80.72%   -0.01%     
==========================================
  Files         156      156              
  Lines       16496    16507      +11     
  Branches     2806     2806              
==========================================
+ Hits        13317    13325       +8     
- Misses       2298     2299       +1     
- Partials      881      883       +2     
Files with missing lines Coverage Δ
sentry_sdk/integrations/anthropic.py 85.29% <100.00%> (ø)
sentry_sdk/integrations/cohere.py 75.80% <ø> (ø)
sentry_sdk/integrations/huggingface_hub.py 83.83% <100.00%> (ø)
sentry_sdk/integrations/langchain.py 70.90% <ø> (ø)
sentry_sdk/integrations/openai.py 80.66% <91.66%> (+0.51%) ⬆️
sentry_sdk/ai/monitoring.py 81.81% <60.00%> (-4.49%) ⬇️

... and 3 files with indirect coverage changes

@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
@antonpirker antonpirker marked this pull request as ready for review July 15, 2025 12:40
@antonpirker antonpirker requested a review from a team as a code owner July 15, 2025 12:40
Copy link
Contributor

@sentrivana sentrivana left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, see one suggestion

Co-authored-by: Ivana Kellyer <ivana.kellyer@sentry.io>
@antonpirker antonpirker enabled auto-merge (squash) July 15, 2025 14:47
@antonpirker antonpirker merged commit 2ccab61 into master Jul 15, 2025
136 checks passed
@antonpirker antonpirker deleted the antonpirker/openai-token-usage branch July 15, 2025 14:54
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.

2 participants