Guides · Updated October 24, 2026

Cursor agent stuck in a loop: how to stop it, roll back, and keep it from repeating

Cursor’s agent “fixes” the bug, runs the test, gets the same failure, and fixes it again. Or it rewrites the same file back and forth. Cursor puts no limit on the number of tool calls the agent can make in a task, so nothing stops it but you. Each pass re-sends the growing chat, which burns your included usage first and then paid usage. This guide covers how to stop it, roll back what it did, restart without walking back into the same loop, and make the agent refuse the next pointless repeat on its own.

Signs the agent is looping, not iterating

Edit, run the test, see a different result, edit again: that is normal work, and you don’t want to stop it. A loop is the case where nothing changes between attempts, so the next attempt can’t go differently.

Stop it now

Before you type the next instruction, scroll back through the last few tool calls. If the same command produced the same error three times, the agent needs a new fact (the real error, a file it hasn’t read, a decision from you), not another run.

Roll back what the loop changed

Cursor creates checkpoints automatically before significant changes. Click a checkpoint in the chat timeline to preview the files at that point, then restore it, or use Restore Checkpoint on an earlier request. Three things to know:

Restart without walking back into it

Why a rule saying “stop after 3 tries” doesn’t hold

A line in .cursor/rules or AGENTS.md is context, not enforcement. The model can lose track of which attempt it is on in a long chat, and it won’t see that npm run test | tail is the same run as npm test. A limit you can rely on has to run outside the model, on every tool call, and be able to refuse one.

Cursor has exactly that: hooks. A preToolUse hook in ~/.cursor/hooks.json runs before every agent tool call and can return {"permission": "deny"} with a message the agent reads. A postToolUse hook sees each call’s result, which is how a guard can tell “same command, same output” from “same command, new output”. Cursor also caps how many automatic follow-ups a stop hook can trigger (loop_limit, default 5).

Make Cursor refuse the repeat: LoopGuard

LoopGuard is an open-source (MIT) CLI that installs those hooks. No proxy, no API key.

npm i -g loopguard-cli          # puts `loopguard` on your PATH
loopguard init --agent cursor   # adds preToolUse, postToolUse, postToolUseFailure and stop hooks
                                # to ~/.cursor/hooks.json (original backed up first)
loopguard status                # caps, guarded agents, last 5 trips

Restart Cursor (or reload the window) so it loads the hooks. What happens on a loop (version 0.1.3):

loopguard config set loop.maxRepeats 3      # stricter
loopguard config set sessionCapUsd 10       # estimated $ per chat
loopguard pause 15                          # bypass enforcement for 15 minutes
loopguard uninstall --agent cursor --restore   # put your original hooks.json back

Limits worth knowing

Related

LoopGuard on GitHub · Pricing