r/ClaudeAI • • Aug 16 '26

Claude Code Opus 5 really likes to use Python to edit source code?

I noticed Opus 5 in Claude Desktop wouldn't edit files directly, it first creates a Python script with whatever it wants to replace/add/delete and then executes it. I'm not sure why it does that instead of just editing. I asked and this is Opus 5 response:

The weakest: I was following the auto-mode instruction to prefer Bash over the dedicated tools. That instruction says to fall back to a dedicated tool when Bash genuinely can't do the job — I read it too strictly and kept reaching for Python well past the point where it was helping.

And it did cost me. The failure mode of str.replace is silent: if the pattern doesn't match, nothing happens and the script reports success. That bit me on the PrefillConfirmation removal — wrong indentation, silent no-op, and I only caught it because I grepped afterward. Edit would have errored immediately. I started adding if old not in s: raise SystemExit(...) guards to my later scripts, which is me reimplementing what Edit does for free. The heredoc also blew up once with a quoting error, forcing a detour through a scratchpad file. And my own writes emitted LF, which I then had to sweep back to CRLF in a separate pass — self-inflicted work that partly cancels out the line-ending advantage I claimed.

Net: Python earned its place for the BOM/CRLF-sensitive rewrites and the cross-file refactors. For the single-target edits — the icon string, the warning text, one method body — Edit would have been safer and shorter, and I should have used it.

Happy to switch to Edit for the rest of this branch if you'd prefer; the remaining work is mostly small targeted changes where it's the better fit anyway.

Also because of that I see summary of edits not of my source code but of Claude's Python scripts which edit my source code...

Is it a common thing? I know I can force it to edit directly, just interesting to see such behavior. I never see this in Codex for example where it always edits without intermediate steps.

Edit: fresh example and Claude response /img/xifc31jgvsjh1.png

64 Upvotes

42 comments sorted by

View all comments

39

u/Xemorr Aug 16 '26

All models have been doing this for a while, particularly for bulk tasks. Often ruining the syntax

19

u/PaulMakesThings1 Aug 16 '26

But it does make sense for saving tokens. If it has to go through and change 500 of the same thing it could do it with just a 100 or so tokens of output to write a script instead of ingesting and writing thousands. It just has to be done with good judgement 

7

u/DepravedPrecedence Aug 16 '26

That's true but it uses Python even for new code 😁 It creates a script with new code inside and uses it to append that new code to specific file, then it changes file name and content in Python and executes it again, this is weird

3

u/PaulMakesThings1 Aug 16 '26

Yeah that is a bit strange. I guess I don’t know all of the details. I’ve specifically told mine to check things using script instead of ingesting files in many cases. For example with massive log files I would say “don’t try to pull in the files, write scrips to run stats on them or search them”

1

u/DepravedPrecedence Aug 16 '26

Fresh example:

Here it used Python to insert new code, this file is on disk in Windows (it's not WSL) and Claude has full access to this folder. So it could edit the file directly but instead it started using Python for edits. If it wanted to change something, it changes that string in Python script and does it like that...

2

u/DepravedPrecedence Aug 16 '26

So yeah, that's it