โThis code works but it's a mess, and I'm scared to touch it.โ
Give the agent a measurable goal, a test baseline and a stopping rule. It makes one small change at a time and keeps only what passes.
Fix it yourself in 3 steps
Make sure tests (or at least a run script) pass before you start, and commit.
Give the agent a specific goal and the loop prompt below.
Review the diff and the agent's list of what it tried and rejected before merging.
Copy this prompt
Refactor [file/module] to [measurable goal, e.g. 'split the 600-line function into units under 80 lines']. First run the tests and record the baseline. Then make one small change at a time without changing public behavior, rerunning the same tests after each. Keep only changes that pass. Stop after 5 rounds, when the goal is met, or if a change needs my approval. Finish with the diff summary, test results, and what you tried and rejected.
Type your details in the boxes above the message, then tap Copy & open. Leave a box empty if you're not sure.
Tips from people who've done it
Huge agent-run refactors can cost thousands in usage and still need careful human review โ start with one module.
Yes, if you give it tests to check against. Make sure your tests pass and commit before you start, then have an agent like Claude Code make one small change at a time and keep only what still passes. Review the diff before you merge.
How do I stop an AI agent from rewriting too much code?
Give it a specific, measurable goal and a stopping rule, like 'stop when this file is under a set size and all tests pass.' Ask it to list what it tried and what it rejected. Small steps are easier to check and undo.
What if my code has no tests and I want AI to refactor it?
Ask the agent to write basic tests that capture how the code works today, before changing anything. If nothing else, have a run script that proves the app still starts and does its main job. Then refactor.
Anthropic's coding agent for your terminal, IDE and desktop: it reads the whole repo, runs commands and tests, edits files in place, and keeps a plan visible while it works โ strongest when you want Claude driving the change set instead of pasting snippets by hand.
A VS Code-style code editor built around AI: chat with your codebase, accept multi-line edits and hand bigger tasks to background agents. The comfortable choice if you live in an editor.
OpenAI's coding agent that runs in the CLI and in the cloud: you hand it a well-scoped task, it edits the repo, runs checks, and comes back with a pull request you review before merge โ best when the change is clear and you want a discrete handoff.
Open-source AI pair programmer in your terminal: it maps your repo, edits files from plain-English requests, runs tests when you ask, and commits each change to git. Works with many cloud and local models you already use.
2026-11-02Qodo Codex pluginQodo (via OpenAI) โ Codex marketplace plugin that pulls org codebase rules and cross-repo context into a coding session, then runs an independent Qodo review before you open a PR so local changes don't miss P0 breaks downstream. source