-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsuggest_adaptive_routing.py
More file actions
153 lines (121 loc) · 5.24 KB
/
Copy pathsuggest_adaptive_routing.py
File metadata and controls
153 lines (121 loc) · 5.24 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env python3
"""Analyze telemetry and suggest if adaptive routing would save money.
This script checks your telemetry data and recommends enabling adaptive
routing if it would provide significant cost savings.
Run: python examples/suggest_adaptive_routing.py
Copyright 2025 Smart-AI-Memory
Licensed under Fair Source License 0.9
"""
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent / "src"))
from empathy_os.models import AdaptiveModelRouter
from empathy_os.telemetry import UsageTracker
def analyze_savings_potential():
"""Analyze telemetry and suggest adaptive routing if beneficial."""
print("=" * 70)
print("ADAPTIVE ROUTING SAVINGS ANALYSIS")
print("=" * 70)
try:
tracker = UsageTracker.get_instance()
router = AdaptiveModelRouter(tracker)
except Exception as e:
print(f"\n❌ Could not initialize: {e}")
print("Ensure Redis is running or use mock mode.")
return
# Get telemetry stats
stats = tracker.get_stats(days=30)
if stats["total_calls"] == 0:
print("\n📊 No telemetry data found.")
print("Run some workflows first to generate data:")
print(" empathy workflow run code-review")
print(" empathy workflow run bug-predict")
return
print(f"\n📊 Telemetry Data (Last 30 Days):")
print(f" Total calls: {stats['total_calls']:,}")
print(f" Total cost: ${stats['total_cost']:.2f}")
print(f" Cache hit rate: {stats['cache_hit_rate']:.1f}%")
print(f"\n💰 Current Cost Distribution:")
for tier, cost in sorted(stats["by_tier"].items(), key=lambda x: x[1], reverse=True):
pct = (cost / stats["total_cost"] * 100) if stats["total_cost"] > 0 else 0
print(f" {tier:8s}: ${cost:6.2f} ({pct:5.1f}%)")
# Analyze workflows for potential savings
print(f"\n🔍 Analyzing Workflow Efficiency...")
print("-" * 70)
workflows = list(stats["by_workflow"].keys())
total_potential_savings = 0.0
recommendations = []
for workflow_name in workflows[:10]: # Top 10 workflows
try:
wf_stats = router.get_routing_stats(workflow_name, days=30)
if wf_stats["total_calls"] < 10:
continue # Not enough data
current_cost = wf_stats["avg_cost"]
total_cost = wf_stats["total_calls"] * current_cost
# Estimate if 50% could use cheaper models
if current_cost > 0.002: # More expensive than Haiku
haiku_cost = 0.0016
potential_per_call = (current_cost - haiku_cost) * 0.5
potential_total = potential_per_call * wf_stats["total_calls"]
if potential_total > 0.5: # Only significant savings
total_potential_savings += potential_total
recommendations.append(
{
"workflow": workflow_name,
"calls": wf_stats["total_calls"],
"current_cost": current_cost,
"potential_savings": potential_total,
}
)
except Exception:
continue
# Show recommendations
if recommendations:
print(f"\n💡 Top Opportunities for Adaptive Routing:\n")
recommendations.sort(key=lambda x: x["potential_savings"], reverse=True)
for i, rec in enumerate(recommendations[:5], 1):
print(
f"{i}. {rec['workflow']}: ${rec['potential_savings']:.2f} potential savings"
)
print(
f" ({rec['calls']} calls @ ${rec['current_cost']:.4f} avg, "
f"could use cheaper models)"
)
print()
# Calculate annualized savings
days_analyzed = 30
annual_multiplier = 365 / days_analyzed
annual_savings = total_potential_savings * annual_multiplier
print("=" * 70)
print("💰 SAVINGS POTENTIAL")
print("=" * 70)
print(f" Last 30 days: ${total_potential_savings:.2f}")
print(f" Annualized: ${annual_savings:.2f}")
if annual_savings > 500:
print(f"\n✅ RECOMMENDATION: Enable adaptive routing!")
print(f" Potential savings: ${annual_savings:.2f}/year")
print(f"\n How to enable:")
print(f" 1. Add to workflow: enable_adaptive_routing=True")
print(
f" 2. Or set env var: export EMPATHY_ADAPTIVE_ROUTING=true"
)
print(
f" 3. Or add to config: adaptive_routing.enabled = true"
)
else:
print(f"\n📊 Current routing is fairly optimal.")
print(f" Adaptive routing would save ~${annual_savings:.2f}/year")
print(f" May not be worth enabling unless cost is a concern.")
else:
print("\n✅ Your workflows are already cost-optimized!")
print(" Adaptive routing would provide minimal benefit.")
print(f"\n" + "=" * 70)
print("ANALYSIS COMPLETE")
print("=" * 70)
if __name__ == "__main__":
try:
analyze_savings_potential()
except Exception as e:
print(f"\n❌ Error: {e}")
import traceback
traceback.print_exc()