creamy-byte-34126
08/09/2025, 9:32 PMtall-airline-16756
08/09/2025, 9:32 PMtall-airline-16756
08/09/2025, 9:33 PMCloseEvent
and set up a callback on the AgentSession
. Here’s a simple way to do it:
from livekit.agents import AgentSession, CloseEvent
async def on_session_close(event: CloseEvent):
print(f"Session closed. Reason: {event.reason}")
print(f"Closed at: {event.created_at}")
# Create and start your AgentSession
session = AgentSession(...)
await session.start(...)
# Attach the close event handler
session.on("close")(on_session_close)
Quick rundown:
• CloseEvent: This gives you the reason
why the session ended and the created_at
timestamp for when it happened.
• Event Handling: By listening to the close
event on your AgentSession
, you get these details right when the call ends.
This way, you can easily keep track of and log call closures. If you have any questions or want me to walk you through it, just let me know—I’m here to help!
Sources: tests/test_agent_session.pycreamy-byte-34126
08/09/2025, 9:58 PMRoomInputOptions.close_on_disconnect=False
) {"participant": "sip_905385569470", "reason": "CLIENT_INITIATED", "pid": 13581, "job_id": "AJ_n6bTL4NghX65"}
I wanna capture this eventtall-airline-16756
08/09/2025, 9:59 PMparticipant_disconnected
event handler in LiveKit's Python SDK. Here's a friendly example to get you started:
import asyncio
import logging
from livekit import rtc, api
# Set up logging
logging.basicConfig(level=<http://logging.INFO|logging.INFO>)
logger = logging.getLogger(__name__)
# Initialize the room and set up the event handler
room = rtc.Room()
@room.on("participant_disconnected")
async def on_participant_disconnected(participant: rtc.RemoteParticipant):
<http://logger.info|logger.info>(f"Participant disconnected: {participant.sid} {participant.identity}")
<http://logger.info|logger.info>(f"Disconnect reason: {participant.disconnect_reason}")
async def main():
# Create an access token for the room
token = (
api.AccessToken()
.with_identity("your-identity")
.with_room("your-room-name")
.to_jwt()
)
# Connect to the room
await room.connect("your-livekit-url", token)
<http://logger.info|logger.info>("Connected to the room")
# Keep the connection alive
while True:
await asyncio.sleep(1)
# Run the main async function
asyncio.run(main())
Here's a quick rundown:
• Event Handling: The @room.on("participant_disconnected")
decorator listens for when someone leaves the room.
• Logging: It logs the participant's SID, their identity, and why they disconnected—super useful info!
• Connection: This part connects you to your LiveKit room using an access token.
This setup should help you track participant disconnections smoothly. If you have any questions or want to dive deeper, just let me know—I’m here to help!