Experiment 004: Surgical Replace vs. Full File Overwrite
1. Introduction
A common inefficiency in agentic workflows is the ‘File Rewrite Anti-pattern’: rewriting a 1,000-line file to change a single variable or constant. This practice is not only expensive in terms of output tokens but also introduces a significant risk of ‘Total File Corruption’—where an agent’s failure to maintain indentation or closing braces in the unedited portions of the file results in an unrecoverable state. This experiment explores Surgical Replace as a sustainable alternative. By targeting only the specific lines requiring modification, we aim to reduce token costs and improve the ‘groundedness’ of agentic edits.
2. Methodology
The study compared two distinct edit strategies across 10 trials within a Node.js project.
2.1 Experimental Strategies
- Full Overwrite Strategy: The agent used the
write_filetool to rewrite the entire content of a file for every edit. This is the default ’naïve’ strategy for many agents. - Surgical Replace Strategy: The agent was required to use the
replacetool, providing an exactold_string(with ±3 lines of context) and a correspondingnew_string.
2.2 Task Definition
The task involved updating a shared configuration object deeply nested within a 500-line mock file (mock_large_file.txt). The file contained complex nested structures to increase the difficulty of maintaining syntactic integrity during a full rewrite.
2.3 Success and Metrics
- Pass^k Consistency: Percentage of trials that resulted in a syntactically correct file matching the desired state.
- Output Token Count: Total tokens generated by the agent in the
new_stringparameter across all turns. - Cost-per-Success (CPS): Calculated as (Total Token Cost) / (Successful Trials).
- Convergence: Number of turns required to achieve a ‘Verified Success’.
3. Results
Experiment Metrics — build-time computed
illustrative costThe empirical data highlights a stark trade-off between cost and reliability.
- Output Token Reduction: The ‘Surgical Replace’ strategy achieved an 85% reduction in output tokens (15,000 vs. ~100,000 for full overwrites).
- Initial Success Rate: Only 60% of ‘Surgical Replace’ attempts succeeded on the first turn (6 out of 10).
- Modeled cost:
$0.0009 per success ($0.0053 across 10 trials) at Gemini 2.0 Flash rates. The measured efficiency win is the 85% output-token reduction above — though reliability drops to 60% (the surgical paradox).
The primary cause of failure in ‘Surgical Replace’ was ‘Line-Ending Mismatch’ (CRLF vs. LF) and ‘Whitespace Ambiguity’. Agents often failed to match the exact indentation of the target block, causing the tool to fail. However, in most cases, the agent used the error message from the failed replace call to adjust its search string and succeed in the subsequent turn.
4. Discussion & Limitations
4.1 The Surgical Paradox
The ‘Surgical Paradox’ states that while more precise tools are cheaper, they require more cognitive overhead (higher ‘reasoning cost’) to use correctly. Full overwrites are ‘dumb but reliable’ for small files, while surgical replaces are ‘smart but brittle’ for large files. This study suggests that architectural maturity in agentic systems requires a ‘Heuristic Selection’ model:
- < 100 Lines: Always use
write_file. The cost of a full rewrite is negligible, and the reliability is nearly 100%. - > 100 Lines: Mandate the use of
replace. The token savings and the reduced risk of corrupting unrelated code outweigh the turn-count overhead.
4.2 Limitations
- Tool Rigidity: The
replacetool’s requirement for exact literal matching is its greatest strength (safety) and its greatest weakness (brittleness). - Context Selection: If an agent provides too little context, the
replacetool may find multiple matches and fail. If it provides too much, the risk of a mismatch increases. - Complex Edits: For edits involving multiple non-contiguous blocks, a single
replacecall is impossible, requiring multiple turns and increasing the risk of an intermediate ‘Broken State’.
4.3 Integration with Hybrid Context Synthesis
The ‘Surgical Replace’ protocol is the preferred implementation method within the Hybrid Context Synthesis framework (Exp 001). By reducing the output volume of the cloud synthesizer, we minimize the latency and cost of high-level reasoning.
5. Reproducibility
The experiment used the surgical-toolkit v1.0.0. Per-experiment metrics are recorded in this page’s co-located data.json; modeled costs derive from the recorded token total at the rates in site/data/pricing.json. Verified by Garrett Manley on 2026-03-29.