Your agent rewrites whole files
“how to stop Claude Code rewriting the entire file to change a few lines”
Your agent rewrites an entire file to change three lines, wiping your comments, your formatting, and unrelated code in the process. It defaults to regenerating whole files because that is easier for it than a surgical edit. The fix is a rule that forces small, targeted edits and a diff you can actually review before anything lands.
Why it regenerates instead of editing
A precise edit requires the model to locate an exact anchor in your file and change only the span around it. Regenerating the whole file does not: it just emits the file again from scratch, which is the kind of long, self-consistent sequence models are best at. So the lazy path and the probable path are the same path, and it runs straight down it, carrying along changes to code you never mentioned.
The collateral damage is not malice, it is loss of fidelity. When the model re-emits a file it reconstructs your code from its own understanding, and anything it does not consider load-bearing, your comments, your import order, a deliberate bit of ugliness, gets smoothed away or dropped. The same instinct shows up when it recreates a deprecated version of a method instead of editing the one you have, which is documented behavior, not a rare glitch.
- // NOTE: keep padding at 12, design locked it (see #441)
- import { useLayoutEffect } from "react"; // used below
export function Header() {
- const pad = 12;
+ const pad = 16;
// ...298 other lines re-emitted...
- // TODO(sam): a11y pass before launch
}
// your comments, import order, and TODO: gone export function Header() {
- const pad = 12;
+ const pad = 16;
}
// one line changed. everything else untouched,
// because nothing else needed touching.The manual fix (free, and complete)
The fix is to forbid whole-file regeneration for small changes and to require a reviewable diff. Paste this into CLAUDE.md or AGENTS.md. It is the complete rule; there is no cut-down version behind a paywall.
## Edit, do not regenerate
- To change part of a file, edit that part in place. Do not rewrite the whole
file to alter a few lines.
- Preserve comments, import order, and any code you were not asked to change. If
you think unrelated code should change too, ask first; do not fold it in.
- Show the change as a minimal diff (only the touched lines) before or as you
apply it, so I can review exactly what moved.
- If a real refactor genuinely needs a rewrite, say so and get my yes before
regenerating the file.- Forbid regeneration for small changes. Require in-place edits and ban rewriting a whole file to change a few lines.
- Protect the untouched code. Tell the agent to preserve comments, import order, and anything it was not asked to change.
- Require a minimal diff. Ask for the change as a diff of only the touched lines so you can review what actually moved.
- Gate real rewrites. If a refactor truly needs a full rewrite, require the agent to say so and get approval first.
The kit's starter AGENTS.md bundles this edit-in-place rule with the rest of the behavior set, so it lands in your file already phrased and ready.
The durable fix (the kit)
A rule against whole-file rewrites is only as good as the model's willingness to follow it mid-task, and under pressure the easy regeneration path wins again. The kit turns the rule into a guardrail: a hook watches for a write that replaces a file wholesale when the request was small, and blocks it, pushing the agent back to a targeted edit instead of trusting it to choose the harder path on its own.
As with every rule here, the edit-in-place instruction travels to Codex, Cursor, and Antigravity through AGENTS.md, so the behavior is cross-tool. The enforcing hooks, plus the navigator HUD that shows you what changed at a glance, run in Claude Code only. One command to install, one-time price.
The real numbers
Whole-file rewrites are a big part of why unconfigured agents produce so much more churn for the same result. In our first clean-room suite, five scripted tasks with the same model in both arms, the kit-configured agent produced about 40 percent less code for the same outcome, in good part because it edited surgically instead of re-emitting files and their incidental changes.
Kept honest: the guardrail is not free. The extra hook context added about a 25 percent average cost premium across the suite, and on trivial tasks that is pure overhead with nothing to protect. You are paying to keep your own code intact on the edits where that matters.
| Measure | Vanilla agent | Kit-configured |
|---|---|---|
| Code churned for same outcome | baseline | about 40% less |
| Your comments and formatting | often lost | preserved |
| Guardrail context cost | none | about +25% average |
Directional, n=1 per task. Less churn is the win; the context cost is the price.
It is in Anthropic's own tracker
The clearest documented instance of this exact instinct is the most-upvoted report in the tracker.
That report calls out the agent creating a deprecated version of a method instead of editing the one that exists. Re-emitting rather than editing in place is the same behavior at file scale.
Common questions
How do I stop Claude Code rewriting whole files?
Add an edit-in-place rule to CLAUDE.md or AGENTS.md: forbid regenerating a whole file to change a few lines, require the agent to preserve comments and untouched code, and ask for a minimal diff you can review. To make it hold under pressure, a hook can block a wholesale file write when the request was small.
Why does my agent overwrite my comments and formatting?
Because when it re-emits a file it reconstructs it from its own understanding, and anything it does not treat as load-bearing, comments, import order, deliberate quirks, gets smoothed away. Editing in place avoids the reconstruction entirely, so nothing you did not ask to change gets touched.
Is a whole-file rewrite ever the right move?
Sometimes, for a genuine refactor. The rule is not never rewrite; it is do not rewrite silently for a small change. When a real restructure needs it, the agent should say so and get your yes first, so a rewrite is a decision you made, not a default you inherited.
Does asking for a diff actually help?
Yes, in two ways. It gives you a reviewable change instead of a mystery file, and the act of producing a minimal diff nudges the model toward a minimal edit. A rule plus a hook that enforces the diff turns a good intention into consistent behavior.

Make your agent ask before it builds
One kit. Ask-first behavior, lean output, no AI slop. Works with Claude Code, Codex, and Antigravity.
Get the kit · $29One-time. Less than one hour of cleaning up AI slop.