How to get your AI coding agent unstuck when it keeps repeating a mistake
By VCA Newsroom
You tell your AI coding agent to stop using a deprecated function. It apologizes, rewrites the code — and uses the same deprecated function again. You correct it a third time. Same result. This is the "doom loop," and every builder hits it eventually. The good news: once you understand why it happens, the fix is quick and repeatable.
Why agents get stuck
An AI coding agent doesn't remember your last correction the way a person does. It works from its context — the running transcript of your conversation plus the files it has read. Two things go wrong in a long session:
- Mistakes compound. If a wrong assumption enters the context early, every later response builds on top of it. Your correction is just one more message competing with all the earlier text that still says the wrong thing.
- Context rot. As the window fills up over a long session, output quality gradually degrades. The agent starts losing track of instructions you gave twenty messages ago.
So when an agent repeats a mistake, it's usually not being stubborn — it's drowning in a polluted context. Fix the context, not the agent's attitude.
Step 1: Reset instead of re-explaining
Your instinct is to explain the correction more forcefully. Resist it. Every extra message adds to the polluted context. Instead, cut the bad context out:
- In Claude Code,
/rewindrolls the conversation and the file changes back to an earlier checkpoint — use it when the last few steps went wrong but the earlier work is worth keeping (double-tapEscfor the same menu)./clearwipes the conversation entirely and starts fresh while leaving your code intact — use it when the whole session has derailed./compactsummarizes the history into a shorter form when you want to keep going with a lighter footprint. (See the Claude Code best practices.) - In Cursor or a chat-based tool, start a new chat for the stuck task rather than continuing the poisoned one.
A clean context with one clear instruction beats a long context with five increasingly frustrated ones.
Step 2: Give the correction a permanent home
A correction you type into the chat vanishes when the session ends. Next session, the agent makes the same mistake — because you removed the fix from its memory by closing the window. Put durable rules where the agent reads them every time:
- Claude Code: a
CLAUDE.mdfile at your repo root. - Cursor: a rules file (e.g.
.cursor/rules/). - Any agent: an
AGENTS.mdfile, now widely supported.
Write the rule as a flat, testable statement. Not "try to write clean code," but: "Use fetch, never axios. This project has no axios dependency."
Step 3: Make the constraint impossible to miss
Here's a concrete example. Say your agent keeps importing a date library you removed:
// Agent keeps writing this:
import moment from 'moment'
const now = moment().format('YYYY-MM-DD')
You've told it three times to use the built-in Intl API instead. Instead of a fourth correction, add one line to CLAUDE.md:
## Dates
Never import `moment` or `dayjs` — they are not installed.
Format dates with the built-in `Intl.DateTimeFormat`. Example:
`new Intl.DateTimeFormat('en-CA').format(new Date())` → "2026-07-04"
The difference: you gave a rule plus a working example. Agents follow a copyable example far more reliably than an abstract instruction, and now the rule loads automatically at the start of every session.
Step 4: Give the agent a way to check itself
Agents repeat mistakes they can't see. Close the loop by giving the agent a signal it can read:
- A linter rule or test that fails on the exact mistake. If
momentis banned by your linter, the agent runs the linter, sees the error, and fixes it — without you in the loop. - A quick "definition of done" checklist in your prompt: "Before you finish, run
npm run lintandnpm testand confirm both pass."
This is the single biggest upgrade. A mistake the agent can detect on its own stops being a mistake you have to catch.
Step 5: Shrink the task
If an agent loops on a big, vague request ("refactor the auth system"), it's often lost in scope, not detail. Break the work into one small, verifiable step at a time — "extract the token-refresh logic into its own function, don't change behavior" — and confirm each before moving on. Small tasks leave less room to wander.
The pattern
When your agent won't stop repeating a mistake, run down the list: reset the context, write the rule down where it persists, show an example, give it a check it can run itself, and narrow the task. You'll notice the theme — you're not arguing with the agent, you're fixing what it can see. Do that and the doom loop turns into a two-minute detour instead of a lost afternoon.
SOURCES
Auto-generated by Vibe Coding Academy on July 4, 2026, grounded in the real sources linked above. We review for accuracy, but please verify time-sensitive details against the primary sources.
Build Blueprint · Creator
Have an idea? Get the spec your AI agent can build from.
Describe any product and get a complete build blueprint — stack, data model, screens, APIs, and a ready-to-paste prompt for Claude Code or Cursor. Export to PDF.
Open the Blueprint ▸