fix: refund users when model returns zero tokens with non-zero cost#489
Open
sh1ftred wants to merge 1 commit into
Open
fix: refund users when model returns zero tokens with non-zero cost#489sh1ftred wants to merge 1 commit into
sh1ftred wants to merge 1 commit into
Conversation
When a model produces empty output (zero tokens reported) but the cost calculation returns a non-zero USD cost, the previous behavior was to charge the full amount as output_msats. This is incorrect — if no tokens were produced, the user should not be charged. Now the else branch: - Logs a warning with the usd_cost and model for debugging - Sets input_msats, output_msats, and cost_in_msats to 0 (full refund)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When a model produces empty output (zero tokens reported) but the cost calculation returns a non-zero USD cost, the previous behavior was to charge the full amount as
output_msats. This means users were being billed for responses where the model produced nothing.Fix
In the
elsebranch ofcalculate_cost()(when no tokens are reported):output_msats = cost_in_msats(user gets charged the full amount)input_msats = 0,output_msats = 0,cost_in_msats = 0— issuing a full refund to the userlogger.warningwithusd_costandmodeldetails for debugging/monitoringFile Changed
routstr/payment/cost_calculation.pyTesting
The change is a simple conditional branch update. When
usage.input_tokensandusage.output_tokensare both 0 butusd_cost > 0, the user now receives a full refund instead of being charged.