From Prompts to Loops: A Practical Guide to Building Agentic Workflows in Codex and Claude
By Nathaniel · Published 3 December 2026 · 8 min read
Facts checked: 3 December 2026.
A prompt asks once. A loop asks, checks the result against a test you wrote, and tries again until the test passes or a stop rule ends it. Add a schedule and the loop runs without you. Both Claude Code and Codex support this, and you can build your first one in an afternoon.
What is the difference between a prompt and a loop?
A prompt is one request and one answer. You read the answer, decide if it is good, and ask again. The checking happens in your head, and you are the only thing that keeps the work moving.
A loop moves the checking into the workflow. The agent does one step, runs a check you defined, reads the result, and decides the next step. It stops when the check passes or when a stop rule you set says enough. You come back to a finished result or to a clear note about where it got stuck.
What does a loop need to work?
Every working loop has the same five parts. Write them down before you start and most loops that go wrong would have worked.
Without a stop rule a loop can run for hours on the same mistake. Write the maximum number of rounds into the prompt every time.
How do I turn a prompt into a loop?
Take a task you already do with prompts. Add the five parts one at a time. This prompt works in both Claude Code and Codex, since both can run commands in your project and read the output.
A loop with a check and a stop rule
Goal: all tests in this project pass. Work in rounds. Each round: run the tests, pick the single most important failure, make the smallest change that could fix it, and run the tests again. After every round, add a line to LOOP-NOTES.md with what you tried and what happened. Stop when all tests pass, or after five rounds, or if two rounds in a row fail in the same way. When you stop, write what is left and what you suggest as the next step. Do not change the tests themselves to make them pass.
The last line matters more than it looks. An agent that is told to make a check pass will sometimes change the check. Say what it may not touch.
Not working in code? The same shape fits any work with a clear test. A weekly report is done when every section has a figure and a source. A page is ready when it passes your checklist for tone and links. Write the checklist, and the agent can run it.
A loop for writing work
Goal: a finished draft that meets every point on my checklist below. Checklist: [your list, for example: under 300 words, one clear ask, no jargon, every claim has a source]. Write the draft. Then check it against each point on the checklist and list which points fail. Rewrite only the failing parts. Repeat for up to four rounds. Save each version as draft-1, draft-2 and so on, and finish by telling me which checklist points still fail, if any.
How do I run loops in Claude?
Claude Code gives you four ways to repeat work, from quick to permanent. Pick the lightest one that does the job.
| Option | What it does | Use it when |
|---|---|---|
| Loop inside a task | Claude works in rounds and checks itself, as in the prompt above | You are at the keyboard and want a result now |
| /loop command | Repeats a prompt on an interval while your session is open. A session can hold up to 50 scheduled tasks, and they stop when the session ends | You want a check every few minutes while you work, such as watching a deploy |
| Desktop scheduled task | Runs a prompt on a schedule on your own computer, in the Claude Desktop app | A daily or weekly task that needs your files and tools |
| Routine | Runs in the cloud on a schedule or a trigger, even with your computer closed. Available on Pro, Max, Team and Enterprise plans. Create it with /schedule or at claude.ai/code/routines | A task that should keep running without you, such as a Monday morning summary |
Start with the loop inside a task. When it works three times in a row, move it to a schedule. Moving too early hides the bugs in the prompt behind a timer.
Ask Claude to schedule it
Take the loop we just built and turn it into a routine that runs every Monday at 08:00. It should write its result to a file and stop after five rounds. Show me the schedule and the prompt it will run before you save anything.
How do I run loops in Codex?
Codex has two routes. Automations live in the Codex app and suit people who want a schedule without scripting. The command line suits people who want to run a loop from a script or a CI job.
| Option | What it does | Use it when |
|---|---|---|
| Loop inside a task | Codex works in rounds and checks itself, as in the prompt above | You are at the keyboard |
| Automation in the app | Runs a task on a schedule. Thread automations return to an existing conversation, and project automations run against a project on your disk, either directly or in a separate worktree. Results land in an inbox, and a run with nothing to report is archived. The app must be running | Recurring reviews, morning briefs and weekly checks |
| codex exec | Runs Codex without a person in the chat, for scripts and CI. It starts read-only. Add --sandbox workspace-write to let it edit files, --json for machine-readable output, and resume --last to continue a run | A loop you want to start from a script, a cron job or a build pipeline |
Background runs act without you watching. OpenAI advises against full access for automations and suggests workspace-write with rules instead. Give a loop the least access that lets it finish.
Run the loop from the command line
codex exec --sandbox workspace-write "Goal: all tests pass. Work in rounds, run the tests each round, make the smallest fix, log each round in LOOP-NOTES.md, and stop after five rounds or when the same failure repeats twice. Do not edit the tests."
Why should a second agent do the checking?
An agent that grades its own work tends to approve it. The same model that wrote a flaw is the one reading it for flaws, so it often misses what it missed the first time.
Split the roles. One agent makes the change. A second agent, started fresh and without the first one's reasoning, checks the result against your tests and your checklist. Claude Code and Codex can both run helper agents for this. If you only have one agent, start a new session for the checking and give it only the result and the checklist.
A checker that starts fresh
You are reviewing someone else's work. Here is the goal and the checklist. Here is the result. Check each point on the checklist and answer pass or fail with one line of evidence for each. Do not fix anything. If any point fails, say which one and why.
What should the agent already know before the first round?
An agent forgets everything between runs. A loop that starts cold spends its first rounds guessing how your project works, and guesses wrong.
- Keep a project file the agent reads at the start, such as CLAUDE.md for Claude Code or AGENTS.md for Codex. Put in how to build, how to run the tests, your naming and tone, and what must never change.
- Define your words. If you write we, you and users in the file, say who each one is, so the agent does not mix up the person asking with the person it builds for.
- Keep a progress file the loop updates every round, as in the notes file above. A new run, or a new agent, picks up where the last one stopped.
What if I run several loops at once?
Two agents editing the same files cause the same trouble as two people editing the same files. Give each loop its own copy of the project. A git worktree is a second checkout of the same repository in its own folder, so each loop works on its own branch and nothing collides. Codex automations can run in a separate worktree for this reason.
How do I keep a loop safe?
- Give it the narrowest access that works. A loop that only reads and writes in one folder cannot damage anything elsewhere.
- Keep anything that leaves your computer behind your own yes: sending email, publishing, paying, deleting. Let the loop prepare the draft and stop there.
- Cap the rounds, and cap the cost if your plan has usage limits. A loop that runs all night uses your allowance all night.
- Add a circuit breaker: a maximum number of failed rounds in a row and a maximum running time. When either is reached, the loop stops, leaves the work untouched on its branch and tells you.
- Give the agent permission to say no. A short file that states what the project is for and what is out of scope lets the loop stop when a task falls outside it.
- Read the notes file after the first few runs. It tells you where the loop wastes rounds and where your prompt is vague.
- Work on a copy or a branch. If a round goes wrong, you throw the branch away.
Loops suit narrow tasks with a check that can be run. Decisions about direction, taste and risk stay with you. A loop that writes, reviews and publishes with nobody looking has no one to notice when the goal itself was wrong.
Which should I use, Codex or Claude?
Both can run a loop with a check and a stop rule, and both can run it on a schedule. The loop design is the same, so the prompt above moves between them unchanged.
Choose the one you or your team already have open, since the best loop is the one you run every week.
Want a loop built for your own work?
I set up your own AI agent in a hands-on session at home, at your office or at a café in the Stockholm area, and we build your first loop around a task from your week. After the session you run it yourself. See how the setup works or write to me directly.
Frequently asked questions
What is an agentic loop?
An agentic loop is a workflow where an AI agent does a step, checks the result against a test, and repeats until the test passes or a stop rule ends it. The check and the stop rule are what separate it from a series of prompts.
Can Claude Code run on a schedule when my computer is off?
Yes, with a routine. Routines run in the cloud on a schedule or trigger and are available on the Pro, Max, Team and Enterprise plans. The /loop command and local scheduled tasks need your session or your computer running.
Can Codex run on a schedule?
Yes. Automations in the Codex app run recurring tasks, and results arrive in an inbox. The app needs to be running and the project needs to be on your disk. For scripts and CI, use codex exec.
How do I stop a loop from running forever?
Write a stop rule into the prompt: a maximum number of rounds, and a condition for giving up such as the same failure twice in a row. Also ask the agent to write what it tried to a notes file in every round.
Do I need to be a developer?
No. A loop needs a goal, a check and a stop rule, and a checklist works as a check for writing, reports and reviews. The prompts in this guide work with plain text.
Sources
About the author
Nathaniel is a commercial operator in Stockholm with twenty years of building and commercialising companies. Nathaniel sets up AI agents for sole traders and small businesses.