How to design AI agent loops: schedules, goals, and subagents in Claude Code and Codex

· Source: Lenny's Newsletter · Field: Technology & Digital — Artificial Intelligence & Machine Learning, Software Development & Engineering · Depth: Intermediate, extended

Summary

The content demystifies AI agent loops, explaining how agents can autonomously prompt themselves for various tasks, moving beyond human-triggered inputs. It details four primary loop types: heartbeats (scheduled tasks like every 5 minutes), crons (time-specific schedules like 9 a.m. daily), hooks (event-driven triggers like receiving an email), and the newer goal-based loops, which run an agent until a measurable outcome is validated or blocked. Effective loop design requires foundational components such as isolated work trees, reusable skills, external plugins/connectors, subagents for federated tasks, and state tracking. Practical examples include a Claude Code routine for daily aging PR review and an advanced Codex automation that identifies missing skills from code commits, then spawns goal-driven subagents to validate them. The discussion also highlights critical warnings regarding potential high token costs and the necessity of precise prompt engineering for goal-based loops.

Key takeaway

For AI Engineers or Product Managers seeking to automate repetitive development or operational tasks, embrace AI agent loops to move beyond manual prompting. Implement scheduled "heartbeat" or "cron" loops for routine checks, or leverage "goal-based" loops for tasks requiring iterative completion, like PR babysitting or skill validation. Be precise with your loop prompts and monitor token consumption closely to manage costs and ensure efficient, high-quality autonomous agent execution.

Key insights

AI agent loops enable autonomous, self-prompting workflows, extending automation beyond human-triggered inputs for continuous task execution.

Principles

Method

Define an automation (schedule/goal), provide instructions, configure access (connectors), and optionally integrate subagents for specific tasks or validation, such as in Claude Code or Codex.

In practice

Topics

Best for: AI Engineer, Software Engineer, AI Product Manager

Related on AIssential

Open in AIssential →

Editorial summary, takeaway, and curation by AIssential. Original article published by Lenny's Newsletter.