Reconcile upstream into CT116 production state #1
Executable
+56
@@ -0,0 +1,56 @@
|
||||
#!/bin/bash
|
||||
# Daily LiteLLM cost snapshot — cron at midnight
|
||||
# Writes to /var/log/litellm/costs.csv on CT 116
|
||||
|
||||
MASTER="sk-litellm-7f96080dd99b15c36bd4b333b58a6796"
|
||||
BASE="http://127.0.0.1:4001"
|
||||
CSV="/var/log/litellm/costs.csv"
|
||||
DATE=$(date -I)
|
||||
YESTERDAY=$(date -I -d yesterday)
|
||||
|
||||
# Fetch yesterday's spend by key
|
||||
RESP=$(curl -s "${BASE}/global/spend/logs?start_date=${YESTERDAY}T00:00:00&end_date=${YESTERDAY}T23:59:59&page_size=1000" \
|
||||
-H "Authorization: Bearer $MASTER" 2>/dev/null)
|
||||
|
||||
# Parse total spend
|
||||
TOTAL=$(echo "$RESP" | python3 -c "
|
||||
import sys, json
|
||||
data = json.load(sys.stdin)
|
||||
total = sum(r.get('spend', 0) or 0 for r in data.get('data', []))
|
||||
print(f'{total:.6f}')
|
||||
" 2>/dev/null)
|
||||
|
||||
# Parse per-model spend
|
||||
PER_MODEL=$(echo "$RESP" | python3 -c "
|
||||
import sys, json
|
||||
from collections import defaultdict
|
||||
data = json.load(sys.stdin)
|
||||
spend = defaultdict(float)
|
||||
for r in data.get('data', []):
|
||||
model = r.get('model', 'unknown')
|
||||
spend[model] += r.get('spend', 0) or 0
|
||||
for model, cost in sorted(spend.items()):
|
||||
print(f'{model}:{cost:.6f}')
|
||||
" 2>/dev/null)
|
||||
|
||||
# Per-key spend
|
||||
PER_KEY=$(echo "$RESP" | python3 -c "
|
||||
import sys, json
|
||||
from collections import defaultdict
|
||||
data = json.load(sys.stdin)
|
||||
spend = defaultdict(float)
|
||||
for r in data.get('data', []):
|
||||
key = r.get('api_key', 'unknown')[:20]
|
||||
spend[key] += r.get('spend', 0) or 0
|
||||
for key, cost in sorted(spend.items()):
|
||||
print(f'{key}:{cost:.6f}')
|
||||
" 2>/dev/null)
|
||||
|
||||
# Write CSV
|
||||
mkdir -p $(dirname "$CSV")
|
||||
if [ ! -f "$CSV" ]; then
|
||||
echo "date,total_cost,per_model,per_key" > "$CSV"
|
||||
fi
|
||||
echo "${DATE},${TOTAL},\"${PER_MODEL}\",\"${PER_KEY}\"" >> "$CSV"
|
||||
|
||||
echo "Snapshot: ${DATE} | Total: \$${TOTAL}"
|
||||
Reference in New Issue
Block a user