Skip to content

Prevent agents from calling duplicate tools with same inputs#9

Draft
Copilot wants to merge 4 commits into
mainfrom
copilot/fix-8
Draft

Prevent agents from calling duplicate tools with same inputs#9
Copilot wants to merge 4 commits into
mainfrom
copilot/fix-8

Conversation

Copy link
Copy Markdown

Copilot AI commented Aug 11, 2025

This PR implements duplicate tool call prevention to address a common failure mode where frontier models (GPT-5, Gemini-Flash-2.5) get stuck calling the same tool repeatedly with identical inputs.

Problem

Agents would sometimes enter infinite loops calling tools like:

<tool name="lookup_memory">{"query": "project timeline"}</tool>
<tool name="lookup_memory">{"query": "project timeline"}</tool>
<tool name="lookup_memory">{"query": "project timeline"}</tool>
<!-- ... continues indefinitely -->

Solution

The implementation tracks tool calls within each AgentRun execution and blocks duplicates based on tool name + normalized arguments. However, agents can override this by providing unique reasoning:

<!-- First call - allowed -->
<tool name="lookup_memory">{"query": "project timeline"}</tool>

<!-- Exact duplicate - blocked -->
<tool name="lookup_memory">{"query": "project timeline"}</tool>

<!-- Duplicate with unique reasoning - allowed -->
<tool name="lookup_memory">{"query": "project timeline", "_duplicate_reasoning": "searching with broader context"}</tool>

Key Features

  • Argument normalization: Excludes _nonce and _duplicate_reasoning from duplicate detection
  • Reasoning validation: Ensures _duplicate_reasoning is unique within the current session
  • Format support: Works with both JSON and XML argument formats
  • Clear error messages: Guides agents when duplicates are blocked
  • Whitespace handling: Normalizes reasoning to prevent trivial bypasses
  • Stress level adjustment: Increases when duplicates are blocked to encourage different behavior

Components Affected

  • AgentRun: Primary implementation with tool call history tracking
  • AgentLearn: Extended to support duplicate prevention
  • Both components now initialize self.tool_call_history = [] and reset it on each execution

Technical Implementation

Added helper functions:

  • normalize_tool_args(): Removes special parameters for comparison
  • extract_nonce_and_reasoning(): Extracts special parameters from parsed args
  • is_duplicate_tool_call(): Checks if tool call matches previous calls
  • is_reasoning_unique(): Validates reasoning hasn't been used before

The duplicate check is integrated into handle_tool_use() before tool execution, providing immediate feedback when duplicates are detected.

Fixes #8.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI self-assigned this Aug 11, 2025
Copy link
Copy Markdown
Author

Copilot AI commented Aug 11, 2025

@wmeddie 👋 This repository doesn't have Copilot instructions. With Copilot instructions, I can understand the repository better, work faster and produce higher quality PRs.

I can generate a .github/copilot-instructions.md file for you automatically. Click here to open a pre-filled issue and assign it to me. I'll write the instructions, and then tag you for review.

Copilot AI and others added 3 commits August 11, 2025 18:08
Co-authored-by: wmeddie <122480+wmeddie@users.noreply.github.com>
Co-authored-by: wmeddie <122480+wmeddie@users.noreply.github.com>
Co-authored-by: wmeddie <122480+wmeddie@users.noreply.github.com>
Copilot AI changed the title [WIP] We need to prevent agents to call the same tool multiple times in a row with the same inputs. Prevent agents from calling duplicate tools with same inputs Aug 11, 2025
Copilot AI requested a review from wmeddie August 11, 2025 18:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

We need to prevent agents to call the same tool multiple times in a row with the same inputs.

2 participants