Skip to content

Handoff tool: send a focused query string to sub-agent instead of full message context #265

@gama2219

Description

Current behavior
The supervisor handoff currently sends the entire message context to the sub-agent.

Problem
This increases token usage and can make the sub-agent more likely to hallucinate or misinterpret intent.

Proposal
Add an option to the handoff tool to accept a query: str so it sends just a single HumanMessage (from the query) to the sub-agent, not the full message history.

Example implementation:

def create_handoff_tool(*,
     agent_name: str,
     name: str | None = None,
     description: str|None = None,
     add_handoff_messages: bool = True,
) -> BaseTool:
    if name is None:
        name = f"transfer_to_{_normalize_agent_name(agent_name)}"

    if description is None:
        description = f"Ask agent '{agent_name}' for help"

    @tool(name, description=description)
    def handoff_to_agent(
        query: Annotated[str, f'specific query to pass to {name} agent'],
        state: Annotated[dict, InjectedState],
        tool_call_id: Annotated[str, InjectedToolCallId],
    ) -> Command:
        _message = HumanMessage(content=query)
        tool_message = ToolMessage(
            content=f"successfully transferred to {agent_name}",
            name=name,
            tool_call_id=tool_call_id,
            response_metadata={METADATA_KEY_HANDOFF_DESTINATION: agent_name},
        )
        if add_handoff_messages:
            update_message = state["messages"] + [tool_message]        
        else:
            update_message = state["messages"][:-1]
        return Command(
            graph=Command.PARENT,
            update={**state, "messages": update_message},
            goto=Send(agent_name, {"messages": [_message]}),
        )

Benefits

  • Saves tokens / cost
  • Gives the sub-agent only the relevant query, reducing hallucinations

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions