-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbudget-aware-routing.py
More file actions
113 lines (95 loc) · 4.09 KB
/
Copy pathbudget-aware-routing.py
File metadata and controls
113 lines (95 loc) · 4.09 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
"""
Rhumb Example: Budget-aware routing with cost controls.
This script shows how to:
1. Set a budget limit for your agent
2. Use cost-optimal routing to pick the cheapest provider above a quality floor
3. Check spend against budget
Requires: RHUMB_API_KEY (your governed API key) environment variable.
"""
import os
import httpx
from resolve_helpers import (
describe_recovery_hint,
preferred_execute_provider,
preferred_recovery_handoff,
recovery_credential_modes_url,
recovery_resolve_url,
)
BASE = "https://api.rhumb.dev/v1"
API_KEY = os.environ.get("RHUMB_API_KEY")
def main():
if not API_KEY:
print("⚠️ Set RHUMB_API_KEY (your governed API key) to run this example.")
print(" Get a governed API key at https://rhumb.dev/auth/login")
return
headers = {"X-Rhumb-Key": API_KEY, "Content-Type": "application/json"}
# Step 1: Check current balance
print("💳 Checking balance...\n")
balance = httpx.get(f"{BASE}/agent/billing", headers=headers)
b = balance.json().get("data", balance.json())
print(f" Balance: ${b.get('balance_usd', b.get('balance', '?'))}")
# Step 2: Set routing strategy to cost-optimal
print("\n⚙️ Setting routing to cost-optimal (quality floor: 6.0)...\n")
routing = httpx.post(
f"{BASE}/agent/routing",
headers=headers,
json={
"strategy": "cheapest",
"quality_floor": 6.0,
"max_cost_per_call_usd": 0.05,
},
)
print(f" Routing configured: {routing.status_code}")
# Step 3: Resolve with routing applied
capability = "data.enrich_company"
print(f"\n🔍 Resolving '{capability}' with cost-optimal routing...\n")
resp = httpx.get(f"{BASE}/capabilities/{capability}/resolve", headers=headers)
data = resp.json().get("data", {})
providers = data.get("providers", [])
execute_hint = data.get("execute_hint") or {}
for p in providers[:3]:
slug = p.get("service_slug", "?")
score = p.get("an_score", "?")
cost = p.get("cost_per_call", "?")
print(f" {slug:20s} AN Score: {score} Est. cost: ${cost}")
preferred_provider = preferred_execute_provider(data)
recovery_summary = describe_recovery_hint(data)
recovery_handoff = preferred_recovery_handoff(data)
credential_modes_url = recovery_credential_modes_url(data)
resolve_url = recovery_resolve_url(data)
if preferred_provider:
print(f"\n🧭 Preferred execute provider: {preferred_provider}")
if execute_hint.get("preferred_credential_mode"):
print(f" Preferred credential mode: {execute_hint['preferred_credential_mode']}")
elif recovery_handoff:
handoff_kind, handoff = recovery_handoff
provider = handoff.get("preferred_provider", "?")
mode = handoff.get("preferred_credential_mode", "?")
if handoff_kind == "alternate_execute":
print(f"\n🧭 Alternate execute rail: {provider} ({mode})")
if handoff.get("endpoint_pattern"):
print(f" Endpoint: {handoff['endpoint_pattern']}")
else:
print(f"\n🧭 Setup next: {provider} ({mode})")
if handoff.get("setup_url"):
print(f" Setup URL: {handoff['setup_url']}")
elif handoff.get("setup_hint"):
print(f" Setup hint: {handoff['setup_hint']}")
if resolve_url:
print(f" Resolve URL: {resolve_url}")
if credential_modes_url:
print(f" Credential modes URL: {credential_modes_url}")
elif resolve_url:
print(f"\n🧭 Resolve URL: {resolve_url}")
if credential_modes_url:
print(f" Credential modes URL: {credential_modes_url}")
if recovery_summary:
print(f" Recovery hint: {recovery_summary}")
# Step 4: Check spend
print("\n📊 Current spend...\n")
spend = httpx.get(f"{BASE}/agent/billing/spend", headers=headers)
s = spend.json().get("data", spend.json())
print(f" Total spend: ${s.get('total_spend_usd', s.get('total', '?'))}")
print(f" Calls today: {s.get('calls_today', '?')}")
if __name__ == "__main__":
main()