Skip to content

Fix bug with multiple tool calls overwriting each other#122

Merged
tghamm merged 1 commit into
tghamm:mainfrom
gritse:fix/multiple-tool-calls-bug
Jul 10, 2025
Merged

Fix bug with multiple tool calls overwriting each other#122
tghamm merged 1 commit into
tghamm:mainfrom
gritse:fix/multiple-tool-calls-bug

Conversation

@gritse
Copy link
Copy Markdown
Contributor

@gritse gritse commented Jul 5, 2025

Summary

Fixes a bug where multiple tool calls to the same function in a single response would overwrite each other's IDs and arguments.

Problem

When the model called the same tool multiple times in one response, the code was reusing the same tool instance and directly modifying its properties:

tool.Function.Arguments = (message as ToolUseContent).Input;
tool.Function.Id = (message as ToolUseContent).Id;

This caused all tool calls to end up with the same ID and arguments (the last ones processed).

Solution

  • Create a copy of the tool for each call using new Common.Tool(tool)
  • Set the arguments and ID on the copied instance instead of the original
  • Applied fix to both non-streaming (GetClaudeMessageAsync) and streaming (StreamClaudeMessageAsync) methods

Test

Added TestMultipleCallsSameFunction that:

  • Asks Claude to make multiple calls to the same function in one response
  • Verifies each tool call has a unique ID using HashSet.Add()
  • Verifies each tool call has different arguments by comparing JSON strings
  • Test fails before the fix and passes after the fix

Files Changed

  • Anthropic.SDK/Messaging/MessagesEndpoint.cs - The bug fix
  • Anthropic.SDK.Tests/Tools.cs - Added test case

🤖 Generated with Claude Code

- Create copies of tools instead of reusing same instance in MessagesEndpoint
- Add test TestMultipleCallsSameFunction to verify unique IDs and arguments
- Fixes issue where same tool called multiple times would overwrite previous calls

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@gritse
Copy link
Copy Markdown
Contributor Author

gritse commented Jul 5, 2025

@tghamm

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.

2 participants