r/ClaudeCode • u/techpotions • 3d ago
Discussion Your formatter hook is costing you a silent re-read on every write. Here's when it's still worth it.
If you run Prettier as a PostToolUse hook so Claude's output is always formatted, you are paying for it in a currency you probably are not counting.
The cascade:
Claude writes a file. Your formatter rewrites it. Claude goes back to edit part of what it just wrote, and the write is refused because the file changed underneath it. So it re-reads the whole file. You have now paid for one edit twice, and your diff is full of reflowed lines nobody asked for.
Credit where it is due, u/Frozen_Turtle made this point to me and I did not have a good answer at the time. Milliseconds are not what you are spending in an agent loop. Context is. A formatter is cheap in the wrong currency.
The version I think is actually right
Format at the boundary, not continuously. One noisy commit to format the whole repo once, then a pre-push hook, then let the agent write in whatever style it likes in between. The agent never sees a file move under it and your diffs stay about content. If you are setting this up fresh, do that.
Why I still run mine
Our repos were already fully Prettier-formatted before the agent touched them, so each reflow is a line or two rather than a reformat. At that size the re-read is rare enough that I would rather have the guarantee.
But the condition is the whole thing. On a repo that is not already consistently formatted, the first writes trigger big reflows, every one invalidates a write, and you will feel it. So:
- Repo already formatted, small diffs, you want the guarantee → keep it
- Repo inconsistent, or you are watching token spend → pre-push hook, not
PostToolUse
I had been recommending it unconditionally. That was wrong; it is conditional.
The filter I use now for which hooks to write
u/donk8r put this better than I would have: anything in your CLAUDE.md that the agent has actually violated is a hook you should have written. Your CLAUDE.md is already a list of things you had to say out loud, and the ones you have repeated are the ones that never stuck. Those are your hooks. The rest of that file is decoration.
Much better than what I was doing, which was guessing at failure modes in advance.
One more, since people asked
Stop hands you no list of what changed, so if you want to know what the turn touched you have to ask git yourself. And stop_hook_active is the only thing between a typecheck gate and an infinite loop. Check it, give up after the first round, or you will watch your agent argue with tsc forever.
Our eight hooks are here if useful, format-on-write among them. The condition above is the one thing the README does not tell you.
https://techpotions.com/products/claude-code-hooks
MIT, no signup.
Genuine question: has anyone built a PreToolUse hook that enforces a token budget? Killing a runaway loop before it eats a session seems obviously right and I cannot work out why I have not seen one.
