important-pharmacist-78974
09/10/2025, 5:35 PMtall-airline-16756
09/10/2025, 5:35 PMtall-airline-16756
09/10/2025, 5:36 PMfunction_tool
decorator to define a tool that returns a new agent instance. For example:
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:
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 Docsimportant-pharmacist-78974
09/10/2025, 5:37 PMtall-airline-16756
09/10/2025, 5:38 PMimportant-pharmacist-78974
09/10/2025, 5:41 PMtall-airline-16756
09/10/2025, 5:41 PM@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:
@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:
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