Heartbeat Token Cost Analysis
Deep analysis of OpenClaw's cron heartbeat mechanism and its token cost implications.
Source
src/cron/service/timer.ts (877 lines)
What is a Heartbeat?
A heartbeat is a full context sent to the LLM every 600 seconds (10 minutes) expecting a simple "HEARTBEAT_OK" response. This keeps long-running cron jobs alive and responsive.
Cost Model
Token Consumption Per Call
Pricing assumptions (Claude Opus 4.6):
- Input: $3/M tokens
- Output: $15/M tokens
Daily Cost Projections
At 600-second (10-minute) intervals = 144 heartbeats/day
Monthly Projections (30 days)
Error Backoff Strategy
When heartbeat errors occur, OpenClaw applies exponential backoff:
MAX_SCHEDULE_ERRORS = 3 before job is disabled.
Timeout Configuration
Timeout per job execution: 10 minutes (600,000ms)
Optimization Recommendations
1. Increase Heartbeat Interval
Current: 600 seconds (10 minutes) Proposed: 1800-3600 seconds (30-60 minutes)
Savings: 3-6x reduction in daily costs
2. Use Lightweight Health Check
Replace full context with minimal status check:
- Send only: job metadata + last run status
- Expected response: simple "OK" or status enum
- Token savings: 95%+ reduction
Estimated cost with lightweight check:
- Input: ~500 tokens (metadata only)
- Output: ~100 tokens
- Cost/call: ~$0.003
- Daily cost: ~$0.43 (vs. $86.40)
3. Conditional Heartbeat
Only send heartbeat when:
- Job has pending work
- Last run was > 1 hour ago
- User explicitly configured heartbeat: true
Savings: 50-80% reduction depending on job activity patterns
4. Context Pruning for Heartbeats
Apply aggressive compaction before heartbeat:
- Keep only: last 5-10 messages
- Summary of older history
- Token savings: 60-80% reduction
Trade-offs
Recommended Hybrid Approach
- Increase interval to 30 minutes (immediate 3x savings, minimal risk)
- Add lightweight check between full heartbeats (check every 5 min, full heartbeat every 30 min)
- Apply context pruning to full heartbeats (keep last 10 messages + summary)
Expected savings: 80-90% cost reduction with acceptable trade-offs