A two-tier AI-powered monitoring system that provides cost-effective continuous monitoring with intelligent escalation.
┌─────────────────────────────────────────────────────────────┐
│ LAUNCHER.PY │
│ Orchestrates the system, manages process lifecycle │
└──────────────┬────────────────────────────┬─────────────────┘
│ │
▼ ▼
┌──────────────────┐ ┌──────────────────────┐
│ TIER 1: HAIKU │ │ TIER 2: OPUS │
│ Fast, Frequent │ │ Deep Analysis │
│ Basic Checks │ │ Decision Making │
└──────────────────┘ └──────────────────────┘
│ ▲
│ Exit Code 1 │
│ (Need Help) │
└────────────────────────────┘
┌────────────────────────┐
│ .coordination/ │
│ - blackboard.json │
│ - status.md │
│ - events.jsonl │
│ - *.log │
└────────────────────────┘
The Tiered Watcher Pattern solves the problem of continuous AI monitoring at scale:
- Problem: Running Opus constantly is expensive
- Solution: Use Haiku for frequent basic checks, escalate to Opus only when needed
- Benefit: 95% cost reduction while maintaining high-quality oversight
- Runs every 30 seconds
- Checks coordination files for issues
- Fast pattern recognition
- Low cost (~$0.001 per check)
- Exits with code 1 when it needs help
- Invoked only when Haiku requests intervention
- Deep analysis and decision-making
- Updates coordination state
- Higher cost but infrequent (~5-10x per day)
- Runs Haiku in a loop
- Monitors exit codes
- Invokes Opus when needed
- Handles graceful shutdown
- Logs all activity
# Using the convenience script
~/.claude/emergent-learning/scripts/start-watcher.sh
# Or directly
cd ~/.claude/emergent-learning
python watcher/launcher.py# Graceful shutdown (SIGINT)
Ctrl+C
# Or send SIGTERM
kill <launcher_pid># Watch the logs
tail -f ~/.claude/emergent-learning/.coordination/launcher.log
# Check Haiku logs
tail -f ~/.claude/emergent-learning/.coordination/haiku_watcher.log
# Check Opus logs
tail -f ~/.claude/emergent-learning/.coordination/opus_handler.log
# View current status
cat ~/.claude/emergent-learning/.coordination/status.mdEdit watcher/config.py to customize:
POLL_INTERVAL = 30 # Seconds between Haiku checks
HEARTBEAT_TIMEOUT = 120 # Seconds before considering watcher dead
MAX_RESTART_ATTEMPTS = 3 # Max consecutive restarts
LOG_RETENTION = 100 # Log entries to keep in memoryexport ANTHROPIC_API_KEY="your-api-key-here"watcher/
├── __init__.py # Package initialization
├── config.py # Configuration settings
├── launcher.py # Main orchestrator
├── haiku_watcher.py # Tier 1 watcher (created by Agent 2)
├── opus_handler.py # Tier 2 handler (created by Agent 3)
└── README.md # This file
.coordination/
├── blackboard.json # Shared state between agents
├── status.md # Current system status
├── events.jsonl # Event log
├── launcher.log # Launcher activity
├── haiku_watcher.log # Haiku checks and findings
└── opus_handler.log # Opus interventions
- 0: Normal exit (clean shutdown)
- 1: Intervention needed (Haiku requests Opus)
- 2: Error occurred (will retry)
Problem: ModuleNotFoundError or import errors
Solution:
cd ~/.claude/emergent-learning
pip install anthropic # or bun install if using JS versionProblem: ANTHROPIC_API_KEY not found
Solution:
export ANTHROPIC_API_KEY="your-key-here"
# Or add to ~/.bashrc or ~/.zshrcCheck the logs:
tail -50 ~/.claude/emergent-learning/.coordination/launcher.logCommon causes:
- Haiku watcher script has bugs
- Configuration files missing
- Permissions issues with .coordination directory
Verify Haiku is detecting issues:
cat ~/.claude/emergent-learning/.coordination/haiku_watcher.logHaiku should exit with code 1 when intervention is needed. If it's not detecting issues, check the detection logic in haiku_watcher.py.
Problem: Opus being called too frequently (high cost)
Solutions:
- Adjust Haiku detection thresholds (make it less sensitive)
- Increase
POLL_INTERVALto reduce check frequency - Review Haiku logic to ensure it's handling common issues independently
Problem: Log files consuming disk space
Solution: Implement log rotation
# Add to crontab
0 0 * * * find ~/.claude/emergent-learning/.coordination -name "*.log" -mtime +7 -deleteOr use the built-in LOG_RETENTION setting (keeps last N entries in memory).
Track API usage to ensure the tier system is providing expected savings:
# Check number of Haiku calls
grep "Starting check" ~/.claude/emergent-learning/.coordination/haiku_watcher.log | wc -l
# Check number of Opus calls
grep "Invoking Opus" ~/.claude/emergent-learning/.coordination/launcher.log | wc -l
# Expected ratio: 50:1 or higher (Haiku:Opus)If Opus is invoked too often:
- Make Haiku more autonomous (handle more cases)
- Increase intervention threshold
- Add more Haiku-level patterns
If Opus is rarely invoked:
- Lower intervention threshold
- Expand detection scope
Set up a weekly review:
# What issues did Haiku detect?
grep "ISSUE DETECTED" ~/.claude/emergent-learning/.coordination/haiku_watcher.log
# What decisions did Opus make?
grep "DECISION" ~/.claude/emergent-learning/.coordination/opus_handler.logThe .coordination/ directory contains valuable system state:
# Daily backup
tar -czf coordination-backup-$(date +%Y%m%d).tar.gz .coordination/Create /etc/systemd/system/watcher.service:
[Unit]
Description=Tiered Watcher Service
After=network.target
[Service]
Type=simple
User=youruser
WorkingDirectory=/home/youruser/.claude/emergent-learning
ExecStart=/usr/bin/python3 watcher/launcher.py
Restart=on-failure
RestartSec=10
[Install]
WantedBy=multi-user.targetEnable and start:
sudo systemctl enable watcher
sudo systemctl start watcher
sudo systemctl status watcherThe watcher can be extended to monitor any system. Modify haiku_watcher.py to:
- Check external APIs
- Monitor file systems
- Watch databases
- Track application metrics
The pattern remains the same: Haiku does fast checks, Opus handles complex decisions.
Haiku Tier:
- Checks per hour: 120 (every 30s)
- Checks per day: 2,880
- Cost per check: ~$0.001
- Daily cost: ~$2.88
Opus Tier:
- Interventions per day: ~10 (estimated)
- Cost per intervention: ~$0.10
- Daily cost: ~$1.00
Total daily cost: ~$3.88
Comparison:
- Running Opus every 30s: ~$288/day (75x more expensive)
- Savings:
$284/day ($8,500/month)
The launcher uses subprocess.Popen to run Haiku rather than threading:
Pros:
- Clean process isolation
- Easy restart on failure
- Clear exit code communication
- No shared state issues
Cons:
- Slightly higher overhead
- Requires IPC via files
Decision: Subprocess chosen for reliability and simplicity.
Haiku signals intervention needs via exit code 1:
Pros:
- Works across languages (Python, Bash, Node)
- Standard Unix pattern
- No need for complex IPC
Cons:
- Limited to ~255 distinct codes
- Less expressive than structured data
Decision: Exit codes chosen for simplicity and portability.
- Add metrics collection (Prometheus/StatsD)
- Web dashboard for monitoring
- Configurable intervention policies
- Multi-tier escalation (add Sonnet tier)
- Automatic cost optimization
- Pattern learning from interventions
When modifying the watcher system:
- Test both tiers independently first
- Verify graceful shutdown works
- Check log output is useful
- Document configuration changes
- Update this README
Part of the Emergent Learning Framework.