fierce-jordan-44682
09/26/2025, 9:16 AMtall-airline-16756
09/26/2025, 9:16 AMtall-airline-16756
09/26/2025, 9:17 AMdeparture_timeout setting, which decides how long the room remains available after the last participant leaves.
Also, if a participant’s token expires while they’re still in the room, their session won’t end right away. LiveKit has ways, like token refresh, to keep the session going smoothly. The room will only close once the departure_timeout period has passed after the last person leaves, no matter what happens with token expiration.
Sources: livekit-api/src/services/room.rs | livekit-api/livekit/api/access_token.py | livekit/livekit_models.pb.go | Authentication | LiveKit Docs | config-sample.yamlfierce-jordan-44682
09/26/2025, 9:21 AMtall-airline-16756
09/26/2025, 9:21 AMfierce-jordan-44682
09/26/2025, 9:30 AMtall-airline-16756
09/26/2025, 9:31 AMfierce-jordan-44682
09/26/2025, 9:31 AMtall-airline-16756
09/26/2025, 9:32 AMconst at = new AccessToken(process.env.LIVEKIT_API_KEY, process.env.LIVEKIT_API_SECRET, {
identity: participantName,
ttl: '10m', // Token expires after 10 minutes
});
at.addGrant({ roomJoin: true, room: roomName });
After the token expires, the user won’t be able to join the room again unless your backend issues a new token for them.
If you ever need to remove a participant before their token expires, you can use the RemoveParticipant API. Just keep in mind, this doesn’t invalidate the token itself, so the participant could potentially rejoin if their token is still valid. To avoid that, make sure your backend doesn’t issue a new token to that participant.
Sources: Authentication | LiveKit Docs | Generating tokens | LiveKit Docs | Managing participants | LiveKit Docsfierce-jordan-44682
09/26/2025, 9:40 AMtall-airline-16756
09/26/2025, 9:41 AMdeparture_timeout setting for you. This setting’s maximum limit is based on its data type, which is a uint32. That means it can handle values up to 4,294,967,295 seconds — that’s roughly 136 years! Of course, in real-world use, most people stick to much shorter, more manageable durations.
By default, departure_timeout is set to 20 seconds, as you can see in the sample configuration. While you’re free to set it higher, just keep in mind that very long timeouts might not be the most practical choice and could affect how your server manages resources. If you have any questions about what duration might work best for you, I’m here to help!
Sources: livekit/livekit_room.pb.go | pkg/config/config.gofierce-jordan-44682
09/26/2025, 9:42 AMtall-airline-16756
09/26/2025, 9:43 AMdeparture_timeout, which is set to 20 seconds by default. During this time, the server keeps just enough information, like metadata and the room’s state, so that participants can reconnect smoothly if needed. However, any active media processing, like audio or video streaming, will stop as soon as the last participant leaves.
After the departure_timeout period ends, the room will automatically close and free up its resources, including the memory used for the room object and its internal state. If for some reason the room stays open longer—maybe due to server hooks or agents—it will keep using memory until you explicitly delete it.
To help keep things running efficiently, you can also delete the room programmatically using the server API right after the session ends. This way, you make sure resources are freed up immediately. If you have any questions about how to do that, I’m here to help!
Sources: pkg/rtc/room.go | Rooms, participants, and tracks | LiveKit Docs | Docs::Agentsfierce-jordan-44682
09/26/2025, 9:52 AMtall-airline-16756
09/26/2025, 9:52 AMfierce-jordan-44682
09/26/2025, 9:53 AMtall-airline-16756
09/26/2025, 9:54 AMhandle = session.say("Goodbye for now.", allow_interruptions=False)
handle.waitForPlayout().then(async () => {
await new Promise((resolve) => setTimeout(resolve, 500));
await hangUpCall();
});
In this example, hangUpCall() is where you would implement the room deletion using the LiveKit API:
async def hangup_call():
ctx = get_job_context()
if ctx is None:
return
await ctx.api.room.delete_room(
api.DeleteRoomRequest(
room=ctx.room.name,
)
)
This way, you can be sure the LLM’s closing remark is delivered warmly and clearly before the room closes. If you have any questions or need further help, feel free to ask—I’m here to support you!
Sources: Docs::Agents | Docs::Agents | Agents telephony integration | LiveKit Docsfierce-jordan-44682
09/26/2025, 10:00 AMtall-airline-16756
09/26/2025, 10:01 AMon_exit method in the LiveKit AgentSession. This method is called right before the session closes, making it a perfect spot to have the LLM deliver a heartfelt farewell message.
Here’s a simple way to set it up:
1. Inside the on_exit method, use session.generate_reply() to create a thoughtful closing remark, like this:
async def on_exit(self):
await self.session.generate_reply(instructions="Say a heartfelt goodbye and summarize the conversation.")
2. Once the closing speech is delivered, you can delete the room using the LiveKit API:
job_ctx = get_job_context()
await job_ctx.api.room.delete_room(api.DeleteRoomRequest(room=job_ctx.room.name))
3. If you want to tie this into FastAPI, you can create an endpoint that triggers the session shutdown, which will then call the on_exit method. For example:
@router.post("/close-room")
async def close_room():
await agent_session.shutdown()
return {"status": "room closing"}
This way, the LLM gets to share a nice closing message before the room closes. If you’d like, I’m here to help you with any part of the implementation or answer any questions you have!
Sources: realtime/openai-realtime-tools.pyfierce-jordan-44682
09/26/2025, 10:02 AMtall-airline-16756
09/26/2025, 10:02 AMagent_session.shutdown() method actually just disconnects the agent from the room—it doesn’t close the room completely. If your goal is to close the room, you’ll want to use the LiveKit API’s delete_room() method instead.
Here’s a simple example in Python to help you out:
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,
))
Also, if you'd like the room to be deleted automatically when the agent session ends, you can set this up using delete_room_on_close in the RoomInputOptions. Just let me know if you'd like me to walk you through that configuration—I’m here to help!
Sources: livekit-agents/livekit/agents/voice/room_io/room_io.py | Anatomy of an Agent | LiveKit Docs