-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathalert_webhook.py
More file actions
59 lines (46 loc) · 1.88 KB
/
alert_webhook.py
File metadata and controls
59 lines (46 loc) · 1.88 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
# Example: Send alert when costs spike (using webhook.site for demo)
import requests
from datetime import datetime
# 🎯 STUDENT INSTRUCTIONS:
# 1. Go to https://webhook.site
# 2. Copy your unique URL
# 3. Paste it below
# 4. Run this script and watch the alert arrive!
WEBHOOK_URL = "https://webhook.site/b7313890-14e1-4a1b-9b5d-16371f7328eb"
def check_costs_and_alert():
"""
Monitor LLM costs and send alerts when threshold exceeded.
In production, replace webhook.site with:
- Slack: https://hooks.slack.com/services/YOUR/WEBHOOK/URL
- Discord: https://discord.com/api/webhooks/YOUR/WEBHOOK
- PagerDuty, Opsgenie, etc.
"""
# Simulated cost (in production, calculate from Langfuse metrics)
hourly_cost = 15.50 # $15.50 spent this hour
threshold = 10.00 # Alert if over $10/hour
print(f"💰 Current hourly cost: ${hourly_cost:.2f}")
print(f"⚠️ Threshold: ${threshold:.2f}")
if hourly_cost > threshold:
print(f"\n🚨 COST SPIKE DETECTED! Sending alert...\n")
response = requests.post(
WEBHOOK_URL,
json={
"text": f"🚨 Cost Alert: ${hourly_cost:.2f} spent in last hour",
"timestamp": datetime.now().isoformat(),
"alert_type": "cost_spike",
"details": {
"hourly_cost": f"${hourly_cost:.2f}",
"threshold": f"${threshold:.2f}",
"overage": f"${hourly_cost - threshold:.2f}",
"dashboard": "https://cloud.langfuse.com",
},
},
)
if response.status_code == 200:
print("✅ Alert sent! Check webhook.site to see it.")
else:
print(f"❌ Failed: {response.status_code}")
else:
print("✅ Costs within budget. No alert needed.")
if __name__ == "__main__":
check_costs_and_alert()