Agent Handoff Behavior with `transfer_to_agent` (Runner & BaseAgent/LlmAgent)
We're working on a multi-agent application using ADK v0.5.0 and have encountered some ambiguity regarding the expected behavior of agent handoffs, specifically when using actions.transfer_to_agent with the ADK Runner and PostgresSessionService.
Our Scenario & Observations:
We set up a minimal test case:
- A root agent (
MinimalTransferOrchestratorDebug, aBaseAgent) is configured in theRunner. - Upon receiving its first user message, this orchestrator yields a single
Event. TheEvent.actionshastransfer_to_agentset to the name of a target sub-agent (e.g.,"CustomUserInfoAgent_NativeLLM"). - The target sub-agent is correctly listed in the root agent's
sub_agentsattribute during initialization.
Observed Behavior (via backend logs):
- The
MinimalTransferOrchestratorDebugruns its turn and successfully yields the event with thetransfer_to_agentaction. - Our application code (which wraps the
Runner) logs that theRunnerprocessed this event and recognized thetransfer_to_agentrequest. - However, the target agent (
CustomUserInfoAgent_NativeLLM) is not automatically invoked by theRunnerin the samerun_async()cycle. Therun_async()call completes after processing the orchestrator's event. - If a second message is sent from the client (triggering a new
run_async()call), theRunner's_find_agent_to_runmethod still selects the original orchestrator, not the target agent. This is because_find_agent_to_runappears to primarily use theauthorof the last non-user event to determine the next agent, and it doesn't seem to directly act on atransfer_to_agentvalue stored in a previous event's actions within the session history forBaseAgents. - We also tried setting the
authorof the transfer event to the target agent's name. This also did not result in the target agent running on the next turn; the_find_agent_to_runlogic still defaulted to the root agent because the target (aBaseAgent) failed the_is_transferable_across_agent_treecheck (which expectsLlmAgent). - Modifying the target agent to be an
LlmAgentstill did not result in an automatic chained invocation by theRunnerin the same turn.
This leads to our E2E tests timing out, as they expect the target agent to respond immediately after the transfer is signaled by the orchestrator.
Our Current Understanding/Hypothesis:
Based on documentation review (Multi-Agent Systems page) and preliminary Runner source code inspection (runners.py), it seems:
transfer_to_agent(when set inEventActionsby aBaseAgent) primarily serves to record the intent for transfer in the session.- The
Runnerdoes not automatically chain execution to this new target agent within the samerun_async()call that processed the transfer event. - The logic in
_find_agent_to_runfor determining the next agent to execute in a newrun_async()call relies onevent.authorand specific conditions (like_is_transferable_across_agent_treeforLlmAgents) and may not directly use atransfer_to_agentfrom a previous event's actions to pick the next agent if the author doesn't align with these conditions.
Questions for Clarification:
- What is the precise intended mechanism by which the
Runnershould pick up and execute an agent specified in a previous event'sactions.transfer_to_agentwhen the nextrun_async()call occurs (for bothBaseAgentandLlmAgenttargets)? - Does the
Runnersupport automatic chained invocation (i.e., the target agent runs immediately after the transferring agent's turn, within the same initialrun_async()context/loop) whentransfer_to_agentis used by aBaseAgent? Or is a new external stimulus/run_async()call always required? - If a new
run_async()call is required, how does_find_agent_to_runcorrectly identify the agent specified in the priortransfer_to_agentif the last event'sauthoris still the transferring agent (which might not pass_is_transferable_across_agent_treeif it's aBaseAgent)? - What is the recommended pattern for a
BaseAgent(acting as an orchestrator) to reliably hand off control to anotherBaseAgent(a sub-agent) such that the sub-agent runs immediately without requiring an intervening user message? Is direct invocation (target_agent._run_async_impl(ctx)) from the orchestrator the only way, or is there aRunner-mediated approach?
We are trying to achieve a seamless user experience where sub-tasks handled by different agents can flow automatically. Any guidance on the canonical ADK patterns for this, especially concerning BaseAgent orchestration and the Runner's role in transfer_to_agent, would be immensely helpful.
Thank you!
We have been using sub agents and observed automatic transfer, so I wonder if there is some detail in your implementation that is breaking something. It might be worth testing the behaviour using the adk-samples agents to see if you see the same thing.
Caveat: not a googler, just using the library.
What type of agent setup are you trying ? LLMAgent, Sequential , Parallel or custom ?
Mostly just LLMAgent. We have some use of Sequential but I would need to verify transfer is working as expected.
@calvingiles does your LLMAgent takes decision dynamically based on queries? If so, have you configured instructions/planner specifically to drive this for generic usecase?
They do - our instructions explain the situations in which to switch to a specific agent and when to switch back.
There are examples of this is samples - e.g. data_science
Does that help? Otherwise I can share some of ours.
LLM-based agent transfer/handoff is mainly affected by 3 things:
- LlmAgent.name
- LlmAgent.description
- LlmAgent.instruction
LlmAgent.name:
When end-user says something like "help me process data of ....". An agent named as data_processing_agent will likely work better than naming the agent as agent_<an uuid>
LlmAgent.description:
This field plays critical role for LLM to decide the scope of work of an agent and transfer the control to it.
LlmAgent.instruction
In instruction, you can add additional prompt like
call \transfer_to_agent` tool` when user ask for something_a, something_b, ...
Finally, you can check the log to see the final prompt of the outgoing LlmRequest and you'll have better understanding how these fields are mapped to the final prompt as system instruction.
Below is a sample (hello_world_ma), where this agent has two sub-agents.
...
You are an agent. Your internal name is "root_agent".
You have a list of other agents to transfer to:
Agent name: roll_agent
Agent description: Handles rolling dice of different sizes.
Agent name: prime_agent
Agent description: Handles checking if numbers are prime.
If you are the best to answer the question according to your description, you
can answer it.
If another agent is better for answering the question according to its
description, call `transfer_to_agent` function to transfer the
question to that agent. When transferring, do not generate any text other than
the function call.