
When an AI agent is stuck in a loop in Codex or Claude Code, make it stop after the same thing fails twice and change what you give it. The cause is usually missing context, no example of what great looks like, or direction that is too general. Every extra round uses up your business's plan.
I help established business owners build agents inside their companies, and I've seen people fight with their agents for hours and hours, saying the same prompt over and over, and it doesn't get better (video, 13:27). I've seen it with the smallest things and the biggest things.
In short: - If the same thing fails twice, stop the agent; repeating the same prompt does not make the next output better. - Replace vague feedback like "make it friendlier" with your exact reply to an email, three times tops. - Read the agent's first reply, because the cause (like Chrome not being installed) is often right there. - Paste the two-failure rule into your agents.md or claude.md (the rule book your agents read at the start of every session) so the agent stops itself and says what it needs.
Part of the series: How to Reduce Codex and Claude Code Token Usage: 20 Ways. This is way 15. Previous: On big jobs, have the agent look first and fix second · Next: Read what the agent says, then fix the root cause
15. When your AI agent is stuck in a loop, stop after two failures
Let's just take a simple draft email agent: an agent that is responsible for drafting your emails in your voice (13:27). Because the first draft it surfaces doesn't sound like them, people will say something like, "Oh, make it more professional." It comes back. It doesn't sound like them. "Make it more friendly." It comes back. "This doesn't sound like me. Make it more compassionate." It comes back and it still isn't good enough.
After about two to three of those "not good enough" rounds, you want to stop, because there's something wrong (13:57). If the agent isn't improving with every single prompt, instruction or piece of guidance you give it, and the next output is not better than the previous one, something is wrong.
It's usually that it doesn't have enough context, or it doesn't have an example of what great is, or the direction you're giving it is just too general (14:27). People will say, "Oh, that's not friendly enough," but what does that look like?

What a stuck agent costs a small business
Nobody at a small company watches the agents all night. If you skip this, the agent tries the same broken step again and again, quietly uses up your limit, and you find out when you run out. In my own weekly usage report, one of the rules it proposed for me was exactly this: do not repeat failed steps without a change. After two matching errors, find the cause (27:26). In that same report, the biggest leak of my week was client thumbnails that took about 20 tries, because I didn't have a defined reference photo and I didn't give it an example from the start (25:21).
How to get AI to write emails in your voice: give real examples
Instead of saying "this email doesn't sound like me," a better way is to say, "I would have replied to this email like this," and literally give it your exact reply. If you did that three times tops, it would start to sound like you 80% faster (14:27).
| Vague feedback | One real example | |
|---|---|---|
| What you say | "Write a reply to this customer in my voice." Then: "Make it sound more like me." Then: "Make it more friendly." | "Here are three ways I would have actually replied to this email." |
| What the agent learns | Nothing. "More like me" doesn't tell the agent anything | How your replies sound, how long they are and how you sign off |
| What happens next | The draft is still terrible, and an hour later you're still saying general, generic things | The next output improves at least 50 to 70%, and you'll say, "Wow, that's a huge improvement" |
| Video timestamp | 14:57 | 15:29 |

If you don't have an example yet, make the agent ask before it tries again:
What do you need to know to sound like me? List what is missing before you write another word.
Why Codex or Claude Code keeps getting stuck on a simple setting
This one sounds really simple (15:59):
- You: "Open my Chrome tab and pull the report."
- Agent: "Google Chrome isn't installed on this computer."
- You: "Just use my Chrome."
- Agent: tries again. Same answer.
You'd be surprised how many complicated things can be stuck on a simple setting, or on a simple thing that isn't installed on the computer. That happens because the human isn't paying attention and isn't reading what the agent is actually needing (15:59). The answer was in the agent's first reply. Fix that one thing (install Chrome, or tell it which browser you really use), then ask again.

The third loop, "Sorry, I can't do that," is covered in the next way: read what the agent says, then fix the root cause.
How to add the two-failure rule to your agents.md or claude.md
You need to know when to stop (15:29). Most owners don't watch their agents work, so make the agent catch the loop too. This is the block from my field guide. Paste it into the rule book your agents read at the start of every session (agents.md for Codex, claude.md for Claude Code; see way 7).
## Two-failure rule If the same action fails twice for essentially the same reason, stop. Do not try variations. Start your reply with "STOPPED:" and one plain-English sentence explaining why. Then report: 1. What you were trying to do 2. What went wrong, without technical jargon 3. The likely cause: system (login, permissions, rate limit, outage), file (missing or in the wrong place), or instruction (unclear, conflicting, or pointing to the wrong place) 4. What you need from me to continue If you think my instruction caused the problem, say so directly and suggest a clearer version I can approve.
One caution from our own builds: a stop rule only works if the agent is allowed to tell you. A client's agent once got stuck loading contacts for eight days because "I'm stuck" wasn't on the list of reasons it could contact the owner.
Your own stop checklist
- [ ] Has the output failed to improve twice in a row?
- [ ] Did I give a real example, or only an adjective like "friendlier"?
- [ ] Did I read what the agent said it needs?
- [ ] Is the cause the tool, the file, or my instructions?
Frequently asked questions
What should I do when my AI agent is stuck in a loop?
Stop it after the same thing fails twice, then give it more context, a real example of what great looks like, or more specific direction. Shanee says after about two to three "not good enough" rounds, something is wrong (13:57).
Why does Codex or Claude Code keep getting stuck on the same thing?
Often a simple setting is off or something isn't installed, and the person isn't reading what the agent says it needs. In Shanee's Chrome example, "just use my Chrome" got the same "Chrome isn't installed" answer; fix that one thing, then ask again.
How do I get AI to write emails in my voice?
Give the AI your real replies. Shanee says to tell it, "I would have replied to this email like this," and share your exact reply; three examples tops gets drafts sounding like you much faster than "make it friendlier."
What does a stuck AI agent cost a small business?
A stuck agent quietly uses up your limit, and nobody at a small company watches the agents all night. In Shanee's own weekly report, the biggest leak was client thumbnails that took about 20 tries because she hadn't given an example from the start.
Do I need a developer to stop an AI agent from looping?
No. The fix is in what you give Codex or Claude Code: real examples, the missing setting, or the plain-English two-failure rule in your agents.md or claude.md.
Related ways in this series
- Read what the agent says, then fix the root cause: the "Sorry, I can't do that" loop and how to stop mistakes repeating.
- When an agent creates something great, make that the standard: once a draft sounds like you, save it as the example.
- Create a really good agents.md or claude.md: where the two-failure rule lives.
- Build a weekly helper that finds waste and fixes your token usage: the report that flagged repeated failed steps in Shanee's own week.
- Why beginners fail with AI agents: more of the habits that keep agents from getting better.
- Back to the hub: How to Reduce Codex and Claude Code Token Usage: 20 Ways
Sources
Official / Primary Sources
- Shanee Moret, "20 Ways to Optimize How You Use Codex and Claude Code Part 2" (YouTube, 2026-10-03) , the email-draft loop, vague feedback versus real examples, the Chrome loop, and the two-matching-errors rule from her own weekly report.
- Shanee Moret, "20 Ways to Optimize Your Token Usage in Codex and Claude Code" (GrowthAcademy.Global field guide, 2026) , the two-failure rule text for agents.md, the "stop and ask" prompt and the eight-day stuck-agent example.