How I AI: Designing AI Agent Loops in Claude Code and Codex
I'm demystifying AI loops, showing you how to build two autonomous workflows in Claude Code and Codex to manage PRs and generate new agent skills automatically.
Claire Vo

A recent tweet from Peter at OpenClaw set the AI world on fire: prompts are out, and loops are in. He said if your agent isn't prompting itself, you’re behind the times. That one comment spun off a ton of content trying to explain what a loop is, and frankly, I don’t think any of them did a great job of making it clear.
So, I'm here to answer your questions in a safe space. What is a loop? Is it actually useful? And should I really be letting my agents prompt themselves? I think the answer is a resounding yes. Loops are incredibly useful, especially in software engineering, but it's not an all-or-nothing game. I still do plenty of manual prompting, so don’t worry if you’re not “loop maxing” just yet.
In the simplest terms, a loop is just a way to automate prompting. I wrote an early article on this idea called "Why OpenClaw Feels Alive Even Though It's Not", which explained that there are many ways to trigger an agent beyond just a human typing a message. These automated triggers are the foundation of loops:
- Heartbeat: A trigger on a recurring schedule, like every five minutes. Think of an agent that checks for new Jira tickets every five minutes.
- Cron: A trigger at a specific, defined time, like every Sunday at 9 PM.
- Hooks: A trigger based on an event, like a new email arriving (external webhook) or a tool being called within the agent's environment (internal lifecycle).
- Goal: A newer type of loop that’s become a first-class citizen in tools like Claude Code and Codex. You set a final outcome, and the agent runs autonomously, prompting itself until that outcome is achieved and validated.
To build effective loops, you need a few key components. I really like Addy Osmani's article on loop engineering, which outlines these essentials: an automation trigger, isolated work trees, reusable skills, plugins/connectors for tools, sub-agents for federating work, and state tracking. When you put it all together, you're essentially designing a job for an AI employee. Let's build a couple of these AI employees right now.
Workflow 1: Building a Daily PR Review Routine in Claude Code
First, let's tackle a common problem for product and engineering teams: pull requests that get stale. We ship a lot of code, and it's easy to lose track of PRs that are waiting for review or final checks. Instead of manually nagging people, I can build a routine in Claude Code to act as our team's diligent, and slightly mean, PR manager.

Step 1: Create the Routine
In Claude Code, these scheduled loops are called "Routines." I'll create a new one and name it daily aging PR review. The goal is to have it look for open PRs that have been sitting for more than 12 hours and get them ready for merge or alert the team.
I set the schedule for this routine to run daily. This is a classic "cron" style loop—it kicks off at a specific time every day without any manual intervention from me.
Step 2: Write the Prompt
The magic is in the instructions. You're not just writing a one-off prompt; you're defining a job description. I gave the agent a very specific set of tasks, including the ability to spawn its own sub-agents.
Look at the prs open on the chat PRD app app. If there are any prs open more than 12 hours. Please review their merge readiness. If there is anything that you can babysit, spin up a thread to babysit that PR until all merge checks are green. Otherwise send a slack in the product channel to the team about the open prs that are ready for approval and merge be mean when you send the slack.

This prompt gives the agent two possible paths:
- If a PR can be fixed automatically, it should spin up a new thread (a sub-agent) to handle it.
- If a PR needs human eyes, it should use the Slack connector to notify the team (and be a little sassy about it).
Step 3: Running the Loop
Once I kick off the routine, it runs completely on its own. It uses its connected tools (GitHub and Slack) to check the repository, identify aging PRs, and take action. I don't have to monitor it. The end result is that PRs get merged faster, and I get a helpful (and funny) summary in Slack. This is a perfect example of a simple, time-based loop that solves a real-world, recurring problem.

Workflow 2: A Meta-Automation to Generate Skills in Codex
Now for something a bit more advanced. This workflow is wonderfully meta: we're going to build a loop that analyzes our codebase to find opportunities to build new skills for our agents. A skill is just a reusable way to perform a common task, and having an agent that helps us build more of them is a huge force multiplier.
For this, I'm using Codex, which calls its loops "Automations." What I like about Codex is that it provides templates to get you started.
Step 1: Customize the Automation Prompt
I started with the template called "From recent PRs and reviews suggest next skills to deepen" and set it to run weekly on Fridays. Then, I added much more specific instructions to guide the agent.
My prompt enhancement focused on identifying command-line tools or test suites we've built and ensuring there's a corresponding agent skill for them. But here's where it gets interesting—I told the agent to validate its own findings.
If you identify a skill, spin up its own thread and use that skill... validated against the base branch of the repo. We want to confirm that the skill actually works and outputs high quality... You should use a goal when validating the skill.
This creates a loop within a loop. The main automation runs on a weekly schedule (a cron loop). When it identifies a potential new skill, it spawns a sub-agent with a goal (a goal-based loop) to test and validate that the new skill actually works.
Step 2: Watching the Sub-Agents Spin Up
After running the automation, Codex gets to work. It scans our GitHub history and quickly identifies a candidate: a command-line tool I built called chat smoke, CLI. Just as instructed, it spawns a dedicated sub-agent to validate it.

In the chat list, I can see the new agents being created, each with a specific task. One agent, G, is working on the smoke, CLI skill. Another, Galileo, is validating a skill for addressing GitHub comments. Each of these sub-agents is pursuing a goal and will loop on its task until the validation is complete.

This is the real power of agentic systems. We have a primary scheduled loop that acts as a manager, delegating validation tasks to a team of specialized sub-agents, who in turn use their own goal-based loops to get the job done. It's an autonomous system for improving our other autonomous systems.
A Few Words of Warning
This is all amazing, but there are a couple of pitfalls. First, loops can get expensive. An agent that runs on a schedule and spins up other agents that loop until a goal is met can burn through tokens very quickly if you're not careful. Monitor your costs!
Second, writing prompts for loops—especially goal-based loops—requires precision. Your success criteria and evaluation methods must be crystal clear, or the agent will either fail or loop endlessly without producing valuable output. For goal-based prompting, I always recommend consulting guides like OpenAI's documentation on Using Goals in Codex.
Even with those caveats, I think loops are a fundamental shift in how we work with AI. You can move from being a micromanager who types every prompt to a manager who designs jobs, sets schedules, and lets your team of agents run on their own. I can't wait to hear what kinds of loops you're building!
---
Brought to you by:
- WorkOS—Make your app enterprise-ready today
- Runway—The creative AI platform for images, video, and more


