Skip to content

fix: refund users when model returns zero tokens with non-zero cost#489

Open
sh1ftred wants to merge 1 commit into
mainfrom
fix/refund-zero-token-responses
Open

fix: refund users when model returns zero tokens with non-zero cost#489
sh1ftred wants to merge 1 commit into
mainfrom
fix/refund-zero-token-responses

Conversation

@sh1ftred
Copy link
Copy Markdown
Contributor

@sh1ftred sh1ftred commented May 6, 2026

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 else branch of calculate_cost() (when no tokens are reported):

  • Before: output_msats = cost_in_msats (user gets charged the full amount)
  • After: Set input_msats = 0, output_msats = 0, cost_in_msats = 0 — issuing a full refund to the user
  • Added a logger.warning with usd_cost and model details for debugging/monitoring

File Changed

  • routstr/payment/cost_calculation.py

Testing

The change is a simple conditional branch update. When usage.input_tokens and usage.output_tokens are both 0 but usd_cost > 0, the user now receives a full refund instead of being charged.

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant