liveBook · Manning

Transfer_to_agent tool

As explained in Build an AI Agent (From Scratch)

The transfer_to_agent tool is a callable capability that hands control from the current agent to a selected target agent.

The transfer_to_agent tool lets an agent hand a request to a better-suited agent. The LLM selects the tool just as it would any other tool, but the call changes who runs next: the selected agent takes over the shared conversation rather than returning a result to the original agent. section 9.5.2 section 9.7

Build the callable capability from the current agent’s allowed targets:

  • Connect agents through sub_agents and parent references. Targets are children, the parent, and optionally siblings; names identify agents during transfer. section 9.5.3
  • Create a FunctionTool named transfer_to_agent. Put each target’s name and role in its description so the LLM can choose a specialist, and constrain agent_name to valid target names with an enum. section 9.5.4
  • Make the tool set context.transfer_to to the chosen name. The tool records the decision; it does not run the next agent itself. section 9.5.4
{
    "name": "transfer_to_agent",
    "description": "Transfers work to another agent...",
    "parameters": {
        "type": "object",
        "properties": {
            "agent_name": {
                "type": "string",
                "enum": ["math", "search"]  # Only valid targets
            }
        },
        "required": ["agent_name"]
    }
}

The generated tool schema shows the agent_name enum restricting the LLM’s choice to the current agent’s available targets. section 9.5.4

Wire the tool into the existing agent loop:

  • At the start of run(), create the current agent’s transfer tool if needed and cache it by agent name in context.transfer_tools. section 9.5.3 section 9.5.5
  • Include the cached tool in the LLM request and in act()’s tool lookup. Otherwise the LLM cannot select it or the agent cannot execute its call. section 9.5.5
  • After a step, check context.transfer_to. Find the named agent, clear the flag, call its run() with the same context and no new user input, then exit the current agent’s loop. The transferred agent leaves its result in context.final_result. section 9.5.5
Listing 9.18 Detecting and executing the transfer
# Execution loop inside run()
while not context.final_result and context.current_step < self.max_steps:
    await self.step(context)

    # Check for transfer
    if context.transfer_to:
        next_agent = self._find_agent(context.transfer_to)
        context.transfer_to = None  # Reset flag

        if next_agent:

            # Delegate (pass only context, no user_input)
            await next_agent.run(None, context, verbose)
            break  # Exit current agent's loop

    # Check for final response

The run-loop listing makes the distinction between recording a transfer request and actually handing off execution explicit. section 9.5.5

Routing a specialist request

A router with math and search sub-agents receives a calculation request. It calls transfer_to_agent with agent_name set to math. The math agent continues with the existing conversation context and completes the task. If a later request needs another specialty, the agent tree permits a transfer back to the router or, when peer transfers are enabled, to a sibling. section 9.5.3 section 9.5.2

Keep transfer relationships explicit. Agent-tree validation rejects duplicate sub-agent names and agents assigned multiple parents; an option can prevent peer transfers. Unlike Agent as Tool, transfer does not preserve the original agent as the active decision-maker waiting for a specialist’s result. section 9.5.3 section 9.2

How Build an AI Agent (From Scratch) covers it

How deep it goes
Implements target selection, tool generation, registration, execution, and run-loop handoff using shared execution state.
Examples
Uses a router with math and search specialists, plus a generated tool schema and run-loop listings.
What it assumes
Builds on an existing Agent, tool system, and ExecutionContext while constructing the transfer mechanism step by step.
What you can do after
Build it from scratch by generating a target-constrained tool and integrating its handoff into an agent’s run loop.
How it connects
Connects the Transfer pattern to agent trees, tool calling, ExecutionContext, and Agent as Tool.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build an AI Agent (From Scratch). The section links point to the parts of the book it draws on.