Detect a Claude Code or Codex agent stuck in a loop
A loop isn't a stall. The agent is busy and tokens are flowing, but nothing improves: the same test fails, the same fix goes in and comes back out, the same command runs again. It all looks alive, which is why it's easy to miss. Signs you can see from outside, a PreToolUse hook that blocks identical repeated tool calls (we ran it), and the loop a Stop hook can create, with its cap.
Lidnight stops runaway spend, silent approval stalls, and unexpected limit hits while you step away. Track Claude Code, Codex, Antigravity, Gemini and Kiro sessions, limits and cost, approve from your phone, and keep going with the lid closed.
Lidnight is a Mac app, so it can't be installed on this device. Open this address on your Mac to download it.
Copied. Paste it on your Mac to open it.
Couldn't copy. Tap the address below to select it, then copy.
What a loop looks like from outside
The same command or file edit shows up many times in a row. Test output is identical between attempts. Context grows while the diff stays the same. Tokens or spend climb with no new files touched.
Block identical repeated tool calls (Claude Code, Codex)
A PreToolUse hook receives the tool name and input as JSON and can block the call by printing a hookSpecificOutput object with permissionDecision: "deny" and a reason, which the model sees. This script keeps a fingerprint of each call per session and blocks when the last N calls (4 by default) are identical. If you don't have jq, install it with brew install jq.
#!/bin/bash
INPUT=$(cat)
SID=$(echo "$INPUT" | jq -r '.session_id')
SIG=$(echo "$INPUT" | jq -cS '[.tool_name, .tool_input]' | shasum | cut -d' ' -f1)
F="${TMPDIR:-/tmp}/loopguard-$SID"
echo "$SIG" >> "$F"
N=${LOOPGUARD_MAX:-4}
if [ "$(tail -n "$N" "$F" | sort -u | wc -l | tr -d ' ')" = "1" ] && [ "$(wc -l < "$F")" -ge "$N" ]; then
echo '{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"deny","permissionDecisionReason":"Same tool call repeated '"$N"' times. Stop and change approach or report that you are blocked."}}'
fi
exit 0Register it under hooks.PreToolUse in your settings (~/.claude/settings.json for Claude Code). Codex also takes PreToolUse hooks with the same output format, so the same script works there; new hooks only run after you trust them in /hooks (Codex hooks not running). We fed it the same input five times: the first three printed nothing, from the fourth on it printed the deny JSON, and a different input passed again. We didn't run it inside a live session, so try it on a throwaway project first.
Limits: it only catches exact repeats. A loop that changes one character each time, or alternates between two commands, gets through. For looser matching, widen the window or compare only the tool name and file path.
Know the Stop hook cap
A Stop hook that says “keep going until the tests pass” can create a loop of its own. Per the official docs, Claude Code overrides a Stop hook after it blocks eight times in a row with no tool call from Claude in between, and ends the turn with a warning. The hook input's stop_hook_active is true when Claude is already continuing because of a Stop hook, so check it and exit 0 to let Claude stop. If your hook truly needs more iterations, raise the cap with the CLAUDE_CODE_STOP_HOOK_BLOCK_CAP environment variable.
Put a hard end on headless runs
claude -p --max-turns N ends a run after N turns and --max-budget-usd at an estimated spend (both print mode only), so a loop costs at most the cap (stopping cost runaways). For Codex, run it under a time limit from your shell (timeout 2h codex exec …; if your macOS version has no timeout, use gtimeout from Homebrew coreutils) and use notify for a per-turn heartbeat. If heartbeats keep arriving but the repo has no new diff, that's a loop signal you can script.
# 거친 바깥 점검: 지난 30분 동안 git 상태가 바뀌었나?
git -C "$REPO" diff --stat | shasumWith Lidnight
Lidnight reads the last 20 steps of Claude Code and Codex session logs and sends “The same failure keeps repeating” when the same test or build command fails with the same error 3 times, the same command fails 4 times in a row with no file edits in between, or one file is edited 6+ times in 30 minutes while test failures don't go down. Improving failure counts don't trigger it, and error text isn't stored. For Claude Code sessions it also shows how to go back to a point before the failure with /rewind. While you're away it flags sessions burning tokens more than 3× faster than usual, and the service map shows repeated actions or token surges in red, where you can stop the session right there. No per-tool hooks to write. Mac notifications are free; phone alerts are Pro. Lidnight never ends a session on its own, so use the hook and caps above for a hard stop.
Checked 2026-11-28 against Claude Code's official docs (Hooks, Hooks guide, CLI reference) and the hooks code in the openai/codex repository. The hook script's output was checked by running it. Things change; check the docs.
Try Lidnight free
Apple Silicon Macs · macOS 13 or later. Every download includes a free trial of everything in Pro (14 days or 5 lid-closed runs of 10+ minutes, whichever comes first), no payment details needed. Pro is a one-time payment (1 Mac $9.99 · 3 Macs $14.99) with a 14-day refund.
Lidnight is a Mac app, so it can't be installed on this device. Open this address on your Mac to download it.
Copied. Paste it on your Mac to open it.
Couldn't copy. Tap the address below to select it, then copy.