feat(tools): add transfer_reason argument to transfer_to_agent to prevent multi-agent transfer loops - #6590
Open
Srikanth-Saravanan wants to merge 2 commits into
Open
feat(tools): add transfer_reason argument to transfer_to_agent to prevent multi-agent transfer loops#6590Srikanth-Saravanan wants to merge 2 commits into
Srikanth-Saravanan wants to merge 2 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add
transfer_reasonargument totransfer_to_agentProblem
The built-in
transfer_to_agenttool only accepts the targetagent_name. When an agent hands off control, the full session context is carried over, but the tool call itself contains no explicit rationale for why the transfer is happening.In multi-agent systems this causes agents to "ping-pong". Because the receiving agent has no stated reason for the handoff, it frequently cannot tell why it was given control and transfers straight back to the sender — in some cases looping between agents indefinitely.
Solution
This change adds a required
transfer_reason: strargument totransfer_to_agent. The delegating agent must now provide a short, explicit reason whenever it hands off. That reason travels with the transfer, giving the receiving agent the context it needs to continue the task instead of bouncing control back — which breaks the loop.Design notes:
TransferToAgentToolpicks up the new parameter automatically through reflection. The existingagent_nameenum constraint (which restricts transfers to valid agent names) is unchanged.Testing
Unit tests
test_transfer_to_agent_tool.py: the required parameters are now['agent_name', 'transfer_reason'], the property count goes from 1 to 2, and a new assertion verifiestransfer_reasonis a string with no enum constraint.transfer_reasonis required,FunctionTool's mandatory-argument validation now enforces it. Every test that mocks atransfer_to_agentfunction call was updated to include the new argument.pytestresults — 154 passed, 9 skipped, 3 xfailed:Manual verification
Confirmed the generated function declaration exposes the new parameter:
In a multi-agent run, the model now emits calls such as
transfer_to_agent(agent_name="billing_agent", transfer_reason="user asked about an invoice charge"),and the receiving agent uses the stated reason to continue the task rather than transferring back.
Backward compatibility
transfer_reasonis a required argument. LLM-driven callers populate it naturally from the function declaration, but any code that hand-crafts atransfer_to_agentfunction call (for example tests or replay fixtures) must now include it. All such call sites in the repository have been updated in this PR.