[BUG] Model attempts file edits without reading file first, wasting tokens and time
[BUG] Model attempts file edits without reading file first, wasting tokens and time
Preflight Checklist
- [x] I have searched existing issues and this hasn't been reported yet
- [x] This is a single bug report (please file separate reports for different bugs)
- [x] I am using the latest version of Claude Code
What's Wrong?
Claude Code's underlying model consistently attempts to use Edit/Write/str_replace tools on files it has not read in the current context. This wastes significant tokens and time as:
- The model generates an edit command with guessed/hallucinated content
- The tool returns an error: "File has not been read yet. Read it first before writing to it."
- The model then reads the file
- The model regenerates the edit command with correct content
This pattern burns double the output tokens for every file edit and adds unnecessary latency to every coding task.
Impact
- Token waste: Every edit attempt without prior read wastes the full output tokens of the failed edit command
- Time waste: Users wait for failed edits, error processing, and regeneration
- Cost: API users pay for wasted tokens; Pro users hit usage limits faster
- Frustration: This has been occurring for months with no apparent improvement
Expected Behavior
The model should always read a file before attempting to edit it. This should be:
- Part of the model's training/RLHF to never attempt edits on unread files
- Reinforced in the system prompt
- Potentially enforced at the tool level (auto-read before edit)
The tool already has this safety check - the model just needs to respect it proactively rather than learning from the error each session.
Actual Behavior
The model frequently attempts edits on files it hasn't read, receives an error, then reads the file and retries. This happens:
- At the start of new sessions
- After context compaction
- When working across multiple files
- Seemingly at random even mid-session
Reproduction Steps
- Start a new Claude Code session
- Ask Claude to make a simple edit to an existing file (e.g., "Add a docstring to function X in file.py")
- Observe Claude attempt the edit without first reading the file
- Watch it fail and retry
Environment Info
- Platform: macOS (Apple Silicon)
- Terminal: Various (iTerm2, VS Code integrated terminal)
- Claude Code Version: Latest (issue persists across versions)
Related Issues
- #4230 - Feature request for write tool to auto-read (addresses symptom, not root cause)
- #6729 - Edit failures and state assumptions
- #7533 - Context preservation vs correctness when reading files
Suggested Fixes
Option 1: Model-level fix (Preferred)
Fine-tune/RLHF the model to always read before edit. This is a fundamental capability that should be reliable.
Option 2: System prompt reinforcement
Add explicit instruction: "ALWAYS use the Read tool on a file before attempting any Edit/Write operation, even if you believe you know the file's contents."
Option 3: Tool-level enforcement
Modify Edit/Write tools to automatically read the file first if not in context, rather than erroring. This was suggested in #4230.
Additional Context
This issue has persisted for months despite being a fundamental workflow inefficiency. Every Claude Code user is affected by this on nearly every session. The cumulative token waste across the user base must be enormous.
The irony is that Claude is explicitly told this is wrong behavior via error messages, yet continues to make the same mistake. This suggests either:
- The training doesn't sufficiently penalize this behavior
- The system prompt isn't clear enough
- Context compaction loses the learned behavior
Please prioritize this - it's low-hanging fruit that would improve every user's experience.
Found 3 possible duplicate issues:
- https://github.com/anthropics/claude-code/issues/16182
- https://github.com/anthropics/claude-code/issues/14997
- https://github.com/anthropics/claude-code/issues/14964
This issue will be automatically closed as a duplicate in 3 days.
- If your issue is a duplicate, please close it and 👍 the existing issue instead
- To prevent auto-closure, add a comment or 👎 this comment
🤖 Generated with Claude Code
This is not a duplicate. This is a behavior I have complaied about forever. Claude code consistently attempts to write to an existing file without first reading the file, resulting in an error and the need to burn more tokens and time to go back and read the file before modifying it.
I encountered the same issue. Even though I instructed the AI to first Read the corresponding file before each Write in the ~/.claude/CLAUDE.md file, the AI still did not adhere to this convention, always triggering direct Write calls, resulting in wasted tokens and time. Furthermore, sometimes when the task involves editing an existing file that is not relevant to the current work, the AI's attempt to read the old file may actually interfere with its current task (such as creating a new progress.md, reading old progress.md may interfere AI).
I hope this restriction can be removed in the yolo mode. As long as the AI is editing files under the PWD subdirectory, it should be allowed.
I understand that the original intention of this interception is to prevent AI from editing files unexpectedly, but I rarely encounter situations where AI edits the wrong files now. Instead, this interception occurs more frequently, affecting Agent performance.
This issue has been inactive for 30 days. If the issue is still occurring, please comment to let us know. Otherwise, this issue will be automatically closed in 30 days for housekeeping purposes.
same problem here, it gets even more annoying when claude tries to make smalls changes in batches... It reads all files, figures out what has to change, tries to make the first change to all files, all edits fail, it then reads ALL FILES AGAIN, makes the first edit, then tries to do the second edit, IT FAILS AGAIN, ....
ITS INFURIATING!
the irony.
Issue #16546 — "Model attempts file edits without reading file first" — has a stale bot threatening to auto-close it, and that stale bot is itself powered by Claude Code (per the badge on your issue's bot comment: "Generated with Claude Code"). So a Claude-powered bot is trying to close a bug report about Claude not reading before acting... by applying a stale label after 30 days of inactivity on an issue that has has repro and bug labels from the maintainers themselves. And the issue has 6 confirmed labels including area:model and area:tools.
Closing for now — inactive for too long. Please open a new issue if this is still relevant.
This issue has been automatically locked since it was closed and has not had any activity for 7 days. If you're experiencing a similar issue, please file a new issue and reference this one if it's relevant.