Skip to main content

Command Palette

Search for a command to run...

Autonomous AI Agents: Building Self-Running AI with Heartbeat, Cron & Memory

Published
3 min read

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:

  1. Check inbox — messages from other agents?
  2. Read GOALS.md — assigned tasks?
  3. Read CONTEXT.md — recall previous work
  4. Decide — nothing? Return HEARTBEAT_OK and sleep

Think of it as glancing at your watch every 15 minutes. Minimal cost when idle — just one API call.

Tips

  • Enforce HEARTBEAT_OK responses 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.

CronTaskAgent
0 21 * * *Blog editing, translation & multi-platform publishJack
0 9 * * 1-5Learning material delivery (weekday mornings)Xuesi
30 8 * * *Health data reviewHealth

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:

LayerNamePurpose
L1SessionCurrent conversation
L2CONTEXT.mdCurrent working state
L3Daily notesToday's events
L4MEMORY.mdLong-term knowledge
L5Memory ServiceVector-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_OK minimizes 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

LayerSolvesCost
HeartbeatWhen to actMinimal (1 API call)
CronWhat to do whenTask-dependent
MemoryWhat happened beforeFile 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.

More from this blog

A

techsfree

208 posts