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
- Same command, same output.
npm testfails with the identical error three runs in a row, and no file changed in between. - Same command, reworded.
npm test, thennpm run test 2>&1 | tail -40, thennpx vitest runon the same file. It looks like a new attempt and is the same one. - Edit ping-pong. The agent applies a change, then reverts it a few steps later, then applies it again.
- “I see the issue now.” Every few steps, followed by the same fix.
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
- In the editor: click Stop in the agent panel. Cursor’s docs also describe steering instead of stopping (Agent overview): press
Cmd+Enter(Ctrl+Enteron Windows and Linux) to send a message immediately, and it is attached to the current tool results instead of waiting in the queue. PlainEnteronly queues it for after the current task, which is too late for a loop. - In the CLI (
cursor-agent): pressingEnterwhile the agent works steers the run at a safe boundary; pressingEnteragain interrupts the turn. If the session is running a/goal,Ctrl+Cpauses the goal. - Check for a
/goalor/loop. A goal keeps the agent working until it decides the objective is met, and/loopre-runs a prompt on an interval. Both are useful, and both will happily keep going in circles overnight. Stop the loop explicitly when you want it to end.
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:
- Restoring reverts files only. The looping messages stay in the conversation, so the agent still “remembers” its failed attempts and may repeat them. Restore, then start a new chat (below).
- Checkpoints are local and separate from git. Cursor says to use them for undoing agent changes and git for everything else.
- Commands aren’t undone. A checkpoint snapshots files the agent modified. If the loop ran a migration, installed packages or wrote files through a script, check
git statusand clean up with git. Committing or stashing before a long debugging task makes this a one-liner.
Restart without walking back into it
- Get a handoff note first. Ask: “Before we stop: in five lines, write the goal, what you tried, the exact failing output, and your best hypothesis.” Then open a new chat and paste it. A fresh chat is also much cheaper per step than a long one.
- Paste the real error. Copy the exact terminal output; don’t paraphrase it. Loops often come from the agent reading a truncated tail of the output and missing the line that matters.
- Ban the rerun. “Don’t run the tests again until you have changed the code and can explain why the change fixes this failure.”
- Narrow the scope. “Only edit
src/auth/session.ts.” Ping-pong between two files stops when one of them is off limits. - Switch models deliberately. A different model sometimes sees the problem the first one keeps missing, but on The X Subscription, Claude and GPT models in Cursor bill at API list price from the first request (see limiting Cursor agent costs), so don’t let a loop continue on the expensive one.
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 tripsRestart Cursor (or reload the window) so it loads the hooks. What happens on a loop (version 0.1.3):
- Only no-progress repeats count. The same tool call 5 times within 10 minutes is denied (
loop.maxRepeats5,loop.windowSec600), but only if nothing changed in between. LoopGuard fingerprints the git working tree on each call and hashes each tool’s output (timestamps, durations and temp paths stripped). If a file changed or the output changed (3 failed→2 failed), the count starts over, so test → fix → test keeps working. - Cosmetic rewording doesn’t reset it.
npm test,npm run test 2>&1 | tail -50,bash -lc 'npm test'andtimeout 300 npm testcount as the same call. Different tools stay different:npx jestandnpm testare not merged. - The deny message tells the agent what to do instead: don’t rerun it through a different command, write down the goal, what it tried, the exact failing output and its hypotheses, and find the root cause before running it again.
- Spend caps too. An estimated $10 jump within 5 minutes, or crossing the per-chat ($25) or daily ($100) cap, denies every further tool call. Cursor’s hooks have no documented “end the chat” field, so the agent can still reply in text, but it can’t run commands or edit files.
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
- Cursor support is early. It has been tested in a live
cursor-agentsession; the Cursor editor itself hasn’t been tested live yet. Loop detection doesn’t depend on cost data, so it is the more dependable part today. Details in limiting Cursor agent costs. - Cloud agents aren’t covered. They only load project hooks (
.cursor/hooks.jsonin the repo), not~/.cursor/hooks.json. - Output that changes every run for reasons the normalizer doesn’t strip (random seeds, shuffled test order) reads as progress. Outside a git repo, only the output signal is used.
- Keep Cursor’s monthly spend limit set. Hooks are a second line of defense, not a replacement for the billing-side limit.
Related
- How to stop runaway Claude Code, Codex and Cursor agent loops
- How to track AI coding agent token costs per session
- How to set a spending limit for Claude Code
- How to limit Cursor agent usage costs
- How to cap OpenAI Codex CLI spend
- Claude Code usage limit reached: why it happens and how to monitor it
- How to set a hard budget for AI coding agents in CI
- Claude Code stuck in a loop: how to break it and stop it repeating the same command
- Codex usage limit reached: 5-hour and weekly limits explained
- Why Claude Code uses so many tokens, and how to use fewer
- How to set a budget limit for Aider
- GitHub Copilot agent mode stuck in a loop: how to stop it and keep it from repeating
- Cline stuck in a loop: how to stop it, roll back, and cap what it spends
- Gemini CLI usage limits: daily quotas, the June 2026 change, and keeping costs in check
- Codex stuck in a loop: how to stop it, undo it, and keep it from repeating