"""Budget guard checks for paid model calls.""" from __future__ import annotations from typing import Any from backend.services.cost_service import CostService, PriceSpec class CostGuardService: def __init__(self, cost_service: CostService): self.cost_service = cost_service def check_generation_budget( self, *, provider_id: str | None, provider_model: str | None, model_route: str | None = None, planned_task_count: int = 1, planned_image_count: int = 1, planned_total_tokens: int = 0, max_task_count: int | None = None, max_image_count: int | None = None, max_estimated_cost: float | None = None, days: int | None = None, unit_prices: dict[str, PriceSpec] | None = None, ) -> dict[str, Any]: provider = str(provider_id or "unknown") model = str(provider_model or "unknown") current = self.cost_service.usage_summary(provider_id=provider, days=days, unit_prices=unit_prices) planned_cost = self.cost_service.estimate_cost( provider_id=provider, provider_model=model, model_route=str(model_route or "planned"), task_count=planned_task_count, image_count=planned_image_count, total_tokens=planned_total_tokens, unit_prices=unit_prices, ) projected = { "task_count": current["totals"]["task_count"] + max(int(planned_task_count), 0), "image_count": current["totals"]["image_count"] + max(int(planned_image_count), 0), "estimated_cost": round(current["totals"]["estimated_cost"] + planned_cost, 6), } limits = { "max_task_count": _optional_int(max_task_count), "max_image_count": _optional_int(max_image_count), "max_estimated_cost": _optional_float(max_estimated_cost), } violations = _violations(projected, limits) return { "allowed": not violations, "provider_id": provider, "provider_model": model, "model_route": model_route, "current": current["totals"], "planned": { "task_count": max(int(planned_task_count), 0), "image_count": max(int(planned_image_count), 0), "total_tokens": max(int(planned_total_tokens), 0), "estimated_cost": planned_cost, }, "projected": projected, "limits": limits, "violations": violations, } def _violations(projected: dict[str, Any], limits: dict[str, Any]) -> list[str]: violations: list[str] = [] if limits["max_task_count"] is not None and projected["task_count"] > limits["max_task_count"]: violations.append("task_budget_exceeded") if limits["max_image_count"] is not None and projected["image_count"] > limits["max_image_count"]: violations.append("image_budget_exceeded") if limits["max_estimated_cost"] is not None and projected["estimated_cost"] > limits["max_estimated_cost"]: violations.append("cost_budget_exceeded") return violations def _optional_int(value: Any) -> int | None: if value is None or value == "": return None try: return int(value) except (TypeError, ValueError): return None def _optional_float(value: Any) -> float | None: if value is None or value == "": return None try: return float(value) except (TypeError, ValueError): return None