Commit df8c1c7
committed
fix: implement duplicate_tool_call_threshold detection logic
Add missing duplicate tool call detection to prevent infinite loops
when the agent repeatedly calls the same tool with identical arguments.
Changes:
- Add duplicate detection logic before tool execution
- Check recent_tool_signatures for matching tool calls
- Abort execution when threshold is exceeded (default: 3)
- Send AgentEvent::Error to notify frontend
- Return error message to LLM so it can adjust its approach
- Add tracing for debugging
This fixes a critical stability issue where the agent could get stuck
in an infinite loop calling the same tool repeatedly (e.g., web_search
with the same query).
The detection logic:
1. Constructs tool signature: "tool_name:json_args"
2. Counts occurrences in recent_tool_signatures (last 8 calls)
3. If count >= duplicate_tool_call_threshold, abort with error
4. LLM receives error and can try a different approach
Example:
- Agent calls web_search("上海AI政策") 3 times
- On 3rd call, threshold exceeded
- Error returned: "Tool has been called 3 times with identical arguments"
- Agent adjusts strategy and tries different parameters
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent ffcc1f3 commit df8c1c7
1 file changed
Lines changed: 40 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3136 | 3136 | | |
3137 | 3137 | | |
3138 | 3138 | | |
| 3139 | + | |
| 3140 | + | |
| 3141 | + | |
| 3142 | + | |
| 3143 | + | |
| 3144 | + | |
| 3145 | + | |
| 3146 | + | |
| 3147 | + | |
| 3148 | + | |
| 3149 | + | |
| 3150 | + | |
| 3151 | + | |
| 3152 | + | |
| 3153 | + | |
| 3154 | + | |
| 3155 | + | |
| 3156 | + | |
| 3157 | + | |
| 3158 | + | |
| 3159 | + | |
| 3160 | + | |
| 3161 | + | |
| 3162 | + | |
| 3163 | + | |
| 3164 | + | |
| 3165 | + | |
| 3166 | + | |
| 3167 | + | |
| 3168 | + | |
| 3169 | + | |
| 3170 | + | |
| 3171 | + | |
| 3172 | + | |
| 3173 | + | |
| 3174 | + | |
| 3175 | + | |
| 3176 | + | |
| 3177 | + | |
| 3178 | + | |
3139 | 3179 | | |
3140 | 3180 | | |
3141 | 3181 | | |
| |||
0 commit comments