mirror of https://github.com/aliasrobotics/cai.git
FIX CAI_PRICE_LIMIT
This commit is contained in:
parent
f4199b947f
commit
8dc4cdd64f
|
|
@ -123,6 +123,8 @@ def update_toolbar_in_background():
|
|||
os.getenv('CAI_MODEL', 'default')}</ansigreen> | "
|
||||
f"<ansicyan><b>Max Turns:</b></ansicyan> <ansiblue>{
|
||||
os.getenv('CAI_MAX_TURNS', 'inf')}</ansiblue> | "
|
||||
f"<ansiyellow><b>Price Limit:</b></ansiyellow> <ansiblue>{
|
||||
os.getenv('CAI_PRICE_LIMIT', 'inf')}</ansiblue> | "
|
||||
f"<ansigray>{current_time_with_tz}</ansigray>"
|
||||
)
|
||||
toolbar_cache['last_update'] = datetime.datetime.now()
|
||||
|
|
|
|||
|
|
@ -61,3 +61,9 @@ class OutputGuardrailTripwireTriggered(AgentsException):
|
|||
super().__init__(
|
||||
f"Guardrail {guardrail_result.guardrail.__class__.__name__} triggered tripwire"
|
||||
)
|
||||
|
||||
|
||||
class PriceLimitExceeded(AgentsException):
|
||||
"""Raised when the maximum price limit is exceeded."""
|
||||
def __init__(self, current_cost: float, price_limit: float):
|
||||
super().__init__(f"Maximum price limit (${price_limit:.4f}) exceeded. Current cost: ${current_cost:.4f}")
|
||||
|
|
|
|||
|
|
@ -58,6 +58,15 @@ if max_turns_env is not None:
|
|||
else:
|
||||
DEFAULT_MAX_TURNS = float("inf")
|
||||
|
||||
price_limit_env = os.getenv("CAI_PRICE_LIMIT")
|
||||
if price_limit_env is not None:
|
||||
try:
|
||||
DEFAULT_PRICE_LIMIT = float(price_limit_env)
|
||||
except ValueError:
|
||||
DEFAULT_PRICE_LIMIT = float("inf")
|
||||
else:
|
||||
DEFAULT_PRICE_LIMIT = float("inf")
|
||||
|
||||
|
||||
@dataclass
|
||||
class RunConfig:
|
||||
|
|
|
|||
|
|
@ -228,16 +228,22 @@ class CostTracker:
|
|||
# Track the last calculation to debug inconsistencies
|
||||
last_interaction_cost: float = 0.0
|
||||
last_total_cost: float = 0.0
|
||||
|
||||
def reset_interaction_stats(self):
|
||||
"""Reset stats for a new interaction"""
|
||||
self.interaction_input_tokens = 0
|
||||
self.interaction_output_tokens = 0
|
||||
self.interaction_reasoning_tokens = 0
|
||||
self.interaction_cost = 0.0
|
||||
|
||||
def check_price_limit(self, new_cost: float) -> None:
|
||||
"""Check if adding the new cost would exceed the price limit."""
|
||||
from cai.sdk.agents.exceptions import PriceLimitExceeded
|
||||
from cai.sdk.agents.run import DEFAULT_PRICE_LIMIT
|
||||
|
||||
if DEFAULT_PRICE_LIMIT != float("inf"):
|
||||
total_cost = self.session_total_cost + new_cost
|
||||
if total_cost > DEFAULT_PRICE_LIMIT:
|
||||
raise PriceLimitExceeded(total_cost, DEFAULT_PRICE_LIMIT)
|
||||
|
||||
def update_session_cost(self, new_cost: float) -> None:
|
||||
"""Add cost to session total and log the update"""
|
||||
# Check price limit before updating
|
||||
self.check_price_limit(new_cost)
|
||||
|
||||
old_total = self.session_total_cost
|
||||
self.session_total_cost += new_cost
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue