fixed final with new model config and tokens

This commit is contained in:
IGNY8 VPS (Salman)
2025-12-24 15:33:17 +00:00
parent 02d4f1fa46
commit 64e76f5436

View File

@@ -5,6 +5,7 @@ import time
import logging import logging
from typing import List, Dict, Any, Optional, Callable from typing import List, Dict, Any, Optional, Callable
from datetime import datetime from datetime import datetime
from decimal import Decimal
from igny8_core.ai.constants import DEBUG_MODE from igny8_core.ai.constants import DEBUG_MODE
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -195,24 +196,35 @@ class CostTracker:
"""Tracks API costs and token usage""" """Tracks API costs and token usage"""
def __init__(self): def __init__(self):
self.total_cost = 0.0 self.total_cost = Decimal('0.0')
self.total_tokens = 0 self.total_tokens = 0
self.operations = [] self.operations = []
def record(self, function_name: str, cost: float, tokens: int, model: str = None): def record(self, function_name: str, cost, tokens: int, model: str = None):
"""Record an API call cost""" """Record an API call cost
Args:
function_name: Name of the AI function
cost: Cost value (can be float or Decimal)
tokens: Number of tokens used
model: Model name
"""
# Convert cost to Decimal if it's a float to avoid type mixing
if not isinstance(cost, Decimal):
cost = Decimal(str(cost))
self.total_cost += cost self.total_cost += cost
self.total_tokens += tokens self.total_tokens += tokens
self.operations.append({ self.operations.append({
'function': function_name, 'function': function_name,
'cost': cost, 'cost': float(cost), # Store as float for JSON serialization
'tokens': tokens, 'tokens': tokens,
'model': model 'model': model
}) })
def get_total(self) -> float: def get_total(self):
"""Get total cost""" """Get total cost (returns float for JSON serialization)"""
return self.total_cost return float(self.total_cost)
def get_total_tokens(self) -> int: def get_total_tokens(self) -> int:
"""Get total tokens""" """Get total tokens"""