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)
Contributor
|
some upstream will have the problem of not setting the token count for some reasons even when the user gets a response which in that case implies that the request is free |
Contributor
|
Heads up: I opened a clean re-implementation of this fix against current |
shroominic
added a commit
that referenced
this pull request
Jun 20, 2026
When an upstream reports a non-zero USD cost but the response carries no tokens at all (input, output, cache-read and cache-creation all zero), the USD path billed the full USD-derived amount for an empty response. Return an all-zero cost (full refund) for that case only. The gate is tightened relative to the superseded PR #489: a cache-read- or cache-creation-only turn legitimately reports zero prompt/completion tokens with a real cost and must still be billed, so the refund only fires when every token bucket is zero. Adds unit tests covering both the refund and the still-billed cache-only path. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.