when the agent is disconnected, how can we know if...
# ask-ai
b
when the agent is disconnected, how can we know if it was the user who disconnected it
t
Thanks for reaching out, Mohammed. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To figure out if a user disconnected an agent, you can keep an eye on disconnection events and check out the reasons behind them: • Client-Side: Listen for the
participantDisconnected
event and see if
disconnectReason === CLIENT_INITIATED
. This means the user chose to disconnect. • Agent-Side: Watch for the
close
event on the
AgentSession
. If
reason === PARTICIPANT_DISCONNECTED
, it shows the user disconnected the agent. If you'd like, I’m happy to share more details or examples to help you out! Sources: livekit-rtc/livekit/rtc/_proto/participant_pb2.pyi | AI voice agents | LiveKit Docs | web/src/components/chat.tsx | web/src/components/chat.tsx | complex-agents/shopify-voice-shopper/shopify-voice-frontend/app/page.tsx
b
i have not seen any of these logs in agent logs, there is no reason shown
t
Hey there! If you’re not seeing disconnect reasons in your agent logs, no worries—here are a few easy steps to check and fix the issue: 1. First, make sure the
participantDisconnected
event listener is set up correctly in your agent implementation. 2. Double-check that your event handler is actually logging the
disconnectReason
field. For Python, that’s
participant.disconnect_reason
, and for JavaScript, it’s
participant.disconnectReason
. 3. Take a look at your agent configuration and confirm the
closeOnDisconnect
option is turned on so logs get triggered when someone disconnects. 4. Lastly, verify your LiveKit server version supports the
disconnect_reason
field within
ParticipantInfo
. If you’re still running into trouble after these steps, just give me a shout—I’m here to help you dig a little deeper! Sources: protobufs/livekit_models.proto | livekit-agents/livekit/agents/voice/room_io/room_io.py | agents/src/voice/room_io/room_io.ts
b
how do i log the disconnect reason in the agents code
t
Hey there! To help you log the disconnect reason in your agent's code, here’s a simple way to do it: 1. Keep an eye out for the
participantDisconnected
event in your agent's setup. 2. Grab the
disconnectReason
from the participant object. Here’s how it looks: JavaScript Example:
Copy code
room.on(RoomEvent.ParticipantDisconnected, (participant) => {
  console.log('Disconnect reason:', participant.disconnectReason);
});
Python Example:
Copy code
@session.on("close")
def on_close(ev: CloseEvent):
    print("Disconnect reason:", rtc.DisconnectReason.Name(ev.reason))
If you hit any bumps or want a hand with implementation or debugging, just give me a shout—I’m here to help! Sources: livekit-agents/livekit/agents/voice/room_io/room_io.py | livekit-ffi/src/livekit.proto.rs | packages/livekit-rtc/src/proto/participant_pb.ts | agent-console/lib/event-definitions.tsx | protobufs/livekit_models.proto
b
can we get the reason in the participant_disconnected room event in livekit agents?
t
Hey there! You can totally find out why someone got disconnected by checking out the
participant_disconnected
event in LiveKit agents. This event has a handy
disconnect_reason
field that tells you exactly why the disconnection happened. Python Example:
Copy code
@room.on("participant_disconnected")
def on_participant_disconnected(participant: rtc.Participant):
    logging.info(f"Participant {participant.identity} disconnected, reason: {rtc.DisconnectReason.Name(participant.disconnect_reason)}")
If you want, I’m here to help with more examples or to walk you through it in other programming languages—just let me know! Sources: livekit-rtc/livekit/rtc/_proto/participant_pb2.pyi | agent-console/lib/event-types.ts | livekit-agents/livekit/agents/voice/room_io/room_io.py | Runtime/Scripts/Proto/Participant.cs | livekit/livekit_models.pb.go