Expand description
Collapse redundant identical tool calls.
When an agent re-issues the same tool call with the same arguments
(e.g. read_file { path: "src/lib.rs" } called at step 3 and again
at step 17), the call itself is wasted tokens — the earlier
response is already in history (and by this point usually already
deduplicated by super::dedup).
This pass walks the buffer, hashes each ContentPart::ToolCall by
(name, arguments), and replaces the arguments of later
duplicates with a short [REPEAT of call_X] marker. The tool’s
result block for the repeat is left untouched so the model still
sees a consistent call→result pairing; dedup will have collapsed
the result body if the output was identical.
§Safety
- Only the arguments string is rewritten. Name, id, and thought_signature are preserved so every provider adapter still round-trips.
- Tool calls in the most recent
KEEP_LAST_MESSAGESmessages are never touched — the model is likely actively reasoning over them. - Only triggers when arguments exceed
MIN_COLLAPSE_BYTESso micro-arguments like{}stay readable.
§Rollout
This strategy is intentionally kept out of the default-safe
super::apply_all path because replacing historical arguments
with back-references can erase still-relevant call details.
Constants§
- KEEP_
LAST_ MESSAGES - Keep tool-call arguments verbatim in this many trailing messages.
- MIN_
COLLAPSE_ BYTES - Tool-call arguments shorter than this are never collapsed.
Functions§
- collapse_
duplicate_ calls - Collapse duplicate tool-call arguments into short back-references.