From c8f4e7d3e5fa7bc34a0e80ba72f942fbaf2c4703 Mon Sep 17 00:00:00 2001 From: Shroominic Date: Tue, 16 Sep 2025 13:00:38 +0100 Subject: [PATCH] fix max-tokens coming in as string --- routstr/payment/helpers.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/routstr/payment/helpers.py b/routstr/payment/helpers.py index 892aec59..6dc4b8ff 100644 --- a/routstr/payment/helpers.py +++ b/routstr/payment/helpers.py @@ -183,14 +183,23 @@ async def calculate_discounted_max_cost( else: adjusted = adjusted + math.ceil(-estimated_prompt_delta_sats * 1000) - if max_tokens := body.get("max_tokens"): - estimated_completion_delta_sats = ( - max_completion_allowed_sats - max_tokens * model_pricing.completion - ) - if estimated_completion_delta_sats >= 0: - adjusted = adjusted - math.floor(estimated_completion_delta_sats * 1000) + max_tokens_raw = body.get("max_tokens", None) + if max_tokens_raw is not None: + try: + max_tokens_int = int(max_tokens_raw) + except (TypeError, ValueError): + logger.warning( + "Invalid max_tokens; ignoring in cost adjustment", + extra={"max_tokens": str(max_tokens_raw)[:64], "model": model}, + ) else: - adjusted = adjusted + math.ceil(-estimated_completion_delta_sats * 1000) + estimated_completion_delta_sats = ( + max_completion_allowed_sats - max_tokens_int * model_pricing.completion + ) + if estimated_completion_delta_sats >= 0: + adjusted = adjusted - math.floor(estimated_completion_delta_sats * 1000) + else: + adjusted = adjusted + math.ceil(-estimated_completion_delta_sats * 1000) logger.debug( "Discounted max cost computed",