able-jackal-19719
08/18/2025, 9:31 AMtall-airline-16756
08/18/2025, 9:31 AMtall-airline-16756
08/18/2025, 9:32 AMdeleteRoom
method. This will wrap up the session for all participants and take down the room.
Here’s a little example to show you how it works:
from livekit import api
async def entrypoint(ctx: JobContext):
# Perform the agent's task and generate a response
# ... (agent's logic here) ...
# Disconnect all participants and close the room
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)
)
So, in this example:
• The agent does its thing and creates a response.
• Then, the deleteRoom
method is called to close the room and disconnect everyone.
If you have any questions or want me to walk you through it, just let me know—I’m here to help!able-jackal-19719
08/18/2025, 9:33 AMctx.shutdown()
? what's the difference with your solution?tall-airline-16756
08/18/2025, 9:33 AMctx.shutdown()
and the deleteRoom
API, since they serve different purposes:
• ctx.shutdown()
: This one just disconnects the agent from the room, but the room stays open for everyone else. So, if the agent’s part is done but others want to keep chatting, this is the way to go.
• deleteRoom
API: This one shuts down the whole room, kicking everyone out and removing it completely. Perfect when you want to wrap things up for everyone.
So, if you want the room to keep going after the agent leaves, ctx.shutdown()
is your friend. But if it’s time to close up shop and disconnect everyone, then deleteRoom
is the right call.
Feel free to ask if you want me to explain anything else or help out more!
Sources: Job lifecycle | LiveKit Docs | Connecting to LiveKit | LiveKit Docsable-jackal-19719
08/18/2025, 9:35 AM@session.on("close")
will be executed?tall-airline-16756
08/18/2025, 9:36 AM@session.on("close")
event kicks in:
• User-Initiated Closure: This happens when the user decides to end the conversation.
• Manual Closure: When you call session.aclose()
in your code.
• Room Deletion: If the room gets deleted, everyone gets disconnected.
• Unrecoverable Errors: When something goes wrong that the session can’t recover from.
When this event fires, you get a CloseEvent
object that gives you details like any error info and why the session closed (for example, USER_INITIATED
or ROOM_DELETED
).
Here’s a quick example:
@session.on("close")
def on_close(ev: CloseEvent):
print(f"Session closed. Reason: {ev.reason}")
# You can add any cleanup or logging you need here
This way, you can make sure everything wraps up nicely when a session ends. If you want to chat more about it or need help, just let me know!able-jackal-19719
08/18/2025, 9:37 AMtall-airline-16756
08/18/2025, 9:38 AMsession.aclose()
, ctx.shutdown()
, and deleteRoom
so it’s a bit clearer:
• session.aclose()
: This gently closes the agent's session. It only affects the agent’s connection, while the room and other participants keep going. It’s perfect when the agent’s job is done but the room should stay open.
• ctx.shutdown()
: This disconnects the agent from the room but leaves the room active for everyone else. Handy when the agent needs to step away but the session should keep running for others.
• deleteRoom
: This one deletes the whole room, kicking everyone out and removing it from the server. Use this when you want to end the session completely for everyone involved.
You can mix and match these depending on what you need. For example, you might call session.aclose()
first and then deleteRoom
to fully shut things down. If you want me to clarify anything else, just let me know—I’m here to help!
Sources: flows/declarative_flow.py | Job lifecycle | LiveKit Docs | Inside a session | LiveKit Docs