Overview
Heartbeat runs a periodic check loop that:- Reads tasks from
HEARTBEAT.md - Executes agent with heartbeat prompt
- Processes tool calls and responses
- Reports results to user (optional)
- Monitoring device status
- Checking for notifications
- Periodic system health checks
- Automated maintenance tasks
- Scheduled reports
Configuration
Service Creation
Parameters
- workspace (string) - Working directory containing
HEARTBEAT.md - intervalMinutes (int) - Check interval (minimum: 5 minutes)
- enabled (bool) - Service enabled status
- Minimum interval: 5 minutes
- Default interval: 30 minutes
- Initial delay: 1 second
Setting Up Dependencies
Heartbeat File
HEARTBEAT.md Format
The heartbeat file contains tasks for the agent to check:Auto-Generation
IfHEARTBEAT.md doesn’t exist, a default template is created on first run:
Service Lifecycle
Starting the Service
- Loads configuration
- Starts background ticker
- Executes first heartbeat after 1 second
- Runs subsequent checks at configured interval
Stopping the Service
- Gracefully stops ticker
- Waits for current heartbeat to complete
- Closes background goroutine
Checking Status
Execution Flow
1. Heartbeat Trigger
Every interval (e.g., 30 minutes):2. Prompt Building
Heartbeat readsHEARTBEAT.md and builds the prompt:
3. Handler Execution
The handler processes the heartbeat:4. Result Processing
Silent (HEARTBEAT_OK):Channel Resolution
Heartbeat uses the last active channel for notifications:State Management
Channel Validation
Internal channels are filtered:- telegram
- discord
- slack
- etc.
- cli
- system
- cron
- heartbeat
Advanced Usage
Multi-Task Heartbeats
Execute multiple checks in one heartbeat:Using Spawn Tool
For long-running tasks, use thespawn tool:
Agent’s heartbeat response:
- Heartbeat completes immediately (async)
- Subagent runs in background
- User notified when subagent finishes
Conditional Responses
Respond only when action is needed:- Call
exectool:df -h - Parse output
- If usage > 85%: spawn cleanup subagent
- If usage < 85%: return
HEARTBEAT_OK
Custom Intervals
Adjust interval based on task urgency:Logging
Heartbeat maintains a log file: Location:<workspace>/heartbeat.log
Format:
- INFO - Normal operations
- ERROR - Execution failures
Error Handling
Handler Errors
Missing HEARTBEAT.md
Channel Resolution Failure
Performance Considerations
Heartbeat Execution Time
- Simple checks: < 1 second
- With tools: 2-5 seconds
- With spawn: 1-2 seconds (async)
Resource Usage
- Memory: Minimal (single goroutine)
- CPU: Idle between ticks, active during execution
- Disk I/O: Read
HEARTBEAT.md, write logs
Optimization Tips
-
Use spawn for heavy tasks:
-
Combine related checks:
-
Adjust interval appropriately:
- Critical: 5-10 minutes
- Standard: 30 minutes
- Non-urgent: 60+ minutes
Best Practices
-
Keep tasks atomic:
- One clear objective per task
- Easy to validate completion
-
Use HEARTBEAT_OK liberally:
- Only notify user when action is needed
- Reduces notification fatigue
-
Leverage async operations:
- Use
spawnfor time-consuming tasks - Keep heartbeat execution fast
- Use
-
Document task expectations:
- Clear success criteria
- Failure handling instructions
-
Monitor heartbeat logs:
- Check for recurring errors
- Adjust tasks based on results
-
Test before deployment:
- Run manual heartbeat check
- Verify tool availability
- Confirm channel routing