Monitor OpenClaw API costs in real-time, set budget alerts, and optimize model spending.
Real user stories:
Check current session costs:
bash
# View token usage and estimated cost
openclaw /status
openclaw /usage
Set up cost alerts:
bash
# Alert when daily spend exceeds $10
echo "Alert if daily_cost > $10"
# Alert when monthly spend exceeds $100
echo "Alert if monthly_cost > $100"
Use cheaper models for simple tasks:
Routing rules:
text
Simple queries (weather, facts) → Haiku
Complex analysis → Sonnet
Creative writing → Opus (only if needed)
Reduce token waste:
Common mistake:
text
❌ Every 30 min: "Is it daytime yet?" → $18.75/night
✅ Every 6 hours: Check emails + calendar → $2/day
Best practices:
CategoryTokensCostHeartbeat checks50,000$0.50Web searches100,000$1.00Code generation200,000$2.00Document analysis150,000$1.50Total500,000$5.00/day
text
Daily average: $5.00
Monthly projection: $150.00
Budget limit: $100.00
⚠️ Warning: Will exceed budget by $50
text
Daily limit: $1.00
Alert at: $0.80/day
Actions: Reduce heartbeat frequency, use Haiku more
text
Daily limit: $2.50
Alert at: $2.00/day
Actions: Optimize context, review model routing
text
Daily limit: $10.00
Alert at: $8.00/day
Actions: Audit all automations, implement strict routing
bash
# Get current session stats
openclaw /status
# Get detailed usage breakdown
openclaw /usage --detailed
bash
# Last 7 days cost trend
echo "Analyze spending trend for past 7 days"
# Identify most expensive tasks
echo "What are my top 5 most expensive task types?"
bash
# Alert if spending is 2x normal
echo "Alert if hourly_cost > 2 * average_hourly_cost"
bash
# Send daily cost summary to Slack
curl -X POST https://hooks.slack.com/services/YOUR/WEBHOOK/URL \
-d '{"text": "Daily OpenClaw cost: $5.00 (Budget: $10.00)"}'
bash
# Send weekly cost report via email
echo "Weekly cost report: $35.00 total, $5.00/day average" | mail -s "OpenClaw Cost Report" user@example.com
bash
#!/bin/bash
# Pause session if daily cost exceeds limit
DAILY_LIMIT=10
CURRENT_COST=$(get_current_cost)
if (( $(echo "$CURRENT_COST > $DAILY_LIMIT" | bc -l) )); then
echo "⚠️ Daily cost ($CURRENT_COST) exceeds limit ($DAILY_LIMIT)"
echo "Pausing non-essential automations..."
# Add pause logic here
fi
bash
#!/bin/bash
# Route queries to appropriate model based on complexity
QUERY="$1"
WORD_COUNT=$(echo "$QUERY" | wc -w)
if [ $WORD_COUNT -lt 20 ]; then
echo "Using Haiku (simple query)"
MODEL="haiku"
elif [ $WORD_COUNT -lt 100 ]; then
echo "Using Sonnet (medium complexity)"
MODEL="sonnet"
else
echo "Using Opus (complex analysis)"
MODEL="opus"
fi
text
Agent keeps calling itself → 1000s of API calls → $100+ in hours
Fix: Implement loop detection and limits
text
Every tool output saved to context → Context grows → Each call costs more
Fix: Only store essential information
text
Using Opus for weather queries → 60x more expensive than needed
Fix: Implement model routing
text
Heartbeat every 5 minutes → 288 calls/day → $50+/day
Fix: Use 4-6 hour intervals
ModelInputOutputBest ForHaiku$0.25/1M$1.25/1MSimple queries, factsSonnet$3/1M$15/1MGeneral tasksOpus$15/1M$75/1MComplex analysis
ModelInputOutputBest ForGPT-4o Mini$0.15/1M$0.60/1MSimple queriesGPT-4o$2.5/1M$10/1MGeneral taskso1$15/1M$60/1MComplex reasoning
ModelInputOutputBest ForFlash$0.075/1M$0.30/1MSimple queriesPro$1.25/1M$5/1MGeneral tasksUltra$7.5/1M$30/1MComplex analysis
Calculate if automation is worth it:
text
Manual task time: 30 minutes
Your hourly rate: $50/hour
Manual cost: $25 per task
Automation cost:
- API calls: $0.50
- Setup time (amortized): $5.00
- Total: $5.50 per task
Savings: $25 - $5.50 = $19.50 per task
ROI: 355%