Compare commits

...

1 Commits

Author SHA1 Message Date
redshift
da1065fa50 fix: refund users when model returns zero tokens with non-zero cost
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)
2026-05-06 19:15:05 +08:00

View File

@@ -180,7 +180,17 @@ async def calculate_cost( # todo: can be sync
input_msats = int(cost_in_msats * input_ratio)
output_msats = cost_in_msats - input_msats
else:
output_msats = cost_in_msats
# No tokens reported — model produced empty output, issue full refund
logger.warning(
"Zero tokens with non-zero USD cost — issuing full refund",
extra={
"usd_cost": usd_cost,
"model": response_data.get("model", "unknown"),
},
)
input_msats = 0
output_msats = 0
cost_in_msats = 0
logger.info(
"Using cost from usage data/details",