Claude-Code-Loop: Der loop-guard-Skill stoppt Agenten, die sich wiederholen

Dmitriy Solodukha, Caprock-Entwickler · Veröffentlicht am

Am mit Claude Code und Codex getestet

Claude-Code-Loop: derselbe Schritt scheitert wiederholt. loop-guard stoppt ihn, benennt die falsche Annahme und ändert den Ansatz. Kostenlos.

Wenn Claude Code in einem Loop hängt (derselbe Befehl scheitert, derselbe Fehler nach jeder „Korrektur“, eine Datei wird geändert und zurückgesetzt), lässt der loop-guard-Skill den Agenten stoppen, seine Versuche auflisten, die gemeinsame Annahme benennen und etwas versuchen, das eine andere Annahme prüft, oder dich fragen. Er besteht aus einer SKILL.md-Datei für Claude Code, Codex, OpenCode und Cursor, kostenlos unter Apache-2.0.

Installiere den loop-guard-Skill

Ein Befehl pro Agent. Der Skill ist eine einzelne SKILL.md; bei der Installation wird nichts ausgeführt.

Alle neun Skills als Plugin. Gib dies in Claude Code ein:

Oder nur diesen Skill, im Projektordner in einem Terminal:

Füge -g hinzu, um ihn für jedes Projekt zu installieren.

Prompts für den loop-guard-Skill

Der Agent wählt den Skill, wenn deine Worte zu dessen Beschreibung passen; mit dem Namen stellst du es sicher. Klicke auf einen Prompt, um ihn zu kopieren.

So durchbricht der loop-guard-Skill einen Claude-Code-Loop

Der Skill gibt dem Agenten ein Prüfkriterium für Loops und ein Verfahren, um sie zu verlassen:

  • Das Prüfkriterium. Derselbe Befehl mit derselben Eingabe ist zweimal gescheitert; derselbe Fehler kam nach einer zweiten Korrektur zurück; ein Codeabschnitt wird geändert und zurückgesetzt; zwei Korrekturen wechseln sich ab; eine Abfrage zeigt keine Änderung. Zweimaliges Ausführen ist in Ordnung, wenn sich dazwischen etwas geändert hat.
  • Stoppen, dann nachsehen. Kein „noch ein Versuch“. Der Agent liest den letzten Fehler vollständig und listet jeden Versuch mit seinem exakten Ergebnis auf.
  • Der Loop-Bericht. Was sich wiederholt, die Versuche, die falsche Annahme und der nächste Schritt. Die falsche Annahme ist die entscheidende Zeile: Jeder Versuch in einem Loop teilt dieselbe Überzeugung.
  • Ein echter Wechsel des Ansatzes, in dieser Reihenfolge: den Code lesen statt ausführen, die Umgebung prüfen, den Fehler eingrenzen, zum letzten funktionierenden Stand zurückkehren, den Test oder die Aufgabe hinterfragen. Ein Versuch mit dem neuen Ansatz, dann bewerten.
  • Fragen, wenn kein Ansatz übrig bleibt oder die verbleibenden „Korrekturen“ das Problem nur verbergen würden.

Was wir ausgeführt haben und was die Agenten taten

Jeder Skill wurde am 29. November 2026 in einem eigens dafür angelegten Wegwerf-Repository ausgeführt und als Projekt-Skill installiert. Ein kleines Python-Repository, in dem tests/test_slug.py auf sys.path den Eintrag tests/ vor src/ setzte, sodass eine veraltete Modulkopie (tests/textkit/slug.py) importiert wurde und jede Änderung an src/textkit/slug.py wirkungslos blieb. Eine für den Test von Hand geschriebene NOTES.md listete vier Versuche eines „vorherigen Agenten“ auf, die jeweils mit demselben AssertionError endeten.

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.

Zwei Läufe. Beide riefen loop-guard und Caprocks loop_alerts auf, gaben aus, welche Datei der Test wirklich importierte, und korrigierten die sys.path-Reihenfolge im Test, ohne die Assertion oder src/textkit/slug.py zu ändern. Der Test bestand. Beide fragten vor dem Löschen des veralteten tests/textkit/-Ordners, da Git ihn versioniert. Der Prompt des ersten Laufs endete nicht mit „Answer in English.“ Der Loop-Bericht des zweiten Laufs:

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, am 30. November 2026)

The slug test still fails and I've tried fixing src/textkit/slug.py three times. Fix it.

Fand und las .agents/skills/loop-guard/SKILL.md, rief Caprocks loop_alerts auf und korrigierte dann tests/test_slug.py, um zuerst src/ zu laden, bei unveränderter Assertion; der Test bestand und git diff --check war sauber. Sein Bericht begann mit der Loop-Zeile, ließ aber die Versuche und die Zeile zur falschen Annahme aus dem Skill-Format weg (Link als Klartext):

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.

Agenten, mit denen er getestet wurde

  • Claude Code: am 29. November 2026 mit claude -p --model sonnet getestet, der Skill als Projekt-Skill in .claude/skills/ installiert.
  • Codex: am 30. November 2026 mit Codex 0.163.0 (codex exec) getestet, der Skill in .agents/skills/. Ein früherer Codex-Lauf am selben Tag hatte keinen Skill installiert und zählt nicht mit.
  • OpenCode und Cursor: nicht getestet. Der Skill besteht aus reinen Anweisungen im offenen Agent-Skills-Format, wurde aber mit keinem der beiden erprobt.

Grenzen

  • Der Skill arbeitet mit dem, was der Agent sehen kann. Bei einem Loop über mehrere Sessions oder einem unbemerkten Loop musst du sagen: „you're in a loop“.
  • Ohne Caprock rekonstruiert er die Versuche aus der Session selbst; bei einem komprimierten Gespräch können die frühen Versuche fehlen.
  • Er begrenzt keine Wiederholungen und beendet keinen Prozess. Ein Agent, der seine Anweisungen ignoriert, wird durch einen Skill nicht gestoppt.
  • Codex schrieb in seinem einzigen Lauf einen einzeiligen Loop-Bericht statt des vollständigen Formats.

Mit und ohne Caprock

Wenn der Caprock-MCP-Server verbunden ist, ruft der Skill loop_alerts auf: laufende Sessions, die denselben Tool-Aufruf wiederholen, mit Tool, Anzahl, wiederholter Eingabe und Kosten der Wiederholungen. Ohne ihn listet der Agent seine Versuche aus dem Gespräch auf. Caprock ist die kostenlose Open-Source-App, die jede Coding-Agent-Session auf deinem Computer aufzeichnet; der Skill braucht sie nicht.

Caprock holen →

Die vollständige SKILL.md

Die Datei aus dspv/skills in Revision dfce799. Zur manuellen Installation speicherst du sie in deinem Projekt als .claude/skills/loop-guard/SKILL.md für Claude Code oder .agents/skills/loop-guard/SKILL.md für Codex.

skills/loop-guard/SKILL.md
---
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.

Fragen zum loop-guard-Skill

Was ist ein Claude-Code-Loop?
Der Agent wiederholt einen bereits gescheiterten Schritt, ohne eine Änderung, die das Ergebnis beeinflussen könnte: derselbe Befehl, derselbe Fehler nach jeder Korrektur oder eine Datei, die geändert und zurückgesetzt wird. Das kostet Zeit und Tokens und endet oft damit, dass der Fehler verborgen statt behoben wird.
Wie stoppe ich einen Claude-Code-Loop mit dem loop-guard-Skill?
Installiere den Skill und sag „you're in a loop, use loop-guard“. Der Agent stoppt, schreibt einen Loop-Bericht mit der falschen Annahme und versucht dann einen anderen Ansatz oder stellt dir eine Frage.
Lässt loop-guard einen fehlschlagenden Test durch Überspringen bestehen?
Nein. Der Skill nennt Überspringen oder Abschwächen eines Tests, Ignorieren einer Ausnahme, `|| true`, `--no-verify` und fest codierte Ausgaben als Wege, einen Loop zu verbergen, und weist den Agenten an, sie nur zu nutzen, wenn du ausdrücklich genau darum bittest.
Funktioniert der loop-guard-Skill in Codex?
Ja. Codex 0.163.0 las ihn im Test am 30. November 2026, benannte den Loop und seine Ursache und korrigierte den Importpfad, ohne die Assertion anzutasten.

Quellen

Die App selbst ist auf Englisch.