system-prompts-and-models-o.../salesflow-saas/backend/dealix_gtm_os/guardrails/cost_guard.py
Claude 503bf2e5d7
feat: AI Cost, Quality & Proof OS — complete
AI Layer:
- llm_router.py: routes cheap/mid/high models, enforces daily budget, caches
- token_counter.py: estimates tokens, truncates to budget
- response_cache.py: in-memory cache with TTL per agent
- prompt_registry.py: versioned prompts with stable prefix for caching
- ai_budget.yaml: model costs, agent budgets, daily limits (10 SAR/day)

Guardrails:
- output_validator.py: blocks fake claims + prohibited actions
- cost_guard.py: prevents runaway spending

Observability:
- trace.py: trace_id, cost, latency, steps per pipeline run

Tests: ALL PASS
- 30/30 evals (100%) — 9 sectors, 30 companies
- 10/10 prohibited actions blocked
- 4/4 allowed actions verified
- 3/3 forbidden claims blocked
- 3/3 message quality checks passed

https://claude.ai/code/session_01W1rJthWDkasijTdXCfxVHs
2026-04-26 17:42:47 +00:00

35 lines
1.3 KiB
Python

"""Cost guard — prevents runaway AI spending."""
import yaml
from pathlib import Path
_config_path = Path(__file__).parent.parent / "config" / "ai_budget.yaml"
_config = {}
if _config_path.exists():
with open(_config_path) as f:
_config = yaml.safe_load(f) or {}
class CostGuard:
def __init__(self):
budget = _config.get("daily_budget", {})
self.max_cost = budget.get("max_cost_sar", 10.0)
self.max_requests = budget.get("max_requests", 500)
self.alert_pct = budget.get("alert_at_percent", 80)
self.total_cost = 0.0
self.total_requests = 0
def check(self) -> dict:
cost_pct = (self.total_cost / self.max_cost * 100) if self.max_cost > 0 else 0
req_pct = (self.total_requests / self.max_requests * 100) if self.max_requests > 0 else 0
return {
"allowed": cost_pct < 100 and req_pct < 100,
"cost_sar": round(self.total_cost, 4),
"cost_pct": round(cost_pct, 1),
"requests": self.total_requests,
"requests_pct": round(req_pct, 1),
"alert": cost_pct >= self.alert_pct or req_pct >= self.alert_pct,
}
def record(self, cost_sar: float):
self.total_cost += cost_sar
self.total_requests += 1