Guides · Updated October 28, 2026

Codex stuck in a loop: how to stop it, undo it, and keep it from repeating

Codex reruns the same failing command, reads the same error, makes a small edit that changes nothing, and runs it again. Each attempt re-sends the whole chat, so on a ChatGPT plan a loop eats your 5-hour or weekly window, and on an API key it eats dollars. This guide covers how to stop a looping Codex CLI session, undo what it did, restart without walking back into it, and make Codex refuse the next no-progress repeat on its own.

What a Codex loop looks like

The common shape: nothing changes between attempts, so the next attempt can’t go differently. Run tests → fix → run tests is not a loop, and you don’t want to stop that.

Stop it now

Source: OpenAI’s Codex CLI command reference and slash commands. Then read the last few tool calls before typing anything. If the same command produced the same error three times, the next attempt needs new information, not another run.

Undo what the loop changed

The Codex CLI has no file checkpoint to rewind to. Pressing Esc twice on an empty composer lets you edit your previous message and fork the chat from that point, but that rewinds the conversation, not your files. For files, use git:

That is the practical case for committing before a long debugging task, or starting it with codex --worktree so the loop happens in a separate checkout.

Restart without walking back into it

Why “stop after 3 tries” in AGENTS.md doesn’t hold

An instruction in AGENTS.md is context, not a rule. The model can lose count, especially after compaction, and it can’t tell that npm run test | tail is the same run as npm test. A limit you can rely on runs outside the model, on every tool call, and can refuse it. Codex CLI has that: a PreToolUse hook runs before each tool call, and exiting with code 2 blocks the call and shows the reason on stderr to the model. PostToolUse hooks see each call’s result, which is how a guard can tell “same command, same output” from “same command, new output”.

Make Codex refuse the repeat: LoopGuard

LoopGuard is an open-source (MIT) CLI that installs those hooks. It needs no proxy and no API key, so it works on a ChatGPT plan as well as an API key. It has been tested against Codex CLI 0.163.

npm i -g loopguard-cli         # puts `loopguard` on your PATH
loopguard init --agent codex   # adds PreToolUse, PostToolUse and Stop hooks (original config backed up)
codex                          # then run /hooks and trust the three LoopGuard hooks
loopguard status               # caps, guarded agents, last 5 trips

Trust the hooks. Codex asks you to trust new hooks. Until you do, it skips them, and in codex exec it does so without saying anything. What LoopGuard does on a loop (version 0.1.3):

It also caps spend: LoopGuard reads the token counts Codex writes to ~/.codex/sessions/**/rollout-*.jsonl, prices them at API list prices, and denies tool calls past a per-session cap ($25 default), a daily cap across agents ($100), or an estimated $10 jump within 5 minutes. On a ChatGPT plan that dollar figure is an estimate of how heavy a session is, not your plan percentage (see Codex usage limits).

loopguard config set loop.maxRepeats 4         # stricter
loopguard config set sessionCapUsd 10          # estimated $ per session
loopguard pause 15                             # bypass enforcement for 15 minutes
loopguard report --agent codex --by session    # what recent sessions would have cost at API prices

What it won’t catch

Related

LoopGuard on GitHub · Pricing