How-To Tips
discussion

How to refactor like a god using AI Agents

vikdevelops
Posted October 9, 2025Updated November 14, 2025
Edited

I've gotten pretty skilled at breaking down massive files with Claude Code. Let me walk you through my exact approach.

The Three-Phase Setup

Start by generating context documents before touching any code. This prep work makes everything smoother.

Phase One: Document Current Usage

Ask Claude Code to create a markdown file explaining how your target file integrates with the rest of your codebase. Think of this as creating a map of all the connections and dependencies.

Phase Two: Build Your Refactoring Blueprint

Request another detailed markdown document outlining the refactoring strategy. Break down every step without writing actual code yet. This becomes your roadmap.

Phase Three: Generate Precise Instructions

Switch to Claude Desktop (or your preferred LLM interface). Pick Sonnet 4.5 for smaller refactors. Go with Opus 4.1 when dealing with larger, gnarlier files.

Upload these three items:

  • Usage documentation from Phase One
  • Refactoring plan from Phase Two
  • The original file you're refactoring (if size permits)

Then use this prompt:

After analyzing the following attached files, I want you to give me the prompts (following the project guidelines) to refactor the <file name goes here> file (which I have also attached) into smaller elements without EVER breaking its functionality, just modularizing it.

When writing the prompts, remember that:
* They must be able to be followed by Claude code without getting lost at any point.
* There should be no implementation gaps.
* Only import tests should be run with the new files created in each phase of the process to ensure compatibility at all times.
* You do not need to write explicit source code, just clear and concise instructions for Claude Code to carry out the task.
* The last prompt must be a deep review.

Executing the Refactor

Copy each generated prompt into Claude Code using the /refactor custom command (grab it from this resource if you don't have it).

Let Claude Code handle the heavy lifting.

A Word About Thinking Mode

Complex refactors benefit from thinking mode (press tab to activate). Just watch your token usage. It burns through them fast.

Handling Context Limits

Here's a critical tip: Don't let Claude Code compress your conversation history. If you're approaching the context limit mid-refactor, stop.

Clear the current session. Feed Claude Code your Phase One and Phase Two documents again to restore context. Pick up where you left off with your next prompt.

This keeps everything coherent without losing progress.

Why This Works

Breaking refactors into documented phases prevents Claude Code from getting confused. You're giving it a clear map, a detailed plan, and step-by-step instructions. The modular approach means you can verify functionality at each stage through import tests.

No guesswork. No breaking changes. Just clean, organized code.

Give it a shot on your next refactor!

claude
claude code
cursor
codex
windsurf
warp terminal
452
Loading comments...