Try free

Last updated 13 October 2026

Stop Claude Code from repeating the same test

Repetitive loops waste time and tokens

Claude Code often gets stuck in a cycle where it runs a test, sees it fail, and then runs the exact same Bash command again without changing any source code. This loop drains your credit balance and prevents the agent from making any actual progress. You watch the terminal scroll as the same error message appears for the fifth or sixth time. It usually happens because the agent believes a different outcome might occur or it has lost track of its previous attempt within the current context window.

Identifying this loop early saves money and reduces developer frustration. The agent might be using the Bash tool to run a test suite like npm test or pytest. When the test fails, the agent sometimes behaves as if it has fixed a file when it has not, or it simply repeats the command to verify the failure again. If you see the same command sequence more than twice without an Edit tool or Write tool being used in between, the agent is stuck in a logic loop.

Interrupting the agent with the Escape key

You can stop any running command by pressing the Escape key once. This immediately halts the Bash tool or whatever tool the agent is currently executing. If the agent is in the middle of a repetitive loop, this is your first line of defense. Once you press Escape, the agent stops its current task and waits for your input. This gives you a chance to look at the logs and see why the tests are failing before the agent spends more of your budget on a redundant attempt.

If the loop has already filled the screen with useless output, you can press the Escape key twice. This special shortcut allows you to go back to earlier messages in the conversation. You can select a point in time before the loop started and resume the session from there. This is often more effective than just stopping the current command because it removes the repetitive failures from the immediate context. By going back, you prevent the agent from being influenced by its own recent mistakes.

Clearing the context window with compact

A long session filled with repetitive test failures makes the agent less efficient. The context window becomes cluttered with the same error logs, making it harder for the model to reason about the actual bug. Use the /compact command to handle this situation. This command summarizes the conversation so far and removes the bulk of the raw tool output. It keeps the important details but frees up space for new reasoning by the agent.

After you run /compact, the agent has a cleaner view of the project state. It will no longer see the ten previous failed test runs in their full detail. This often breaks the loop because the agent is forced to look at the current state of the files rather than its own history of failure. It is a good practice to use this command whenever you notice the agent becoming sluggish or repetitive in its responses.

Defining test behavior in CLAUDE.md

You can prevent loops before they start by giving explicit instructions in the CLAUDE.md file. This file sits in your repo root and guides how the agent behaves across the entire project. Add a section about testing standards. Tell the agent that if a test fails twice with the same output, it must stop and ask for your guidance instead of trying again. You can also specify exactly which Bash commands to use for testing so the agent does not waste time guessing the right flags.

The agent reads CLAUDE.md at the start of every session and refers to it often. If you have a subdirectory with a specific testing framework, you can put a separate CLAUDE.md there to provide localized instructions. For example, you might instruct the agent to always use the Read tool on the failing test file before attempting a fix. This forces a logical step between the failure and the next attempt, breaking the cycle of blind repetition that leads to loops.

Forcing state awareness with the memory command

The /memory command is a powerful way to make the agent remember what it has already tried. If you notice a loop forming, use this command to view or edit the current memory of the project. You can manually add a note saying that a specific test is failing and that previous attempts to fix it with a certain approach have failed. This puts that information directly into the agent's long-term focus for the session.

By explicitly writing the failure history into memory, you change the agent's internal state. It no longer has to rely on the fluctuating context of the conversation. It sees the memory as a hard fact about the project. This prevents the agent from using the Bash tool to run the same failing test because it now knows the outcome is already documented. It encourages the agent to use the Edit tool or the Read tool to try a different approach.

Using permission modes to gate tool usage

Claude Code has different permission modes that change how much freedom the agent has to act on its own. The default mode asks for your permission for certain actions, but you can change this by pressing Shift+Tab inside the session. If the agent is looping, switch to the plan mode. In this mode, the agent describes what it wants to do without actually executing the tools. You can then review its plan and see if it is about to run the same failing test again.

If you want even more control, you can edit the settings.json file in the .claude folder. Under the permissions key, you can set specific tools to always require manual approval. For example, you can make the Bash tool require a prompt every time it runs a command. This stops the loop dead because the agent cannot proceed without you hitting a key. It turns the automated agent back into a guided assistant until you are sure the loop is broken.

Starting fresh with the clear command

Sometimes a session is too far gone to save. If the agent is stuck in a deep loop and /compact did not help, use the /clear command. This clears the current session history while keeping you in the same project folder. It is like a soft reset. The agent forgets the immediate conversation but retains its knowledge of the environment. You can then provide a fresh prompt that is more specific about the bug you are trying to fix.

Starting over removes all the confusing noise from the previous failed attempts. You can then use the claude -p flag to run a single, specific prompt if you want to avoid the interactive interface entirely for a moment. This helps you verify a specific fix without letting the agent take off on its own. A fresh start is often faster than trying to debug why the agent is stuck in a specific conversational loop.

Managing multiple test sessions with adhdTerminal

When you are dealing with complex test suites, you might need to run several debugging attempts at once. This is where adhdTerminal becomes useful for a developer. It allows you to run multiple Claude Code sessions side by side in a tiled interface. You can have one tile focused on fixing a specific unit test while another tile explores a different part of the codebase. Each tile tracks its own state, so a loop in one does not stop your progress in another.

The visual nature of adhdTerminal helps you spot loops across different tasks. Each tile shows the last few exchanges of the conversation. If you see two tiles showing the same repeated Bash output, you can instantly see the pattern. You can use the iPhone app to monitor these tiles while you step away from your desk. If a session gets stuck in a loop while you are away, you can send a new task or a prompt from your phone to break the cycle.

Updating and checking the environment

Loops are not always the fault of the agent's reasoning. Sometimes the environment itself is broken. Use the claude doctor command to check if your installation is healthy. This command verifies your permissions and connections. If the agent is trying to run a test but the underlying test runner is missing or misconfigured, it might keep trying because it thinks the error is temporary. A quick check of the environment can reveal these issues.

If everything looks correct but the behavior persists, run the claude update command. New versions of the command line agent often include improvements to how it handles repetitive tasks and tool failures. Keeping the tool updated ensures you have the latest logic for loop detection. If you use adhdTerminal, you can run the update command in one tile while your other project tiles stay active. This keeps your workflow moving even when you need to perform maintenance on the tool itself.

Questions people ask

How do I know if the agent is actually looping or just working hard?

Look at the tool output in the terminal. If you see the same Bash command and the same error message more than twice without any Edit or Write commands in between, it is a loop. A working agent will usually try to Read a file or change code after a failure.

Will the /clear command delete my CLAUDE.md or settings?

No, /clear only wipes the conversation history of the current session. Your CLAUDE.md file, your .claude/settings.json, and any code changes you have made remain untouched. It is a safe way to reset the agent's focus without losing your actual work.

Can I stop the agent from using the Bash tool entirely?

You can control this in your settings.json file under the permissions section. You can set the Bash tool to require manual approval for every use or deny it in certain modes. However, this will prevent the agent from running tests or searching the file system automatically.

Read next

More on when it goes wrong·All posts