OmniscioGuidesPromptsRefactoring
Claude Code prompt for refactoring
The hard part of a refactor is not the rewriting. It is proving that nothing changed, on a diff you can actually read. A prompt that pins the behaviour first and asks for small steps gets both; a prompt that says "clean this up" gets neither.
A refactor is a claim that nothing changed. The prompt below makes that claim checkable before the first edit, which is the only point at which being wrong is cheap.
- The prompt, ready to copy.
- Why each line is there.
- The plan-mode version, for the bigger ones.
- What good output looks like.
- The common failure.
- Keep it for next time.
Checked against Anthropic's Claude Code documentation on November 25, 2026. By the Omniscio team. Updated .
The prompt
Replace the file and the export names with yours, and name a test runner if the project has none — the prompt cannot pick one for you. The structure is the part worth keeping.
Refactor src/utils/format.ts so each function does one thing, without changing its exported behaviour. Keep every export name and every function signature the same.
Before you touch the file: write a test that pins the current output of every export in that file, run it, and show me it passes.
Then refactor one function at a time, running that test after each change. If the test fails, stop and tell me what changed instead of adjusting the test.
If you find behaviour that looks like a bug, write it down in a list and leave it alone. Do not fix it in this change.Four instructions, four different jobs: what to change, what must not change, how it will be proved, and what to do with everything else it notices along the way.
Why each line is there
"Refactor src/utils/format.ts so each function does one thing"
The outcome, not the steps. One step of Anthropic's refactoring recipe has this shape — name the change, then name the constraint. You do not have to describe how to split the function; deciding that is the work you are paying for.
"without changing its exported behaviour"
Anthropic's example states it the same way: modern features, same behaviour.
The constraint is what makes it a refactor rather than a rewrite. A refactor with this line in it can be reviewed by reading the diff; one without it has to be reviewed by re-deriving every expectation the old code encoded.
"Keep every export name and every function signature the same"
Behaviour can be identical and still break every caller, because a renamed export is a compile error in someone else's file. Spelling out both — the name and the signature — also removes the ambiguity about default parameters and optional arguments, which is where "the same behaviour" quietly stops being true.
"Before you touch the file: write a test that pins the current output … show me it passes"
Anthropic's framing is that Claude stops when the work looks done, and a check that comes back pass or fail lets it close that loop itself — it makes the change, runs the check, and fixes what broke. A characterisation test turns "I believe nothing changed" into a pass or fail.
Asking it to run that test before the edits is what makes it a record rather than a guess. Note that it records what the code does, not what it should do: a bug found here is pinned too, and belongs on the list rather than in the fix.
"one function at a time, running that test after each change"
Small, testable increments are what Anthropic's refactoring tips recommend, and they are also what makes the review possible: a failure in step three is caused by step three. Done as one pass, a failing test tells you only that one of forty edits broke something.
"instead of adjusting the test"
Name the move you do not want, not just the one you do. The cheapest way to make a failing characterisation test pass is to change its expectation, and that is exactly the move that hides the behaviour change you were trying to catch. Saying it out loud costs six words and closes the loop.
"write it down in a list and leave it alone"
Refactors surface things. A suspicious null check, an off-by-one nobody noticed, a log line with a secret in it. Every one of them is a decision that belongs in its own change with its own test, and a list at the end is the cheapest way to keep them without doing them now.
The plan-mode version
For a refactor across more than a couple of files, run this in plan mode. Plan mode tells Claude to research and propose changes without making them: it reads files and writes a plan, and makes no edits until you approve. That is exactly the shape you want for a change whose cost is mostly in how far it spreads.
Two ways in: press Shift+Tab until the status bar reads plan mode, or prefix a single prompt with /plan. From a shell, claude --permission-mode plan starts there.
What the plan should contain, and what to ask for if it does not:
Plan this refactor without editing anything. List every file you would change,
in the order you would change them, and for each one the decision it forces —
what stays, what moves, and which caller has to change with it. Flag any file
where the change would alter behaviour, and stop there if there is one.Once you approve it, paste the prompt from the top of this page to do the work. Its first step is the characterisation test, so ask for that step to appear in the plan: a plan that skips it has skipped the proof that the refactor preserved anything.
When the plan comes back, the approve prompt offers to start in auto mode, to approve each edit manually, or to keep planning. Ctrl+G opens the plan in your editor so you can edit it directly before Claude proceeds, which is often faster than describing the change in chat.
Why not a subagent
A subagent is the right tool when the work returns a short answer and would flood your conversation otherwise. A refactor is the opposite: it edits files, it needs the conventions and constraints already in your conversation, and its output is the diff itself. Anthropic's own list of what subagents cost includes the point that a fresh subagent first has to gather context your conversation already had. Run this one where the context is.
What good output looks like
- The test comes first, with its run. The characterisation test, and the line showing it passed against the untouched code.
- The exports are identical. Diff the export list before and after; a rename is a bug in a refactor even when the behaviour is perfect.
- One function per step. A sequence of small changes, each with the test result after it, not a single 400-line diff with a summary at the end.
- A list at the bottom. The suspicious things it found, named and not fixed.
If the reply says the tests pass without the command and its output, ask for those two lines. All tests pass on its own tells you very little, and where you were not watching the run it is the only evidence that behaviour was preserved.
The common failure
The refactor does more than it was asked to. Two things usually come with it:
- Improvement drift. It fixes the bug it found, renames an export to match the new structure, changes a return type to be cleaner, and adds a guard for a case that cannot happen. Every one is defensible and together they make the diff unreviewable, because now you cannot tell which line preserved behaviour and which changed it.
- The test that adapts. The characterisation test fails mid-refactor, and the expectation is updated to match the new output. The suite is green and the behaviour changed.
Both come from a prompt that names the destination but not the fence. Anthropic's recipe pairs the change with the constraint in the same way: use ES2024 features while maintaining the same behavior. The same page's tip is the other half — do it in small, testable increments, so that the moment behaviour moves, the step you are looking at is the one that moved it.
Planning catches the same failure earlier and more cheaply. A plan that proposes a behaviour change is the improvement drift above, caught while it still costs you a sentence to reject rather than a rewrite.
A third failure is quieter: the refactor that ends with the test suite in a worse state than it started, because the old tests were rewritten to match the new structure. If a test had to change, that is a behaviour change wearing a refactor's clothes, and it belongs in its own change with its own explanation.
Keep it for next time
Refactoring the same shape of code repeatedly is a sign the prompt should be a skill: a SKILL.md saved under .claude/skills/<name>/, runnable as /skill-name, whose body loads only when it is used. Put your project's own rules in it — the export conventions, the test command, the files that must not be touched —.
The other reusable place is CLAUDE.md, which every session reads at the start. The commands that confirm work — the test, the typecheck, the lint — belong there, because they are the one thing that is true of every task, not only a refactor.
Worked examples of both are on this site: the skills examples and the CLAUDE.md examples. For the step-by-step version of the same job, the everyday tasks guide covers it end to end.
How this page was written
Everything here about how plan mode behaves, what a check that returns pass or fail does, and what Anthropic recommends for a refactor was read from Anthropic's Claude Code documentation on November 25, 2026, and checked against the wording in the Sources below.
The prompt is ours: an example written for this site, following the patterns Anthropic documents, not a prompt Anthropic publishes. The file and function names are placeholders. Nothing here claims a measured improvement to any codebase.
Sources
- Anthropic's Common workflows, for the refactoring recipe and the tip about small, testable increments, read November 25, 2026.
- Anthropic's Best practices for Claude Code, for writing a specific prompt, giving Claude a check that returns pass or fail, asking for evidence, what subagents cost, and the CLAUDE.md commands, read November 25, 2026.
- Anthropic's Permission modes, for what plan mode does, how to enter it, and the approve options, read November 25, 2026.
- Anthropic's Create custom subagents, for when a subagent is the right tool and what it costs, read November 25, 2026.
- Anthropic's Extend Claude with skills, for where a skill lives and when its body loads, read November 25, 2026.
- Google's autocomplete suggestions for "claude code prompt for refactoring" and "claude code refactor prompt", read November 25, 2026, for the queries this page answers.
Common questions
What is a good Claude Code prompt for refactoring?
One that says behaviour must not change, pins the current behaviour in a test before the first edit, and moves one function at a time. The prompt at the top of this page is an example you can copy and point at your own file.
How do I stop Claude changing behaviour while refactoring?
Pin the behaviour first. Ask for a test that records what the code outputs today, have it run and pass before any edit, and require the same test after each step. A refactor is only a refactor if that test still passes and was not edited to make it pass.
Should I use plan mode for a refactor?
For a refactor across more than a couple of files, yes. Plan mode tells Claude to research and propose changes without making them, so the plan arrives before any file changes — the moment when calling it off is still free.
What if Claude finds a bug during the refactor?
Ask it to write the bug down and leave it alone. A refactor that also fixes things is two changes in one review, and afterwards nobody can tell which line was which. Fix it in a second change with its own failing test.
Is a subagent a good fit for refactoring?
Rarely. A subagent pays off when the work returns a short answer and would otherwise flood your conversation. A refactor edits files and depends on context your conversation already holds, so it usually belongs where that context is.
One refactor, one session, one plan.
A refactor prompt runs inside one Claude Code session. Omniscio is the level above: a desktop app for running Claude Code and other coding agents side by side, with one list of sessions and one inbox, so an agent only interrupts you when a decision needs you.
The features page lists what is in it today, pricing covers the plans, and you can download it for Windows, macOS or Linux.