Skip to content

feat(ai): AI cost calculation #4840

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 1 commit into from
Jun 24, 2025
Merged

feat(ai): AI cost calculation #4840

merged 1 commit into from
Jun 24, 2025

Conversation

vgrozdanic
Copy link
Member

Fixes problems with old way of calculating costs where we only included input and output tokens into cost calculation, and refactors the whole calculate_ai_model_cost to be more robust, so that there is no need to change multiple functions when we want to include new field into cost calculation.

Closes TET-648: Refactor cost calculation

Comment on lines +2206 to +2210
"ai_prompt_tokens_used": {
"value": 1000
},
"ai_completion_tokens_used": {
"value": 2000
Copy link
Member Author

Choose a reason for hiding this comment

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

these are legacy fields that are still being sent, until we do a cut-off and fully transition to a new agent monitoring we need to support them

Comment on lines -26 to -36
// If we only have total tokens and no breakdown, use input cost for all tokens
// (assuming it's more common to have input cost defined in V1 configs)
if prompt_tokens_used.is_none() && completion_tokens_used.is_none() {
if let Some(total_tokens) = total_tokens_used {
if cost_per_token.input_per_token > 0.0 {
result += cost_per_token.input_per_token * total_tokens;
} else if cost_per_token.output_per_token > 0.0 {
result += cost_per_token.output_per_token * total_tokens;
}
}
}
Copy link
Member Author

Choose a reason for hiding this comment

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

It doesn't make sense to base our calculation on total_tokens used anymore, so this can now be removed

.and_then(|val| val.as_str())
// xxx (vgrozdanic): temporal fallback to legacy field, until we fix
// sentry conventions and standardize what SDKs send
.or_else(|| data.ai_model_id.value().and_then(|val| val.as_str()))
Copy link
Member Author

Choose a reason for hiding this comment

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

Currently we still want to use ai_model_id as a fallback, but we first need to clean up our sentry-conventions (we have made a mistake there), and we need to clean up the data that is being sent from our SDKs (currently both of these fields are sent, and one has priority)

@vgrozdanic vgrozdanic marked this pull request as ready for review June 24, 2025 10:55
@vgrozdanic vgrozdanic requested a review from a team as a code owner June 24, 2025 10:55
@vgrozdanic vgrozdanic force-pushed the vg/ai-cost-calculation branch 2 times, most recently from bbbfd61 to 5c10171 Compare June 24, 2025 10:59
@vgrozdanic vgrozdanic force-pushed the vg/ai-cost-calculation branch from 5c10171 to 3d7289a Compare June 24, 2025 11:04
@vgrozdanic vgrozdanic requested a review from a team June 24, 2025 11:43
@vgrozdanic vgrozdanic added this pull request to the merge queue Jun 24, 2025
@vgrozdanic vgrozdanic merged commit 063da46 into master Jun 24, 2025
27 of 28 checks passed
@vgrozdanic vgrozdanic deleted the vg/ai-cost-calculation branch June 24, 2025 13:13
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