What is Loop? How to Use It
What is Loop? How to Use It
A deep dive into Claude Code's autonomous execution mechanism — from basic principles to advanced design patterns
If you've been anywhere near the AI developer community lately, you've definitely heard the word "Loop" tossed around. At its simplest, the core idea looks something like this:
while :; do cat PROMPT.md | claude ; done
Or, put more cynically:
while [ haven't raised funding ]; do rebrand | tell a new story ; done
In practice, it's about automatically feeding a prompt file to Claude over and over. After each round, ask again, then act again. This used to be a completely manual, human-in-the-loop process. But with the rise of Agents, it's become a first-class feature.
In this article, drawing from Claude's official documentation, I'll walk you through everything you need to know.
What Is a Loop?
As defined by Claude Code's official docs:
A Loop is when an Agent repeatedly executes a work cycle until a stop condition is met.
That bash one-liner at the top is just a crude loop wrapped around Claude. But as Claude Code matured, they built a proper one internally. Every time you chat with Claude, this is what happens under the hood: evaluate whether the task can be done directly → decide whether to output or invoke a tool → if tools were invoked, evaluate the results → repeat until there are no more tools to call → deliver the final result.
Let's say you've got a project with a bug that won't run. You tell Claude Code: "Fix this for me, quickly." It might loop like this:
- Loop 1: Run the tests, find 3 failures
- Loop 2: Open the broken code and corresponding test files
- Loop 3: Edit the code, re-run tests — all 3 pass
- Loop 4: No more tools to call → tells you: "Fixed, all tests pass"
Of course, 4 loops is just an example. The actual number depends on the task. You can also add constraints: max 30 loops, or max $5 spent, etc.
The Four Types of Loops
You may have seen articles saying: "Stop writing prompts — design loops instead." What "designing loops" means is taking what a human used to do and planning which parts the AI should handle. Currently, there are roughly 4 types of loops, distinguished primarily by how much work you delegate to the AI:
1. Turn-based Loop
The overwhelming majority of our Claude interactions are turn-based: you send a message, it runs one round of the loop, returns a result, you check it, then send another message... round and round. In this setup, you are the human compiler. You're not really delegating anything.
For example, asking Claude to make a "Like" button. It reads the code, edits it, runs tests, and hands back something it "thinks" works. Whether it actually works? You still have to open the page and click it yourself.
The upgrade path is to delegate the acceptance step: write down exactly how you'd check it into a , and have Claude self-verify after every task.SKILL.md
For frontend acceptance testing, the delivery audit could look like:
- Spin up a local server, visually inspect the changed page
- Click through new controls, capture before/after screenshots
- Check browser console — no new errors allowed
- Run a performance audit
And add one more rule: if it fails, fix it yourself. Don't hand back a half-baked product. Quantified checks make it much easier for Claude to self-validate.
2. Goal-based: /goal
This is my favorite pattern: use to define what "done" means, and don't stop until the target is met./goal
Every time the Agent wants to clock out, another evaluation model steps in to verify. If it doesn't meet the bar, it gets sent back until the goal is reached (or your credits run out).
Deterministic standards work best here:
/goal get the homepage Lighthouse score to 90 or above,
stop after 5 tries.
3. Time-based: /loop
Scheduled tasks via are another form of Loop. For example, having your Agent check in every morning, or monitoring external systems for new tickets, PRs, or bug reports:/loop
/loop 5m check my PR, address review comments,
and fix failing CI
/loop runs on your local machine. To use it in the cloud, use to create a routine./schedule
4. Proactive Loop
With the human out of the loop, it becomes a proactive loop. Combine auto mode, dynamic workflows (research preview), and you can build a long-running, unattended Loop.
Claude Code's author Boris Cherny deleted his IDE in November last year and landed 259 PRs in 30 days, all written by Claude Code on its own. In a June interview, he said: "I don't prompt Claude anymore. A fleet of loops is running; they prompt Claude, they decide what to do next. My job is writing loops."
For example, to automatically handle user feedback, you could set up:
-
/schedule(research preview) runs a routine to periodically check for new reports -
/goaldefines what "handled" means, with a skill recording how to verify - Dynamic workflows orchestrate multiple Agents — triage, fix, review — each handling their segment
- Auto mode keeps the whole pipeline running without pausing for permission
Put together, a prompt might look like:
/schedule every hour: check #project-feedback for bug reports.
/goal: don't stop until every report found this run is triaged,
actioned, and responded to. When fixing a bug, use a workflow
to explore three solutions in parallel worktrees and have a
judge adversarially review them.
How to Ensure Quality
The quality of Loop-produced work depends on the systems surrounding Claude (notice how this ties back to "context engineering" and "harness engineering"), not on a human sitting in front of a keyboard. Here are 4 things worth investing in:
- The codebase itself must be clean. Claude follows the patterns and conventions already in your codebase.
- Give Claude self-verification tools. Encode what your team considers "good" into skills.
- Documentation must be accessible. Frameworks and libraries have docs with the latest best practices; make sure Claude can reach them.
-
Use a second Agent for code review. A reviewer with fresh context is more objective and won't be biased by the primary Agent's reasoning. Use the built-in skill or the GitHub Code Review.
/code-review
You'll notice these four things share a common thread: the loop must contain something that can say "no" — tests, type checks, real errors. Without that, the Agent just echoes itself in a feedback loop.
Also: if an intermediate result doesn't meet the standard, after fixing it, encode the fix into the system so future iterations benefit.
Token-Saving Tips
I have a hot take: loops are cyber-opioids. You try them once, and you never stop. Between loops, your context grows longer and longer; one "go get 'em!" might cost you a hundred bucks (ask me how I know...).
Claude's official docs offer six money-saving tips:
- Pick the right tool and model. Small tasks don't need multi-Agent setups or loops. Some tasks are fine with cheaper, faster models.
- Define success and stop criteria clearly. The more concrete you are about what "done" looks like, the faster Claude reaches it. (But don't set it too loose — early exit is also a problem.)
- Test the waters first. Dynamic workflows can spin up hundreds of Agents at once. Try a small slice of the work first to gauge usage.
- Write scripts for deterministic work. Running a script is cheaper than reasoning through steps. For example, a PDF skill that includes a form-filling script: Claude just runs it each time instead of re-deriving the code.
- Don't run scheduled tasks too frequently. Match the interval to the change frequency of whatever you're monitoring.
-
Monitor usage. You can check usage anytime and stop if something looks off. breaks down by skill, subagent, and MCP. and each show round counts and per-Agent costs.
/usage/goal/workflows
Getting Started
To get your feet wet with Loops, pick one task where you are the bottleneck. Then ask yourself three questions:
Once you've chosen your approach, boldly hand it off to the AI. Watch the results. Observe how it plans. Before long, your Loop skills will take off.