From da1065fa5080805f1f0ea08d8f3ae24ce3554262 Mon Sep 17 00:00:00 2001 From: redshift <213178690+1ftredsh@users.noreply.github.com> Date: Wed, 6 May 2026 19:15:05 +0800 Subject: [PATCH] fix: refund users when model returns zero tokens with non-zero cost MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) --- routstr/payment/cost_calculation.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/routstr/payment/cost_calculation.py b/routstr/payment/cost_calculation.py index 292b6d5f..7cdfe02f 100644 --- a/routstr/payment/cost_calculation.py +++ b/routstr/payment/cost_calculation.py @@ -180,7 +180,17 @@ def parse_token_count(value: object) -> int: 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",