Files
openclaw/tools/budget-alert.sh
Clawd Bot ca9b510922 chore: align with upstream openclaw/openclaw and overlay local additions
- Reset master to upstream/main (16,697 commits)
- Overlay 2,271 local-only files (skills, tools, workspace, configs, apps)
- Restore IDENTITY.md and USER.md templates
- Build verified, gateway running, Discord working

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-03 07:40:46 +01:00

71 lines
2.2 KiB
Bash
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# Budget Alert: Check Anthropic API usage and send alerts
# Run via cron hourly or daily
set -e
DASHBOARD_API="http://192.168.1.220:8888/api/anthropic"
MONTHLY_LIMIT=90.00 # EUR
ALERT_THRESHOLDS=(50 70 80) # Alert at 56%, 78%, 89%
# Fetch current usage
RESPONSE=$(curl -s "$DASHBOARD_API")
SPENT=$(echo "$RESPONSE" | jq -r '.monthly.spent')
LIMIT=$(echo "$RESPONSE" | jq -r '.monthly.limit')
PERCENT=$(echo "$RESPONSE" | jq -r '.monthly.used_percent')
if [ "$SPENT" = "null" ] || [ -z "$SPENT" ]; then
echo "❌ Failed to fetch Anthropic usage"
exit 1
fi
echo "💰 Anthropic API Usage: €$SPENT / €$LIMIT ($PERCENT%)"
# Check alert thresholds
for threshold in "${ALERT_THRESHOLDS[@]}"; do
alert_file="/tmp/anthropic-alert-${threshold}.sent"
# Calculate threshold amount
threshold_amount=$(echo "scale=2; $LIMIT * $threshold / 100" | bc)
# Check if spent exceeds threshold
if (( $(echo "$SPENT >= $threshold_amount" | bc -l) )); then
# Check if alert already sent this month
if [ ! -f "$alert_file" ] || [ "$(date +%Y-%m)" != "$(stat -c %y "$alert_file" | cut -d' ' -f1 | cut -d- -f1-2)" ]; then
# Send alert
MESSAGE="⚠️ Anthropic API Budget Alert
Usage: €${SPENT} / €${LIMIT} (${PERCENT}%)
Threshold: ${threshold}% (€${threshold_amount})
Current month spending is above ${threshold}% threshold.
Dashboard: http://192.168.1.220:8888/
Analysis: /tmp/ai-subscriptions-analysis.md"
# Send via OpenClaw (to default channel)
echo "$MESSAGE"
# Mark alert as sent
touch "$alert_file"
echo "✅ Alert sent for ${threshold}% threshold"
else
echo " Alert for ${threshold}% already sent this month"
fi
fi
done
# Reset alert files on new month
current_month=$(date +%Y-%m)
for alert_file in /tmp/anthropic-alert-*.sent; do
if [ -f "$alert_file" ]; then
file_month=$(stat -c %y "$alert_file" | cut -d' ' -f1 | cut -d- -f1-2)
if [ "$file_month" != "$current_month" ]; then
rm "$alert_file"
echo "🗑️ Cleared old alert: $(basename $alert_file)"
fi
fi
done
echo "✅ Budget check complete"