I would like for the agent to disconnect the room ...
# ask-ai
a
I would like for the agent to disconnect the room when they assess that they have all the information they need based on their instruction, what is the best way to do that?
t
Thanks for reaching out, Anne. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
When it’s time to disconnect a room in LiveKit after the agent has gathered all the info they need, you’ve got two handy options: • Disconnect Only the Agent: Simply use the
ctx.shutdown()
method with a reason message. This way, the other participants can keep the session going without any interruption. Here’s how you can do it in Python:
Copy code
async def entrypoint(ctx: JobContext):
    # Do some work...
    # Disconnect from the room
    ctx.shutdown(reason="Session ended")
Disconnect All Participants and Delete the Room: If you want to close things down completely, use the
delete_room
method from the LiveKit API to remove the room and disconnect everyone. Here’s an example in Python:
Copy code
from livekit import api

async def entrypoint(ctx: JobContext):
    # Do some work...
    api_client = api.LiveKitAPI(
        os.getenv("LIVEKIT_URL"),
        os.getenv("LIVEKIT_API_KEY"),
        os.getenv("LIVEKIT_API_SECRET"),
    )
    await api_client.room.delete_room(api.DeleteRoomRequest(room=ctx.job.room.name))
If you have any questions or need a hand with these methods, just let me know—I’m here to help! Sources: Managing rooms | LiveKit Docs | Anatomy of an Agent | LiveKit Docs | Job lifecycle | LiveKit Docs
a
is there a way to know when agent went through all the 'script'?
t
Hey there! To keep track of when an agent has wrapped up a script in LiveKit, here are a few handy ways you can do it: • ResponseDoneEvent: This event pops up once an agent’s response is fully processed, letting you know the script execution is all done. Here’s a quick example:
Copy code
onEvent("response.done", (event: ResponseDoneEvent) => {
    console.log("Script completed:", event);
});
Monitoring Agent Speech Events: You can also listen for events like
VPAEvent.AGENT_STOPPED_SPEAKING
to catch when the agent stops talking, which usually means the script has finished. Check this out:
Copy code
if (event === VPAEvent.AGENT_STOPPED_SPEAKING) {
    console.log("Script completed");
}
RPC Playback Completion: The avatar server sends an RPC signal to the agent once the script playback wraps up, which you can use to trigger whatever you need next. If you want me to walk you through any of these or need a hand setting them up, just let me know—I’m here to help!