How do I pass tool responses to another agent
# ask-ai
i
How do I pass tool responses to another agent
t
Thanks for reaching out, Spencer. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm glad you're exploring how to pass tool responses to another agent in LiveKit! You can achieve this smoothly using the agent handoff mechanism, which lets one agent transfer control along with the tool's return value to another agent. Here's a friendly guide on how it works: • In Python: You can use the
function_tool
decorator to define a tool that returns a new agent instance. For example:
Copy code
from livekit.agents import function_tool, Agent, RunContext

@function_tool()
async def transfer_to_reservation_agent(context: RunContext) -> tuple[Agent, str]:
    next_agent = ReservationAgent()  # Assume it's defined elsewhere
    return next_agent, "Transferring the user to the reservation agent."
In Node.js: You can use
llm.handoff
to return an object with the next agent and an optional return value. Here's an example:
Copy code
const playGame = llm.tool({
  description: 'Transfer user to a game agent.',
  execute: async (): Promise<llm.agenthandoff> => {
    return llm.handoff({ agent: gameAgent, returns: 'The game is now playing.' });
  },
});
Also, to keep things seamless, you can preserve chat history and user data during the handoff by using a shared
UserData
class. This helps maintain continuity as the user moves between agents. If you'd like, I'm here to provide more details or additional examples to help you along the way! Sources: examples/src/restaurant_agent.ts | Building Multi-Agent Architectures with LiveKit Agents | examples/voice_agents/restaurant_agent.py | Tool definition and use | LiveKit Docs
i
How do I share mcp tool outputs with another agent
t
I'd love to help you find the best way to share MCP tool outputs between agents. To get a clearer picture, could you please share a bit more about the following? • Are the agents working within the same session, or are they operating in different sessions? • Do you need the outputs to be shared in real-time (like streaming), or would it be okay to pass the results once a tool call is finished? • Is your goal to hand off control to another agent, or just to share data while both agents stay active? Sources: Tool definition and use | LiveKit Docs | Tool definition and use | LiveKit Docs | Remote method calls (RPC) | LiveKit Docs | README.md
i
The goal Is to hand off to another agent. Right now when transfer takes place, the 2nd agent looses function tool outputs
t
To help you retain function tool outputs during an agent handoff in LiveKit, you can include the tool output as part of the handoff process. Here’s a friendly guide on how to do this: • In Python: You can return a tuple containing the new agent and the tool output. For example:
Copy code
@function_tool()
async def my_tool(self):
    # Perform logic
    return SomeAgent(), "Tool result to pass to next agent"
In Node.js: You can use the
handoff()
utility to return the agent and result. For example:
Copy code
@function_tool()
async myTool() {
    // Perform logic
    return handoff({ agent: new SomeAgent(), returns: "Tool result to pass to next agent" });
}
Also, to make sure the full conversation history (including previous tool outputs) is preserved, simply pass the current
chat_ctx
when initializing the new agent. Like this:
Copy code
return TechnicalSupportAgent(chat_ctx=self.session.chat_ctx)
This way, the new agent will have access to all previous interactions and tool outputs, helping everything run smoothly. If you have any questions or need more help, please don’t hesitate to reach out—I’m here for you! Sources: Tool definition and use | LiveKit Docs | Workflows | LiveKit Docs