forked from microsoft/agent-governance-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcost_guard.py
More file actions
74 lines (57 loc) · 2.64 KB
/
cost_guard.py
File metadata and controls
74 lines (57 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
"""
Cost Guard Example — Catch runaway agent spending.
Demonstrates how CostGuard prevents agent cost explosions by setting
per-task, per-agent, and organization-wide budgets with automatic alerts.
Run:
pip install agent-sre
python examples/cost_guard.py
"""
from agent_sre.cost.guard import CostGuard
# ── Set up cost guardrails ──────────────────────────────────────────────
guard = CostGuard(
per_task_limit=1.00, # No single task should cost > $1
per_agent_daily_limit=20.0, # Each agent: max $20/day
org_monthly_budget=500.0, # Org-wide: $500/month
alert_thresholds=[0.50, 0.75, 0.90], # Alert at 50%, 75%, 90%
)
print("Cost Guard Example")
print("=" * 60)
print()
# ── Simulate normal agent work ──────────────────────────────────────────
agents = ["researcher", "writer", "reviewer"]
for i in range(30):
agent = agents[i % len(agents)]
cost = 0.25 # Normal cost: $0.25 per task
allowed, reason = guard.check_task(agent, estimated_cost=cost)
if allowed:
alerts = guard.record_cost(agent, f"task-{i}", cost)
for alert in alerts:
print(f" ⚠️ {alert.severity.value}: {alert.message}")
print(f"After 30 normal tasks:")
print(f" Total spent: ${guard.org_spent_month:.2f}")
for agent in agents:
budget = guard.get_budget(agent)
print(f" {agent}: ${budget.spent_today_usd:.2f} spent, ${budget.remaining_today_usd:.2f} remaining")
print()
# ── Simulate runaway tool loop ──────────────────────────────────────────
print("Simulating runaway tool loop (researcher calls expensive API)...")
print()
for i in range(50):
cost = 0.80 # Expensive: $0.80 per call
allowed, reason = guard.check_task("researcher", estimated_cost=cost)
if not allowed:
print(f" 🛑 Task {i} BLOCKED: {reason}")
break
alerts = guard.record_cost("researcher", f"loop-{i}", cost)
for alert in alerts:
print(f" ⚠️ {alert.severity.value}: {alert.message}")
print()
print("Summary:")
print(f" Org spent: ${guard.org_spent_month:.2f} / ${guard.org_monthly_budget:.2f}")
researcher = guard.get_budget("researcher")
print(f" Researcher: ${researcher.spent_today_usd:.2f} spent (limit: ${researcher.daily_limit_usd:.2f})")
print()
print("─" * 60)
print("CostGuard stopped the runaway before it burned through the budget.")