creamy-eye-58634
08/02/2025, 10:52 AMtall-airline-16756
08/02/2025, 10:52 AMtall-airline-16756
08/02/2025, 10:53 AMcreamy-eye-58634
08/02/2025, 10:54 AMtall-airline-16756
08/02/2025, 10:54 AMupdate_agent() Method:
You can simply call the update_agent() method on the session to switch to a different agent immediately. Here’s a quick example:
from livekit.agents import Agent
# Let’s say you have a CustomerServiceAgent class defined
session.update_agent(CustomerServiceAgent())
2. Returning a Different Agent from a Tool Call:
Another way is to return a new agent instance right from a tool call, which hands off control automatically. Like this:
from livekit.agents import Agent, function_tool
class ConsentCollector(Agent):
@function_tool()
async def on_consent_given(self):
# Hand off by returning the new agent
return HelpfulAssistant()
Just make sure the new agent is set up properly and has everything it needs to keep the conversation flowing smoothly. If you want, I’m here to help you with the implementation—just let me know!
Sources: Workflows | LiveKit Docs | Workflows | LiveKit Docscreamy-eye-58634
08/02/2025, 10:54 AMtall-airline-16756
08/02/2025, 10:55 AMsession.update_agent() to hand things over, but the reply needs to come from the agent on the other end. Hope that clears things up!
Sources: Workflows | LiveKit Docs | Dispatching agents | LiveKit Docscreamy-eye-58634
08/02/2025, 10:56 AMtall-airline-16756
08/02/2025, 10:56 AMfrom livekit.agents import Agent
class InitialAgent(Agent):
async def on_user_turn_completed(self, turn_ctx, new_message):
if "transfer" in new_message.text_content.lower():
# Transfer to another agent
session.update_agent(SecondAgent())
class SecondAgent(Agent):
async def on_enter(self):
# Automatically respond to the user upon taking over
await self.session.generate_reply("I am the new agent handling your query.")
Here’s what’s happening:
• The InitialAgent looks out for a trigger word like "transfer" in the message and then switches control over to SecondAgent.
• Once SecondAgent takes over, it immediately replies to the user using generate_reply().
If you want me to walk you through this more or need other examples, just let me know—I’m here to help!creamy-eye-58634
08/02/2025, 10:57 AMtall-airline-16756
08/02/2025, 10:57 AMcommit_user_turn or clear_user_turn to take charge of when the LLM responds during the transfer.
3. Interrupt Handling: The interrupt method is great for stopping any ongoing speech and quickly prompting the LLM to reply right away.
4. Event-Driven Triggers: Keep an ear out for state changes (like when an agent session starts or ends) to nudge the LLM to respond exactly when it’s needed during the handoff.
For instance, you might have the LLM say something like "Now transferring you to another agent" while the handoff is happening. If you'd like, I’d be happy to walk you through a detailed example on how to set this up!
Sources: Tool definition and use | LiveKit Docscreamy-eye-58634
08/02/2025, 10:58 AM