Model uses N sequential Edit calls for bulk renumbering instead of single sed/Write

Resolved 💬 3 comments Opened May 20, 2026 by lmello Closed Jul 13, 2026

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.).
  1. Ask Claude Code:

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

  1. 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.

View original on GitHub ↗

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