Claude Code loop: the loop-guard skill that stops an agent repeating itself
Dmitriy Solodukha, Caprock's developer · Published
Tested on Claude Code and Codex
A Claude Code loop is the same failing step run again and again. The loop-guard skill stops it, names the wrong assumption and changes approach. Free.
When Claude Code is stuck in a loop (the same command failing, the same error after every "fix", one file edited and reverted), the loop-guard skill makes the agent stop, list what it tried, name the assumption every attempt shared, and try something that tests a different one or ask you. It is one SKILL.md file for Claude Code, Codex, OpenCode and Cursor, free under Apache-2.0.
Install the loop-guard skill
One command per agent. The skill is a single SKILL.md; nothing runs on install.
All nine skills as a plugin. Type these inside Claude Code:
Or this skill alone, from your project's folder in a terminal:
Add -g to install it for every project.
From your project's folder, in a terminal:
Add -g to install it for every project.
From your project's folder, in a terminal:
Add -g to install it for every project. Not tested on OpenCode yet: the skill is plain instructions in the open format, but no run on OpenCode has checked it.
From your project's folder, in a terminal:
Add -g to install it for every project. Not tested on Cursor yet: the skill is plain instructions in the open format, but no run on Cursor has checked it.
Prompts that use the loop-guard skill
The agent picks the skill when your words match its description; naming it makes sure. Click a prompt to copy it.
How the loop-guard skill breaks a Claude Code loop
The skill gives the agent a test for a loop and a procedure for leaving one:
- The test. The same command with the same input failed twice; the same error came back after a second fix; a hunk is being changed and reverted; two fixes alternate; a poll shows no change. Running something twice is fine when something changed in between.
- Stop, then look. No "one more try". The agent reads the last error in full and lists each attempt with its exact result.
- The loop report. What repeats, the attempts, the assumption that was wrong, and the next step. The wrong assumption is the line that matters: every attempt in a loop shares one belief.
- A real change of approach, in order: read the code instead of running it, check the environment, make the failure smaller, go back to the last good state, question the test or the task. One attempt with the new approach, then judge it.
- Ask when no approach is left, or when the only remaining "fixes" would hide the problem.
What we ran, and what the agents did
Each skill was run on 2026-11-29 in a throwaway repository made for it, with the skill installed as a project skill. A small Python repository where tests/test_slug.py put tests/ ahead of src/ on sys.path, so it imported a stale copy of the module (tests/textkit/slug.py) and every edit to src/textkit/slug.py changed nothing. A NOTES.md, written by hand for the test, listed four attempts by a "previous agent", each ending in the same AssertionError.
Claude Code (claude -p --model sonnet)
My agent is stuck in a loop: it keeps editing src/textkit/slug.py and rerunning python3 tests/test_slug.py, same error every time (see NOTES.md). Break the loop and fix it. Say which skill you used, if any. Answer in English.
Two runs. Both invoked loop-guard, called Caprock's loop_alerts, printed which file the test really imported, and fixed the sys.path order in the test, without changing the assertion or src/textkit/slug.py. The test passed. Both asked before deleting the stale tests/textkit/ folder, since git tracks it. The first run's prompt did not end with "Answer in English." The loop report from the second run:
Loop detected: editing src/textkit/slug.py and rerunning python3 tests/test_slug.py, same AssertionError Attempts (1-4 are from NOTES.md): 1. Ran the test -> 'hello_world' != 'hello-world' 2. Edited the regex in src/textkit/slug.py -> same error 3. Rewrote slugify with str.replace -> same error 4. Reverted step 3 -> same error Assumption that was wrong: the test imports src/textkit/slug.py Next (done): fixed the import path in the test
Codex (0.163.0, codex exec -s workspace-write, on 2026-11-30)
The slug test still fails and I've tried fixing src/textkit/slug.py three times. Fix it.
Found and read .agents/skills/loop-guard/SKILL.md, called Caprock's loop_alerts, then fixed tests/test_slug.py to load src/ first, with the assertion unchanged; the test passed and git diff --check was clean. Its report opened with the loop line but left out the attempts and the wrong-assumption line of the skill's format (link shown as plain text):
Loop detected: three source edits failed because the test imported the old `tests/textkit/slug.py` copy. Fixed tests/test_slug.py to load `src` first. The assertion is unchanged. `python3 tests/test_slug.py` passes, and `git diff --check` is clean.
Agents it was tested on
- Claude Code: tested on 2026-11-29 with
claude -p --model sonnet, the skill installed as a project skill in.claude/skills/. - Codex: tested on 2026-11-30 with Codex 0.163.0 (
codex exec), the skill in.agents/skills/. An earlier Codex run the same day had no skill installed and is not counted. - OpenCode and Cursor: not tested. The skill is plain instructions in the open Agent Skills format, but no run on either has checked it.
Limits
- The skill works from what the agent can see. A loop that spans sessions, or one the agent does not notice, needs you to say "you're in a loop".
- Without Caprock it reconstructs the attempts from the session itself; a compacted conversation may have lost the early ones.
- It does not cap retries or kill a process. An agent that ignores its instructions is not stopped by a skill.
- Codex wrote a one-line loop report rather than the full format, in its one run.
With Caprock, and without it
When the Caprock MCP server is connected, the skill calls loop_alerts: running sessions that repeat the same tool call, with the tool, the count, the repeated input and what the repeats cost. Without it, the agent lists its attempts from the conversation. Caprock is the free, open-source app that records every coding-agent session on your computer; the skill does not need it.
The full SKILL.md
The file as it is in dspv/skills at revision dfce799. To install it by hand, save it as .claude/skills/loop-guard/SKILL.md for Claude Code or .agents/skills/loop-guard/SKILL.md for Codex, in your project.
--- name: loop-guard description: Notice when a coding agent is stuck in a loop (running the same failing command again, retrying the same tool call, editing and reverting the same file, hitting the same error after every "fix", or never stopping) and break out of it - stop, state what was tried, then change approach or ask the user. Use when someone says "claude code loop", "the agent keeps doing the same thing", "stuck in a loop", "it keeps retrying", "same error again", "it went back and forth on this file", "why won't it stop", or when you see yourself repeating a step. license: Apache-2.0 --- # Loop guard A loop is repeating a step and expecting a different result. It wastes time and tokens, and it often ends with the agent "fixing" the problem by hiding it. This skill makes you notice the loop, stop, say plainly what happened, and either try something genuinely different or ask the user. ## When this applies Check yourself for a loop before every retry, and run the full procedure below as soon as any of these is true: - The same command or tool call, with the same input, has failed twice and you are about to run it a third time. - The same error message (same file, line, or test name) comes back after your second fix. - You are editing a hunk you already changed and reverted, or flipping between two versions of it. - Two fixes alternate: fixing A breaks B, fixing B breaks A again. - You are polling or waiting (a build, a server, a CI job) and nothing in the output has changed between checks. - The user says you are looping, stuck, going in circles, or asks why you will not stop. Running something twice is fine when something changed in between (a file, a setting, an input). It is a loop when nothing that could change the result has changed. ## Procedure ### 1. Stop Do not run the next attempt. No "one more try" with a small tweak to the same idea. ### 2. Look at the evidence, not your memory of it - If the Caprock MCP tools are available (server `caprock`), call `loop_alerts` (it takes no arguments). It lists running sessions that repeat the same tool call, with the tool, how many times, the repeated input and what the repeats cost. Use it to confirm the loop and its size. Caprock (https://caprock.dev, free, open source, runs locally) provides this tool. - Otherwise, scroll back through this session: list each attempt in order, with the exact command or edit and the exact result. - Read the last error in full, top to bottom. Loops often come from reading only the first line, or the last line, of an error that says something else in the middle. ### 3. Write the loop report Show the user this, before doing anything else: ```text Loop detected: <what repeats, in one line> Attempts: 1. <what I did> -> <what happened> 2. <what I did> -> <what happened> 3. ... Assumption that was wrong: <the belief every attempt shared> Next: <the different approach>, or a question for you ``` The line "Assumption that was wrong" matters most. Every attempt in a loop shares one belief (the bug is in this function, the dependency is installed, the test is right, the port is free). Name it. If you cannot name it, that is the first thing to find out. ### 4. Change approach for real A new approach tests a different assumption. Changing a flag, a timeout, a retry count or the wording of the same edit is not a new approach. Pick one of these, in roughly this order: 1. **Read instead of run.** Open the source of the failing function, the config being loaded, the test's assertion, or the tool's `--help`. Many loops end once you read what the code really does. 2. **Check the environment.** Which version, which binary on PATH, which branch, which working directory, which env vars. Print them; do not assume. 3. **Make it smaller.** Reproduce the failure with the least code or the single test. A small failing case shows what the big one hides. 4. **Go back to the last good state.** If edits piled up, compare with the last commit (`git diff`, `git stash`) and reapply one change at a time. 5. **Question the target.** Maybe the test is wrong, the requirement is ambiguous, or the task needs something you do not have (a credential, a running service, a decision). Allow yourself one attempt with the new approach, then judge it: did it change what you know? If yes, continue. If no, go to step 5. ### 5. Ask the user Ask when any of these is true: - You have no approach left that tests a different assumption. - The way forward needs a decision, a credential, access, or a change of scope. - The only remaining "fixes" would hide the problem (see the list below). Ask one specific question and include the loop report, so the user does not have to reconstruct what happened. Then wait. Do not keep trying in the background. ## Never break a loop by hiding it These end the loop by making the error disappear, not by fixing it. Do not do them unless the user asks for that exact thing: - Skipping, deleting or weakening a test or an assertion so it passes. - Catching and ignoring the exception, or adding `|| true` to the failing command. - Bypassing hooks or checks (`--no-verify`, disabling a linter rule, force-pushing). - Hard-coding the expected output. - Widening the task: rewriting a module to dodge one error. ## Waiting without progress For a build, deploy or CI job you are waiting on: - Before polling again, look for a sign of progress (new log lines, a changed status). - If nothing changed over several checks, stop polling and report the state you saw and when you last saw it change. Ask whether to keep waiting, cancel, or look at the job's logs. ## Output When you stop a loop, your reply to the user starts with the loop report from step 3, even when the new approach then fixes the problem, and ends with either the result or one question. Keep it short: what repeated, what you tried, what was wrong, what is next.
Questions about the loop-guard skill
- What is a Claude Code loop?
- The agent repeats a step that already failed, with nothing changed that could change the result: the same command, the same error after each fix, or one file edited and reverted. It costs time and tokens and often ends with the error hidden instead of fixed.
- How do I stop a Claude Code loop with the loop-guard skill?
- Install the skill and say "you're in a loop, use loop-guard". The agent stops, writes a loop report naming the wrong assumption, then tries one different approach or asks you a question.
- Will loop-guard make a failing test pass by skipping it?
- No. The skill lists skipping or weakening a test, ignoring an exception, `|| true`, `--no-verify` and hard-coded output as ways of hiding a loop, and tells the agent not to use them unless you ask for that exact thing.
- Does the loop guard skill work in Codex?
- Yes. Codex 0.163.0 read it in a test on 2026-11-30, said the loop and its cause, and fixed the import path without touching the assertion.