[BUG] Model attempts file edits without reading file first, wasting tokens and time

Resolved 💬 8 comments Opened Jan 7, 2026 by SDS-Mike Closed Mar 23, 2026

[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:

  1. The model generates an edit command with guessed/hallucinated content
  2. The tool returns an error: "File has not been read yet. Read it first before writing to it."
  3. The model then reads the file
  4. 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:

  1. Part of the model's training/RLHF to never attempt edits on unread files
  2. Reinforced in the system prompt
  3. 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

  1. Start a new Claude Code session
  2. Ask Claude to make a simple edit to an existing file (e.g., "Add a docstring to function X in file.py")
  3. Observe Claude attempt the edit without first reading the file
  4. 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.

View original on GitHub ↗

This issue has 8 comments on GitHub. Read the full discussion on GitHub ↗