Claude Code Stuck in a Loop? Restart It, Don't Debug It
Engineering
Claude Code Stuck in a Loop? Restart It, Don't Debug It
You've seen it. Claude Code reads the file, tries a fix, the test fails. It says "let me try a different approach" — and does the same thing. Reads the file again. Tries the fix again. Twenty minutes and eight turns later you're watching an AI thrash against the same wall, and you're typing longer and longer corrections into the box.
Stop typing. The correction isn't the fix. A fresh context is the fix. Here's why, and how to break the loop in about thirty seconds.
What a loop actually looks like
A stuck session has a signature:
- It re-reads files it read a few turns ago, as if for the first time.
- It re-runs the same command and gets the same error.
- It re-applies an edit that already failed — sometimes character for character.
- It announces a "different approach" that is, on inspection, the same approach.
- It oscillates: fixes A by breaking B, then fixes B by breaking A, forever.
If you're nodding, you're in a loop. And the turn count is not going down.
Why it happens
The loop isn't randomness, and it isn't the model being "dumb." It's the context.
Every failed attempt in this session is still in the conversation, and every turn feeds the whole conversation back through the model. So the eight things that didn't work are sitting in the prompt right beside your latest instruction, all competing for attention. Worse, if a wrong assumption slipped in early — it misread how your router works, it believes a helper exists that doesn't — that assumption is anchored in the context and quietly steering every attempt after it.
Your instinct is to explain harder. But explaining harder just adds another turn to the pile that's causing the loop. You're trying to out-shout a context that's mostly noise now. That's a fight you lose slowly.
The fix: break the context, not the code
Loops don't break by correction. They break by discontinuity — a clean context that doesn't contain the thrash.
- Clear the context.
/cleargives you an empty window in the same directory (yourCLAUDE.mdreloads automatically). The failed attempts leave the live context — they're still saved to disk if you ever want to look, but they stop poisoning the agent. - Re-prompt with the one thing it kept missing. The loop was usually circling a single fact. Lead with it: "Implement X. Note: the config loads lazily, so it's undefined at import time — read it inside the handler, not at the top of the module."
- Hand it the evidence up front. Paste the failing test output or the exact error into the opening message, so the fresh agent starts from ground truth instead of rediscovering it the hard way.
A clean session that opens with the lesson the loop taught you will clear in one or two turns what the loop couldn't in ten.
Better: don't let it loop at all
Most loops are a scope-and-context problem. You can prevent the majority:
- Scope smaller. "Refactor the auth module" invites thrash; "extract the token-refresh logic into its own function, don't change behavior" doesn't. Small, verifiable tasks loop far less.
- Plan before executing. Have Claude Code lay out its approach first and sanity-check it. A wrong plan is cheap to fix in words; a wrong plan discovered mid-execution becomes a loop.
- Give it the failing test with the task, not after three failed guesses.
- Isolate scoped work in a subagent. A subagent runs in its own separate context window and returns only a summary — so the sub-task's dead ends never touch your main session.
When it's not the agent
Sometimes a loop is telling you something true: your instructions are ambiguous, or your codebase contradicts itself. If a fresh, well-prompted session also loops on the same spot, the problem probably isn't the context — it's that the task is underspecified, two parts of the code disagree, or there simply isn't a clean solution to what you asked for. That's not a headshot problem; that's a "go read the code yourself for two minutes" problem. The loop was a signal — listen to it.
But nine times out of ten, it's just a poisoned context. Clear it, re-prompt with what you learned, and move on. And if the session is more than looping — if it's drifted off the goal entirely — that's when you shoot it in the head and start clean.
Keep reading: Shoot your Claude Code in the head: when to kill a session instead of saving it · /clear vs /compact vs kill: managing Claude Code's context window
Keep reading
View all dispatches →Shopify AI Agent Tools: What Your Agents Can Actually Do in Your Store
Clearly now ships Shopify store tools your AI agents can call — browse the catalog, read orders, roll up revenue, edit products, and attach generated images — over MCP and the beehaven CLI, with a dry-run preview on every write. Here are the tools, with example commands.
How to Manage Your Shopify Store with AI Agents
AI agents can now do the work of running a Shopify store — writing descriptions, generating product images, answering customers, updating the catalog. Here's how agent-run commerce works, and how to set it up.
Claude Code for Shopify: Run Your Store from Your Terminal
If you already run your codebase with Claude Code, the same agents can run your Shopify store — edit the catalog, generate product images, answer customers — with a connector and the right guardrails. Here's the setup.