I wanna capture the exact time when user or agent ...
# ask-ai
c
I wanna capture the exact time when user or agent close the phone in python
t
Thanks for reaching out, Yusuf. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! If you want to capture the exact moment when a user or agent closes a phone call in Python using LiveKit, you can use the
CloseEvent
and set up a callback on the
AgentSession
. Here’s a simple way to do it:
Copy code
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.py
c
2025-08-10 005807,923 - INFO livekit.agents - closing agent session due to participant disconnect (disable via
RoomInputOptions.close_on_disconnect=False
) {"participant": "sip_905385569470", "reason": "CLIENT_INITIATED", "pid": 13581, "job_id": "AJ_n6bTL4NghX65"} I wanna capture this event
t
Hey there! To catch when a participant disconnects, you can use the
participant_disconnected
event handler in LiveKit's Python SDK. Here's a friendly example to get you started:
Copy code
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!