Make Claude check its own work in a loop
Claude fixed a button in 9 tries. This guide: give it a check it can run, and a cap, so it loops until the check passes.
The fix: give it a check
Claude stops when the work looks done. Give it something that returns pass or fail and it will do the work, run the check, read the result and go again. Put the check in the prompt, with a limit.
Fix the failing test in src/button.test.ts.
After every change, run: npm test
Stop when it passes. If it still fails after
5 tries, stop and tell me what you learned.Check it worked
Ask Claude to show evidence, not a claim: the command it ran and what it returned. The best practices guide says to have Claude show the test output instead of asserting success. If the check could never fail, it proves nothing, so break the fix once and watch the check go red.
Pro tip: run the check automatically
A PostToolUse hook runs after every edit. If it exits 2, its stderr goes to Claude, which reads it and tries again. Use a fast check (types or one test file), because it runs after every edit. Add to .claude/settings.json, then run /hooks to see it.
{"hooks": {"PostToolUse": [{
"matcher": "Edit|Write",
"hooks": [{"type": "command",
"command": "out=$(npm test 2>&1) || { echo \"$out\" | tail -20 >&2; exit 2; }"
}]
}]}}Pro tip: unattended, with limits
claude -p runs one prompt and exits. In print mode, --max-turns stops the run with an error after that many agentic turns, and --max-budget-usd stops it once the estimated spend reaches the amount. --allowedTools pre-approves only what the loop needs.
claude -p "fix the failing test, run npm test" \
--max-turns 10 --max-budget-usd 2 \
--allowedTools "Read,Edit,Bash(npm test *)"Common mistake
A loop with no exit. Without a limit an agent that cannot pass the check keeps trying and keeps spending. Set a turn cap, and tell it what to do when the cap is near: stop and report. For a hard gate, a Stop hook that exits 2 blocks Claude from finishing until your check passes; Claude Code overrides it after 8 blocks in a row with no tool call between.
Official docs
One fix of twenty: the whole Claude Code survival kit.
Follow @whyshouldaicareA new disaster and its fix every day. Comment the word under a reel to get its guide.