[PR #9698] feat: select/replace tools Fixes #9701 #13198

Open
opened 2026-02-16 18:18:04 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/9698

State: open
Merged: No


What does this PR do?

Opened this PR as a draft because it's just something I think was worth prototyping.

The search/replace mechanism that has become the defacto workflow for edits has a couple problems:

  • The LLM has to perfectly recall and generate the code it wants to replace in full. Granted, the edit tool has generous matching here.
  • LLM token generation/output is an order of more expensive and several order of magnitudes slower than prefill/input of the same size.
  • Failures are expensive and slow. If the match fails, the entire tool call needs to be reexecuted.

This pull request adds two new tools: select-text and replace-selection.

The flow is:

  • LLM selects text using start and end search strings, this allows the model to not generate/recall large blocks of code that need replacing.
  • If the search fails, the failure is fast and cheap, and the model reattempts the search.
  • On success, the select tool returns the selected code for the model to review and continue. Prefill of large code blocks, even thousands of lines, is sub second, whereas generation may take minutes.
  • The LLM then uses the replace tool and generates/outputs tokens for only the new code.

This is roughly 2x fast for half the price with Claude, for example.

Model Base Input Tokens 5m Cache Writes 1h Cache Writes Cache Hits & Refreshes Output Tokens
Claude Opus 4.5 $5 / MTok $6.25 / MTok $10 / MTok $0.50 / MTok $25 / MTok

The downside is that model training data now extensively uses the existing edit tool style calls, so that flow is baked into their weights. However, as mentioned, failures are fast and inexpensive comparatively with this new tool call flow.

The change needed a way for tools to access the conversation history, as the new tools are stateful.

How did you verify your code works?

Tests and usage.

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/9698 **State:** open **Merged:** No --- ### What does this PR do? Opened this PR as a draft because it's just something I think was worth prototyping. The search/replace mechanism that has become the defacto workflow for edits has a couple problems: * The LLM has to perfectly recall and generate the code it wants to replace in full. Granted, the edit tool has generous matching here. * LLM token generation/output is an order of more expensive and several order of magnitudes slower than prefill/input of the same size. * Failures are expensive and slow. If the match fails, the entire tool call needs to be reexecuted. This pull request adds two new tools: select-text and replace-selection. The flow is: * LLM selects text using start and end search strings, this allows the model to not generate/recall large blocks of code that need replacing. * If the search fails, the failure is fast and cheap, and the model reattempts the search. * On success, the select tool returns the selected code for the model to review and continue. Prefill of large code blocks, even thousands of lines, is sub second, whereas generation may take minutes. * The LLM then uses the replace tool and generates/outputs tokens for only the new code. This is roughly 2x fast for half the price with Claude, for example. | Model | Base Input Tokens | 5m Cache Writes | 1h Cache Writes | Cache Hits & Refreshes | Output Tokens | |-------|------------------|-----------------|-----------------|------------------------|---------------| | Claude Opus 4.5 | $5 / MTok | $6.25 / MTok | $10 / MTok | $0.50 / MTok | $25 / MTok | The downside is that model training data now extensively uses the existing edit tool style calls, so that flow is baked into their weights. However, as mentioned, failures are fast and inexpensive comparatively with this new tool call flow. The change needed a way for tools to access the conversation history, as the new tools are stateful. ### How did you verify your code works? Tests and usage.
yindo added the pull-request label 2026-02-16 18:18:04 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#13198