Skip to content
How I AIHow I AI

How to write AI agent loops in Claude Code and Codex

I break down every loop type from scratch—what a heartbeat, cron, hook, and goal loop actually are, when each one fits, and the five things any effective loop needs before it touches production. Then I build two live loops: a daily aging-PR reviewer in Claude Code that schedules itself at 10:15 a.m. and spins off its own subagents, and a weekly skills-identification loop in Codex that spawns goal-based subagents to validate its own output in real time. *What you’ll learn:* 1. The plain-English definition of a loop—and why it’s just an automated prompt, not a scary new paradigm 2. The four loop types (heartbeat, cron, hook, and goal) and when each one actually fits your workflow 3. How to think about loop design using the “onboarding an employee” mental model 4. The five things every effective loop needs: work trees, skills, plugins/connectors, subagents, and state tracking 5. How to build a scheduled PR-review routine in Claude Code that babysits aging PRs and alerts your team 6. How to set up a weekly skills-identification automation in Codex that spawns its own validating subagents 7. Why goal-based loops are the hardest to write well—and where most people burn tokens for nothing 8. The two warning signs that your loop is going to get expensive before it gets useful *Brought to you by:* WorkOS—Make your app enterprise-ready today: https://workos.com?utm_source=lennys_howiai&utm_medium=podcast&utm_campaign=q22025 Runway—The creative AI platform for images, video, and more: https://runwayml.com/howIAI *In this episode, we cover:* (00:00) Prompts are out and loops are in (02:30) Defining a loop (03:03) The four ways to automate a prompt: heartbeat, cron, hooks, and goals (06:03) Five things every effective loop needs (09:26) The “onboarding an employee” framework for designing loops (11:58) Live build #1: Daily aging PR loop in Claude Code (17:08) Subagents inside loops (19:00) Live build #2: Weekly skills identification loop in Codex (22:57) Watching subagents spin up in real time (25:28) Warning signals around loops (27:31) What listeners are doing with loops *Blog and detailed workflow walkthroughs from this episode:* How I AI: Designing AI Agent Loops in Claude Code and Codex: https://www.chatprd.ai/how-i-ai/how-i-ai-designing-ai-agent-loops-in-claude-code-and-codex ↳ Build a Self-Improving AI to Generate Agent Skills in Codex: https://www.chatprd.ai/how-i-ai/workflows/build-a-self-improving-ai-to-generate-agent-skills-in-codex ↳ Automate Daily Pull Request Reviews with a Claude Code Agent: https://www.chatprd.ai/how-i-ai/workflows/automate-daily-pull-request-reviews-with-a-claude-code-agent *Tools referenced:* • Claude Code: https://claude.ai/code • Codex: https://chatgpt.com/codex • OpenClaw: https://openclaw.ai/ *Other references:* • Claire’s article “Why OpenClaw Feels Alive Even Though It’s Not”: https://x.com/clairevo/article/2017741569521271175 • Addy Osmani’s article on loop engineering: https://addyosmani.com/blog/loop-engineering/ • Using Goals in Codex: https://developers.openai.com/cookbook/examples/codex/using_goals_in_codex *Where to find Claire Vo:* ChatPRD: https://www.chatprd.ai/ Website: https://clairevo.com/ LinkedIn: https://www.linkedin.com/in/clairevo/ X: https://x.com/clairevo _Production and marketing by https://penname.co/._ _For inquiries about sponsoring the podcast, email jordan@penname.co.)_

Claire Vohost
Jun 17, 202629mWatch on YouTube ↗

CHAPTERS

  1. 0:00 – 0:30

    Why “loops” beat manual prompting for AI agents

    Claire frames the shift from one-off prompts to agent loops: automations where an agent can trigger and guide itself. She sets expectations for what will be covered (definitions, when loops help, and pitfalls) and positions loops as especially valuable in software engineering workflows.

    • Prompts vs. loops: why self-prompting via automation matters
    • What viewers will learn: definitions, how to write prompts/loops, pitfalls
    • Loops as a way to get agents doing work without human turn-taking
    • Practical focus: Claude Code and Codex examples later in the episode
  2. 0:30 – 1:32

    Enterprise reality check: secure access is the bottleneck (WorkOS sponsor)

    The sponsor segment explains that AI tools only work well with deep access to company systems, which triggers enterprise security requirements. WorkOS is presented as an API layer to quickly add enterprise-ready features like auth, access control, and audit logs.

    • AI copilots/chatbots need deep access to codebases and internal docs
    • Enterprise buyers require security: auth, access controls, audit logs
    • Building enterprise features in-house is costly and slow
    • WorkOS offers drop-in APIs to accelerate enterprise readiness
  3. 1:32 – 3:34

    Demystifying loops: automated prompting vs. human messages

    Claire responds to hype-driven loop discourse by defining loops in plain terms and emphasizing accessibility. She distinguishes typical chat-based prompting (human messages) from loops (automated prompting), while noting both can be useful.

    • Loop hype vs. practical understanding—making it approachable
    • Message-based prompting is human-triggered and still useful
    • A loop is about automation: the agent gets prompted without you typing
    • Goal: help people decide when loops are worth it
  4. 3:34 – 6:05

    The 4 automation trigger types: heartbeat, cron, hooks, and goals

    Claire catalogs common ways to trigger AI work, borrowing from pre-AI automation patterns. She introduces “goals” as a newer first-class loop type in Claude Code and Codex, where an agent iterates until a measurable outcome is validated or it becomes blocked.

    • Heartbeat: run every N minutes/hours to check and act
    • Cron: run at specific times/days (e.g., Sundays, 9:00 AM)
    • Hooks/webhooks: trigger from lifecycle events or external signals (e.g., email received)
    • Goals: run until an outcome is validated or the agent is blocked
  5. 6:05 – 9:08

    What effective loops need: automation + tooling + structure

    Using Addy Osmani’s framing, Claire breaks down the ingredients of a high-functioning loop system and maps them to Codex and Claude Code features. She also highlights operational supports that keep loop work clean and conflict-free, especially in engineering repos.

    • Loop foundations: automation definition + scheduling + execution surface
    • Codex: Automations tab and templates; Claude Code: scheduled tasks/routines
    • /goal support and hooks/integrations are central in both tools
    • Operational necessities: worktrees, skills, plugins/connectors, subagents, state tracking
  6. 9:08 – 12:09

    Design loops like onboarding an employee (job-to-be-done framing)

    Claire offers a mental model: writing a loop is like hiring and onboarding a worker with a recurring responsibility. She gives examples across roles (executive assistant and software engineer) including a goal-based loop tied to CI checks and standards.

    • Think like a manager: define the job, cadence, and outcomes
    • Example: weekly calendar review/follow-ups loop for an EA
    • Example: hourly issue triage → code → PR loop for an engineer
    • Goal loop example: iterate on a PR until checks/lints/standards are green
  7. 12:09 – 13:40

    Getting started with simple scheduled loops (Claude “morning briefing”)

    To lower the barrier, Claire points out that many users already run loops via scheduled tasks like Claude’s morning briefing. She positions scheduled tasks (and Claude Code routines) as “loop 101” before tackling more advanced goal-based setups.

    • Scheduled tasks are loops: recurring trigger + self-prompting + completion
    • Morning briefing as an everyday, non-technical loop example
    • Good entry point: Claude scheduled tasks / Claude Code routines
    • Contrast: scheduled loops vs. goal loops (which run until validated)
  8. 13:40 – 16:43

    Live build #1 (Claude Code): Daily aging PR review loop

    Claire creates a Claude Code routine called “Daily Aging PR Review” to reduce PR babysitting. The loop checks for PRs older than 12 hours, evaluates merge readiness, spins off threads to babysit checks to green, or posts a (deliberately mean) Slack reminder.

    • Routine runs locally or in the cloud; she chooses local execution
    • Logic: find PRs open >12 hours and assess merge readiness
    • If needed, spawn a thread/subagent to babysit PR checks until green
    • Otherwise, notify the team via Slack about PRs ready to merge
  9. 16:43 – 19:15

    Subagents and connectors in practice: Slack/GitHub + multi-thread babysitting

    Claire explains how the PR loop works autonomously after kickoff and clarifies the success criteria: either babysit PRs to green checks or send Slack updates. A quick hiccup (“No Slack MCP surfaced”) demonstrates the dependency on properly enabled connectors.

    • Automation runs without continuous human monitoring once started
    • Two outcomes/success criteria: babysit checks or message the team
    • Subagents/threads let monitoring work happen outside the main thread
    • Connectors matter: Slack must be enabled/configured for notifications
  10. 19:15 – 20:16

    Codex loop ergonomics: automation templates as inspiration

    Switching to Codex, Claire highlights templates as a learning aid for designing loops. She selects a template that analyzes recent PRs and reviews to recommend skills to deepen—turning engineering output into structured training/playbook improvements.

    • Codex calls loops “automations” and provides reusable templates
    • Templates help users learn loop patterns and good prompting structure
    • Selected template: derive skill recommendations from recent PRs/reviews
    • Goal: make recommendations grounded, specific, and actionable
  11. 20:16 – 22:49

    Live build #2 (Codex): Weekly skill identification + goal-based validation subagents

    Claire customizes the Codex automation with constraints around tool/CLI/MCP validation skills, then instructs it to spin up subthreads for each proposed skill. Each subagent is told to validate the skill against the base branch using a specific goal, creating a layered loop system.

    • Schedule: weekly (Fridays) to ensure enough data for analysis
    • Bias toward skills that validate work via CLI tools/MCPs and test suites
    • For each identified skill: spawn a subagent thread to test it in practice
    • Use goal loops inside subagents for measurable validation against base branch
  12. 22:49 – 25:21

    Watching subagents spin up in real time (Gauss, Galileo)

    As the automation runs, Claire observes Codex creating memory/scaffolding and launching named subagents to validate different skills. The subagents pursue explicit goals (e.g., validating a smoke CLI skill) and iterate until validation is complete, demonstrating parallelization and self-checking behavior.

    • Codex shows automation scaffolding and uses its own memory structure
    • Subagents appear as separate chats/threads with distinct responsibilities
    • Example skills validated: chat smoke CLI; addressing GitHub comment workflows
    • Goal pursuit: subagents loop until the validation objective is satisfied
  13. 25:21 – 27:22

    Warning signals: cost blow-ups and sloppy goal criteria

    Claire closes with cautions: loops can burn tokens quickly, especially when they trigger broad work, spawn subagents, and run until success thresholds are met. She stresses that goal-based loops require precise success/evaluation criteria and recommends using established goal-writing guidance.

    • Loops can be expensive: persistent iteration + subagents + validation runs
    • Thin/unclear validation criteria leads to wasted tokens and poor results
    • Goal prompting is a distinct skill—precision matters more than chat prompts
    • Monitor loops for both cost and efficiency; consider OpenAI goal-writing guidance
  14. 27:22 – 29:06

    Where audiences apply loops + final recap and call for comments

    Claire lists additional loop-friendly use cases beyond PRs and skills—like morning briefings, inbox cleanup, and research that branches into subagents. She reiterates the core definition of a loop and invites viewers to share what they’re building (or whether they think loops are overkill).

    • Use cases: briefings, Gmail cleanup, research with branching subagents
    • Core loop idea: schedule or goal-driven iteration until done/validated
    • Loops can prompt other loops—composability enables “teams” of agents
    • Call to action: share loop ideas and feedback; like/subscribe/review

Get more out of YouTube videos.

High quality summaries for YouTube videos. Accurate transcripts to search & find moments. Powered by ChatGPT & Claude AI.