-
Notifications
You must be signed in to change notification settings - Fork 99
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
Conversation
"ai_prompt_tokens_used": { | ||
"value": 1000 | ||
}, | ||
"ai_completion_tokens_used": { | ||
"value": 2000 |
There was a problem hiding this comment.
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
// 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; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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())) |
There was a problem hiding this comment.
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)
bbbfd61
to
5c10171
Compare
5c10171
to
3d7289a
Compare
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