# When Claude Code runs the same command 10 times, it's usually not the model. I tested 10 everyday commands, plus a hook that stops the 5th identical retry — retained record

> Retained reddit record from reddit:ClaudeCode.

Canonical: https://highsignal.app/data/records/04f957817d600c3a

[← sources](https://highsignal.app/data)


source: [reddit](https://highsignal.app/data/reddit) (reddit:ClaudeCode)  published 2026-10-29 13:34 ingested 2026-10-30 02:40

[original source ↗](https://www.reddit.com/r/ClaudeCode/comments/1xik4fk/when_claude_code_runs_the_same_command_10_times/)

## retained text



```
"Stuck in a loop" posts usually end with "switch models". In an r/opencodeCLI thread last week, the loop happened with three different models from three providers, mostly in fresh sessions. That points at what the agent is running, not who's running it.

I ran 10 everyday commands two ways: with plain pipes and no input, and in a pseudo-terminal nobody types into (8-second limit). In the terminal, 9 of 10 hung: `git log` (pager), `less`, `git commit` without `-m` (editor), `python3 -c 'input()'`, `npm init`, `git clone` from a server that wants a login (Username prompt), `vi`, a dev server and `tail -f`. With pipes, most failed fast instead, with errors like EOFError or "problem with the editor", which look like a reason to retry.

Three layers fix most of it:

1. **Make tools non-interactive.** `GIT_PAGER=cat`, `PAGER=cat`, `GIT_TERMINAL_PROMPT=0`, `GIT_EDITOR=true`. In Claude Code you can put them in the `env` block of `.claude/settings.json` so only the agent's shells get them.
2. **A stop rule in CLAUDE.md / AGENTS.md:** "If a command fails the same way twice, stop and say why before trying anything else. Never retry the same command a third time without changing something."
3. **A hook, because rules get forgotten.** A 142-line Python script (stdlib only) that fingerprints command + result (ignoring timestamps, durations, PIDs and hex). On the 3rd identical result it adds a warning to Claude's context. After the 4th, PreToolUse denies the next run of that command with a reason. A new prompt resets the count. Settings:

        {
          "hooks": {
            "PreToolUse":         [ { "matcher": "Bash", "hooks": [ { "type": "command", "command": "python3 \"${CLAUDE_PROJECT_DIR}/.agents/hooks/loop-guard.py\"" } ] } ],
            "PostToolUse":        [ { "matcher": "Bash", "hooks": [ { "type": "command", "command": "python3 \"${CLAUDE_PROJECT_DIR}/.agents/hooks/loop-guard.py\"" } ] } ],
            "PostToolUseFailure": [ { "matcher": "Bash", "hooks": [ { "type": "command", "command": "python3 \"${CLAUDE_PROJECT_DIR}/.agents/hooks/loop-guard.py\"" } ] } ],
            "UserPromptSubmit":   [ { "hooks": [ { "type": "command", "command": "python3 \"${CLAUDE_PROJECT_DIR}/.agents/hooks/loop-guard.py\"" } ] } ]
          }
        }

The same script also reads Codex's and Cursor's hook formats. Caveat: I tested it with 20 synthetic payloads in each tool's documented hook format, not inside a live session.

Why it matters for your limit: every retry re-sends the whole conversation as cache reads, so a loop of ten identical commands costs ten full requests for zero progress.

Full script, all three configs, the hang-test table and a five-minute diagnosis: https://medium.com/@r3dtuxedo/break-the-loop-5e05af62de55

I wrote the article.
```



document fetched 2026-10-30 00:43 · canonical https://reddit.com/r/ClaudeCode/comments/1xik4fk/when_claude_code_runs_the_same_command_10_times
