Hi all, I'm trying to implement a multi-agent con...
# ask-ai
e
Hi all, I'm trying to implement a multi-agent conversational experience where the user can speak, but several agents may respond one after the other. The core agent logic is implemented in LangGraph. I created a custom adapter and I'm using it for the LLM node for a single LiveKit agent. I'm simulating this multi-AI experience by just using this single agent and I send custom events from LangGraph to LiveKit and then to the front end that allow me distinguish between speakers. However, one issue I have is that it seems there is no way to switch the TTS node dynamically when I switch between agents. So what I'm thinking is potentially having separate Agent objects to represent each agent in my LangGraph graph, each with their own TTS node with different voices. Their responses would come from the same LangGraph endpoint, but I'm thinking I can have custom LLM adapters for each agent that just subscribes to the graph, and just filter for the tokens for their corresponding agent. If I'm able to do this though, I would need to be able to queue up messages for each of the agents and hand off from one agent to another whenever each is done speaking. Is this possible in LiveKit? I don't see any signal or callback that I can use when an agent is done speaking to automatically hand off to another agent. TL;DR: Is there a way to automatically hand off from one agent to another once the first agent is done speaking?
t
Thanks for reaching out, Jorge. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I’m glad you asked about how to automatically hand off from one agent to another when the first agent finishes speaking! You can achieve this smoothly by using the
playback_finished
event in LiveKit. This event is triggered by the
AudioOutput
instance in the agent session once audio playback is complete. Here’s a friendly step-by-step on how to set it up: 1. Start by listening for the
playback_finished
event in your agent code. This event gives you useful info like the playback position and whether the playback was interrupted. 2. Then, use this event to kick off the next agent’s response or the handoff process. For instance, you could queue messages for each agent and switch to the next one as soon as this event fires. 3. If you want to keep web clients in the loop, you might consider adding a Remote Procedure Call (RPC) in your agent to broadcast the
playback_finished
event to the room. That way, web clients can listen for this signal and act accordingly. If you’d like, I’m happy to share more details or even some code examples to help you get this working perfectly—just let me know! Sources: Pipeline nodes and hooks | LiveKit Docs | Docs::Agents | How to detect when an agent has finished speaking | Bringing AI avatars to voice agents | Bringing AI avatars to voice agents
e
Sure, can you provide me with an example code snippet of how I could hand off from the current Agent to another Agent using this PlaybackFinished callback? In the code examples that I've seen, I only see examples of handoffs that are done by the Agents themselves via a tool call, which is not what I want.
t
Here’s a friendly example of how you can use the
playback_finished
event to smoothly hand off from one agent to another:
Copy code
from livekit.agents.voice.io import PlaybackFinishedEvent

# Define a function to handle playback completion
@session.output.audio.on("playback_finished")
def handle_playback_finished(event: PlaybackFinishedEvent):
    logger.info(f"Playback finished at {event.playback_position}s, interrupted: {event.interrupted}")

    # Trigger the next agent's response
    next_agent = get_next_agent()  # Custom function to determine the next agent
    if next_agent:
        next_agent.start_response()  # Custom method to initiate the next agent's response

# Example of a custom function to determine the next agent
def get_next_agent():
    # Logic to select the next agent in the sequence
    # This could be based on a queue, round-robin, or other criteria
    return agent_queue.pop(0) if agent_queue else None
To break it down a bit: • The
playback_finished
event helps us know exactly when the current agent has finished speaking. • We use a custom function,
get_next_agent()
, to decide who should respond next. This can be based on a queue or any logic that fits your needs. • Then, we call the
start_response()
method on the next agent to get things going smoothly. If anything isn’t clear or if you’d like me to walk you through more examples, just let me know—I’m here to help! Sources: tests/test_agent_session.py | Testing and evaluation | LiveKit Docs | Bringing AI avatars to voice agents | How to detect when an agent has finished speaking
e
What I'm wondering about is what that start_response() implementation would look like.
t
It looks like
start_response()
is a custom method in your setup. Could you please share a bit more about what you'd like it to do? For instance, should it send a specific message, create a dynamic response, or maybe something else? Understanding this will help me give you a more personalized and helpful example. Sources: Agents telephony integration | LiveKit Docs | telephony/survey_caller/survey_calling_agent.py | Agent speech and audio | LiveKit Docs