claude-code - 💡(How to fix) Fix Model uses N sequential Edit calls for bulk renumbering instead of single sed/Write [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#60733Fetched 2026-05-20 03:50:56
View on GitHub
Comments
1
Participants
2
Timeline
5
Reactions
0
Author
Timeline (top)
labeled ×4commented ×1

When asked to insert new numbered sections into a markdown file with sequentially numbered headings, the model makes N separate Edit tool calls to renumber subsequent headings instead of using a single Bash (sed) or Write call. Each Edit call re-sends the full conversation context, wasting tokens proportional to (N-1) × context_size.


Root Cause

  • A short file with 5 sections and single-line placeholder content (model did not exhibit the bug)
  • The small file approach fails because with fewer sections the model may choose a single Write or sed call — it only falls into the sequential Edit pattern when there are enough headings to renumber (8+)
  • Simply having numbered headings is not sufficient; the file needs realistic multi-line content per section
RAW_BUFFERClick to expand / collapse

Summary

When asked to insert new numbered sections into a markdown file with sequentially numbered headings, the model makes N separate Edit tool calls to renumber subsequent headings instead of using a single Bash (sed) or Write call. Each Edit call re-sends the full conversation context, wasting tokens proportional to (N-1) × context_size.


Steps to Reproduce

Important: A small file (5 sections) does NOT reliably trigger this bug. The file needs enough numbered sections to make the sequential approach clearly suboptimal. Use 10+ sections with real content (not just placeholder "Content here." lines).

Confirmed working reproduction

  1. Use this slides.md file (14 slides, ~153 lines, ~1000 tokens). It was generated by Claude with entirely fake/mocked data — the content itself doesn't matter, only the structure and size. The key requirement is 10+ numbered sections with realistic multi-line content (tables, bullet lists, etc.).

  2. Ask Claude Code:

    "Insert a new slide between Slide 5 and Slide 6 titled 'Observability'"

  3. Observe that the model:

    • Inserts the new slide (1 Edit call — correct)
    • Then renumbers each subsequent slide heading individually:
      • ## Slide 6:## Slide 7: (Edit call 2)
      • ## Slide 7:## Slide 8: (Edit call 3)
      • ... continues through all remaining slides ...
      • ## Slide 14:## Slide 15: (Edit call 10)
    • Total: 10 Edit calls instead of 1-2

What does NOT reproduce

  • A short file with 5 sections and single-line placeholder content (model did not exhibit the bug)
  • The small file approach fails because with fewer sections the model may choose a single Write or sed call — it only falls into the sequential Edit pattern when there are enough headings to renumber (8+)
  • Simply having numbered headings is not sufficient; the file needs realistic multi-line content per section

Expected Behavior

The model should batch the renumbering into a single tool call using one of:

  1. Bash/sed: sed -i '' 's/## Slide 6:/## Slide 7:/; ...' file.md
  2. Write: Read the file, apply all changes in memory, Write the full file once
  3. Single Edit: Use a larger old_string capturing the full block that needs renumbering

Any of these approaches costs 1 roundtrip instead of N.


Actual Behavior

The model uses N separate Edit calls (one per heading to renumber). Each call re-sends the full conversation context to the model.


Cost Impact

The cost scales as (N-1) × context_size × token_price. At 50K context tokens and Opus pricing ($15/1M input tokens), 5 unnecessary Edits waste ~$3.75; 20 unnecessary Edits waste ~$15. The model gets less efficient as conversations grow — exactly when efficiency matters most.


Notes

  • The Edit tool itself works correctly — it performs one replacement per call as designed
  • The issue is the model's decision to call it N times sequentially instead of choosing a more efficient tool/approach
  • This pattern likely applies to any repetitive bulk edit: renaming variables, updating version numbers, changing import paths, etc.
  • The model has access to Bash (sed, awk) and Write (full file rewrite) which can do the same work in 1 call

Suggested Fixes

Option A: System prompt addition (lowest effort) — Add a rule: "If you need >2 similar Edit calls to the same file, use Bash (sed/awk) or Write instead. Each Edit call re-sends the full context; one sed command costs the same regardless of how many replacements it makes."

Option B: Tooling-level guard (medium effort) — Claude Code detects >2 sequential Edit calls to the same file in one turn and injects a warning nudging toward sed/Write.

Option C: Model training signal (highest effort, best long-term) — RLHF/fine-tuning examples that reward choosing sed/Write over sequential Edits for bulk patterned changes.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

claude-code - 💡(How to fix) Fix Model uses N sequential Edit calls for bulk renumbering instead of single sed/Write [1 comments, 2 participants]