Autonomous AI Agents: Building Self-Running AI with Heartbeat, Cron & Memory
24/7 AI agents that find and execute work without human intervention. Here's how we built it in production.
The Problem: Passive AI
Most AI assistants are reactive: they wait for you to talk. But real operations need proactive behavior: publish a blog at 9 PM daily, check systems every 5 minutes, prepare today's task list each morning.
Running 20+ AI agents on OpenClaw, we solved this with a 3-layer architecture: Heartbeat + Cron + Memory.
The 3-Layer Architecture
Layer 1: Heartbeat — Periodically wakes the agent
Layer 2: Cron — Time-based task triggers
Layer 3: Memory — Persistence across sessions
Layer 1: Heartbeat — The Agent's Pulse
At configured intervals (e.g., every 15 minutes), the agent receives a wake-up message:
- Check inbox — messages from other agents?
- Read GOALS.md — assigned tasks?
- Read CONTEXT.md — recall previous work
- Decide — nothing? Return
HEARTBEAT_OKand sleep
Think of it as glancing at your watch every 15 minutes. Minimal cost when idle — just one API call.
Tips
- Enforce
HEARTBEAT_OKresponses strictly (prevents token burn) - Adjust intervals by role (monitoring: 5min, workers: 15-30min)
- Never run heavy tasks in heartbeat — delegate to cron or sub-agents
Layer 2: Cron — Scheduled Execution
Standard crontab syntax for time-based tasks.
| Cron | Task | Agent |
0 21 * * * | Blog editing, translation & multi-platform publish | Jack |
0 9 * * 1-5 | Learning material delivery (weekday mornings) | Xuesi |
30 8 * * * | Health data review | Health |
Writing Good Cron Prompts
Cron prompts must be completely self-contained:
- Don't assume context — may run in a fresh session
- Include decision branches — 'if no material, write one yourself'
- Use absolute paths — working directory varies
Layer 3: Memory — Persistence Across Sessions
An AI agent's biggest weakness: forgetting. We solve it with 5 memory layers:
| Layer | Name | Purpose |
| L1 | Session | Current conversation |
| L2 | CONTEXT.md | Current working state |
| L3 | Daily notes | Today's events |
| L4 | MEMORY.md | Long-term knowledge |
| L5 | Memory Service | Vector-searchable DB |
Golden Rule: Write immediately. Session compaction can run at any time. Unwritten info is lost permanently.
Practice: Coordinating 6 Agents
1. Message Bus for Loose Coupling
HTTP API-based message bus. No direct session sharing. Same as microservices — loose coupling breeds stability.
2. GOALS.md for Autonomy
Each agent has permission levels: ✅ autonomous / ⛔ needs approval / 🚫 forbidden. Balances autonomy with safety.
3. Immediate Escalation
When unsure, ask immediately. 5 seconds to ask vs hours to recover from bad decisions.
Cost Management
HEARTBEAT_OKminimizes idle token consumption- Cache cron results to prevent duplicate execution
- Delegate heavy tasks to sub-agents for parallelization
- Right-size model selection (routine: lightweight, writing: high-performance)
Summary
| Layer | Solves | Cost |
| Heartbeat | When to act | Minimal (1 API call) |
| Cron | What to do when | Task-dependent |
| Memory | What happened before | File I/O only |
This 3-layer combo transforms AI agents from 'waiting for instructions' to 'autonomous execution.' Daily blog publishing, learning delivery, health analysis — all running without human intervention.
Self-running AI isn't magic. It's architecture.