AI News DailyTips한국어
← AI tool tips
Claude Code✓ Confirmed in official docsAdded Oct 15

How to fan out a big Claude Code refactor into one Haiku session per file

Push a few hundred file edits through one session and the conversation grows long enough that Claude loses track of what was decided early on. Instead, build the file list first, then start a fresh claude -p session for each file. The official Claude Code best-practices page calls this "fan out across files"; on X it spread as "Opus plans, Haiku grinds."

  1. In an interactive session, have Claude write the file list and the rule to files.
List every file that needs this change in files.txt, one per line, and write the rule to apply in RULE.md
  1. Try a few files first, and fix the prompt if the results look off.
head -3 files.txt | while read f; do
  claude -p "Apply the rule in RULE.md to $f. Reply only OK when done, FAIL if not" \
    --model haiku --max-turns 8 \
    --allowedTools "Edit" --permission-mode dontAsk \
    --output-format json
done
  1. If that works, run eight at a time.
mkdir -p out
xargs -P 8 -I{} sh -c 'claude -p "Apply the rule in RULE.md to {}. Reply only OK when done, FAIL if not" --model haiku --max-turns 8 --allowedTools "Edit" --permission-mode dontAsk --output-format json > "out/$(echo {} | tr / _).json"' < files.txt
  1. Review results and cost together. The --output-format json payload carries the answer (result) and a per-run cost estimate (total_cost_usd).
jq -r '[input_filename, .result, .total_cost_usd] | @tsv' out/*.json

Re-run only the FAIL files with --model sonnet.

Tools listed in --allowedTools run without asking, and --permission-mode dontAsk denies anything else that would need approval, so an unattended run never stalls on a prompt. --max-turns works only in -p mode and exits with an error when the limit is hit.

As in the X post, you can also allow Bash(git commit:*) and let each session commit (:* means the same as *). When sessions run in parallel their commits can collide on git's lock, so commit once after everything finishes.

If you'd rather not write a script, use /batch <instruction>: Claude splits the change across 5 to 30 subagents, each working in its own worktree.

Get notified when a new tip lands

Tip alerts only go out in the app. Install it and turn notifications on.

Get the app

All tips →