From e154f65e16027ace12716fcb722daccbfbdd7aca Mon Sep 17 00:00:00 2001 From: Shroominic Date: Sun, 23 Nov 2025 12:06:50 -0800 Subject: [PATCH] fix revenue tracking problems --- routstr/core/log_manager.py | 20 ++++++++++++++++---- routstr/core/logging.py | 4 ++-- routstr/upstream/base.py | 5 +++-- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/routstr/core/log_manager.py b/routstr/core/log_manager.py index 0916a33f..55346f08 100644 --- a/routstr/core/log_manager.py +++ b/routstr/core/log_manager.py @@ -231,7 +231,10 @@ class LogManager: if "received proxy request" in message: model_stats[model]["requests"] += 1 - if "token adjustment completed" in message: + if ( + "completed for streaming" in message + or "completed for non-streaming" in message + ): model_stats[model]["successful"] += 1 cost_data = entry.get("cost_data") if isinstance(cost_data, dict): @@ -321,7 +324,10 @@ class LogManager: if "received proxy request" in message: stats["total_requests"] += 1 - if "token adjustment completed" in message: + if ( + "completed for streaming" in message + or "completed for non-streaming" in message + ): stats["successful_chat_completions"] += 1 if "upstream request failed" in message or "revert payment" in message: @@ -338,7 +344,10 @@ class LogManager: if isinstance(model, str) and model != "unknown": stats["unique_models"].add(model) - if "token adjustment completed" in message: + if ( + "completed for streaming" in message + or "completed for non-streaming" in message + ): cost_data = entry.get("cost_data") if isinstance(cost_data, dict): actual_cost = cost_data.get("total_msats", 0) @@ -426,7 +435,10 @@ class LogManager: if level == "ERROR": bucket["errors"] += 1 - if "token adjustment completed" in message: + if ( + "completed for streaming" in message + or "completed for non-streaming" in message + ): cost_data = entry.get("cost_data") if isinstance(cost_data, dict): actual_cost = cost_data.get("total_msats", 0) diff --git a/routstr/core/logging.py b/routstr/core/logging.py index 6dbd2cfc..ec85b3c2 100644 --- a/routstr/core/logging.py +++ b/routstr/core/logging.py @@ -10,8 +10,8 @@ DO NOT modify or remove these messages without updating the usage tracking logic - Used to count total incoming requests - Includes model information in context -2. "Token adjustment completed for streaming" (INFO) - routstr/upstream/base.py - "Token adjustment completed for non-streaming" (INFO) - routstr/upstream/base.py + 2. "Payment adjustment completed for streaming" (INFO) - routstr/upstream/base.py + "Payment adjustment completed for non-streaming" (INFO) - routstr/upstream/base.py - Used to track successful completions and revenue - The 'cost_data.total_msats' field is extracted for revenue calculation - Must include 'cost_data' in extra dict diff --git a/routstr/upstream/base.py b/routstr/upstream/base.py index 7af1be85..aa994147 100644 --- a/routstr/upstream/base.py +++ b/routstr/upstream/base.py @@ -453,11 +453,12 @@ class BaseUpstreamProvider: ) usage_finalized = True logger.info( - "Token adjustment completed for streaming", + "Payment adjustment completed for streaming", extra={ "key_hash": key.hashed_key[:8] + "...", "cost_data": cost_data, + "model": last_model_seen, "balance_after_adjustment": fresh_key.balance, }, ) @@ -556,7 +557,7 @@ class BaseUpstreamProvider: response_json["cost"] = cost_data logger.info( - "Token adjustment completed for non-streaming", + "Payment adjustment completed for non-streaming", extra={ "key_hash": key.hashed_key[:8] + "...", "cost_data": cost_data,