miniature-toddler-48848
12/10/2025, 1:07 AMtall-airline-16756
12/10/2025, 1:07 AMtall-airline-16756
12/10/2025, 1:07 AMsession.shutdown() or session.aclose() disconnects the agent session and cleans up its internal state, but the participant can remain in the room unless you explicitly remove it.
What you can do
• If you want to remove the participant only: call the RemoveParticipant API with a room admin credential.
• If you want to end the whole room when the agent leaves: call the DeleteRoom API.
• Add a shutdown/cleanup hook (for example via ctx.add_shutdown_callback) to run the chosen removal call when the page unloads or the session ends.
Would you like implementation examples for the RemoveParticipant or DeleteRoom calls in your language (Python/JS)?
Sources: Job lifecycle | LiveKit docs | Managing participants | LiveKit docsminiature-toddler-48848
12/10/2025, 1:09 AMtall-airline-16756
12/10/2025, 1:09 AMminiature-toddler-48848
12/10/2025, 1:09 AMtall-airline-16756
12/10/2025, 1:10 AMAgentSession to an existing disconnected agent participant is not supported. The lingering participant is for visibility, not for session resurrection.
Why:
• An AgentSession is a transient server-side process tied to a single job and its runtime state; when it ends the session state is destroyed.
• The agent participant may remain in the room in a DISCONNECTED state for visibility and reconnect semantics for clients, but it cannot be reactivated or reattached to a new AgentSession.
What to do instead:
• Persist any conversation or agent state externally (database or userdata) and load it into a newly created AgentSession when needed.
• If you want to avoid orphaned participants, run cleanup on session shutdown (call RemoveParticipant for the agent or DeleteRoom if appropriate).
• If you want seamless UX for reloads, detect a disconnected participant and either recreate a fresh session for that identity or remove-and-recreate based on your reconnection policy.
Would you like a short example for:
• persisting/restoring userdata for a new AgentSession, or
• a page-unload hook that calls RemoveParticipant/DeleteRoom? Sources: Managing participants | LiveKit docs | Agent session | LiveKit docs | Agents and handoffs | LiveKit docsminiature-toddler-48848
12/10/2025, 1:11 AMtall-airline-16756
12/10/2025, 1:12 AMlk.agent.state participant attribute (values like initializing, listening, thinking, speaking). If this attribute is present and in an active state (listening, thinking, speaking) the agent is active.
• AgentSession lifecycle signals: have your agent code emit or persist session-close events (the session close event) to your backend so you can mark the session inactive in a database or cache.
Recommended decision flow
1. On user rejoin, fetch the agent participant (by identity) and check:
◦ If participant.state === ACTIVE and lk.agent.state ∈ {listening,thinking,speaking} → treat as active; do not dispatch a new agent.
◦ If participant.state === DISCONNECTED or lk.agent.state is missing/stale → treat as likely zombie; consider cleanup and dispatching a new agent.
1. If uncertain, consult your backend session record (set when AgentSession starts/stops). Prefer authoritative backend state over room-only signals.
2. Cleanup options when you detect a zombie:
◦ Call RemoveParticipant for that agent (room admin API) to remove the lingering participant.
◦ Or delete the room if the whole room should end.
Best practices
• Persist minimal session state (session id, agent job status, timestamps) in your backend when creating an AgentSession; update on close → makes rejoin decisions deterministic.
• Use lk.agent.state for near-real-time liveness checks, but rely on backend state for final dispatch decisions.
• Use shutdown hooks in agent code to call cleanup (RemoveParticipant or notify backend) on session end so zombies are less likely.
If you want, I can:
• Show a short Node.js or Python snippet that implements the decision flow (checking participant.state, lk.agent.state, and falling back to backend state), or
• Provide an example webhook/backend schema for tracking AgentSession lifecycle. Which would help you most? Sources: Turn detection and interruptions | LiveKit Docs | Webhooks | LiveKit docs | Handling events | LiveKit docs | Agent session | LiveKit docs | Agents v0.x migration guide - Python | LiveKit docsminiature-toddler-48848
12/10/2025, 1:59 AMtall-airline-16756
12/10/2025, 2:00 AMinitializing, listening, thinking, speaking)
If state is ACTIVE and lk.agent.state is one of the active values, treat the agent as active.
1. Maintain backend session records:
◦ When creating an AgentSession, persist a record (session id, agent identity, job id, startedAt).
◦ On agent `close`/shutdown handlers, update that record to mark the session ended (endedAt, status).
◦ Prefer this backend record as the authoritative source when deciding whether to dispatch a new agent.
1. Use webhooks or agent lifecycle events to keep backend state in sync:
◦ Have the agent invoke a backend endpoint or emit a webhook on session start and on session close.
◦ If you observe participant disconnect events without a backend close, treat it as an unexpected disconnect and either mark as inactive after a short timeout or attempt cleanup.
1. Decision logic (example)
◦ If backend record shows session is active → do not dispatch.
◦ Else if backend record shows ended → dispatch new agent.
◦ If backend record is missing/untrusted → fall back to participant checks:
▪︎ If participant.state === 'ACTIVE' and lk.agent.state ∈ {listening,thinking,speaking} → treat active and reconcile backend.
▪︎ Otherwise treat as zombie: remove participant or dispatch new agent and update backend.
Quick Node.js hints
• Use LiveKit server SDK or REST API to list/get participants and read attributes.
• Accept agent-side lifecycle callbacks (HTTP endpoint) for start/close to update your DB.
• Add a short safety timeout when participant disconnects unexpectedly before auto-dispatching, to handle transient reconnects.
If you’d like, I can provide a concise Node.js code snippet showing:
• fetching a participant and reading lk.agent.state, and
• an example endpoint that the agent calls on session close to update backend state. Which snippet would help?miniature-toddler-48848
12/10/2025, 2:02 AMtall-airline-16756
12/10/2025, 2:02 AMlk.agent.state is reliable while the AgentSession is actively running, but it is not safe to use as the single source of truth because it can become stale if the agent process crashes or disconnects unexpectedly.
Why not atomic on close
• On graceful shutdown the agent can update lk.agent.state before disconnecting.
• If the process is killed, crashes, or loses network, it cannot update LiveKit; the attribute remains at its last value (e.g., thinking), producing a "zombie" participant until server timeouts clean it up.
Safe, trusted approach (one source of truth)
1. Use your backend as the authoritative source:
◦ Record session start (agent id/identity, room, session id, startedAt).
◦ Update on session end (endedAt, status) when you receive an explicit lifecycle signal.
1. Keep lk.agent.state for near-real-time UI/liveness hints, not as authoritative state.
2. Sync via webhooks / agent callbacks:
◦ Have the agent POST to your backend on session start and on close.
◦ Also subscribe to participant_left webhooks to reconcile unexpected disconnects.
1. Add guarded cleanup:
◦ If webhooks/callbacks are delayed or missing, run a short expiry/heartbeat policy (e.g., mark inactive after 20–30s with no heartbeat).
1. Decision logic for dispatch:
◦ If backend record = active → do not dispatch.
◦ If backend record = ended → dispatch.
◦ If backend missing → consult participant.state + lk.agent.state as a fallback, then reconcile and update backend.
If you want, I can provide a concise Node.js snippet that:
• fetches a participant and reads lk.agent.state, and
• shows an endpoint the agent calls on close to update backend session state. Which snippet would you prefer (participant-check, webhook handler, or both)? Sources: Agents Framework | livekit/livekit_composite | DeepWiki | Web and mobile frontends | LiveKit docs | Job lifecycle | LiveKit docs | Understanding the STATE_MISMATCH Disconnect Reason | Server-side | livekit/livekit_composite | DeepWikiminiature-toddler-48848
12/10/2025, 2:03 AMtall-airline-16756
12/10/2025, 2:03 AMlk.agent.state is absent/stale.
• On marking inactive, optionally call RemoveParticipant to clean the room.
1. Use participant attributes as a hint, not the source of truth
• Read lk.agent.state and participant.state from LiveKit to help decide—but authoritative truth comes from your DB which is updated by webhooks/heartbeats/cleanup.
Example decision flow (pseudo)
• If DB.status === "active" and lastHeartbeatAt is recent → agent active.
• Else if webhook says participant_left → set DB.status = "ended".
• Else if no heartbeat and participant.state !== ACTIVE or lk.agent.state missing/stale → expire session in DB and treat as ended.
Quick Node.js sketch ideas I can provide
• webhook handler for participant_left that updates DB
• simple heartbeat endpoint the agent calls
• cleanup job that queries DB and LiveKit participants, then expires stale sessions
Which snippet would you like first: webhook handler, heartbeat + DB update, or the cleanup job?miniature-toddler-48848
12/10/2025, 2:04 AMtall-airline-16756
12/10/2025, 2:05 AMparticipant.state (ACTIVE vs DISCONNECTED) and attributes['lk.agent.state'].
◦ If participant.state === ACTIVE and lk.agent.state ∈ {listening,thinking,speaking} → treat as active.
1. If both checks are inconclusive, consider it not active and dispatch a new agent, optionally removing the stale participant.
Role of heartbeats
• Heartbeats make the DB check fast and authoritative on reload. A short heartbeat interval (5–10s) with a short expiry (20–30s) lets your backend reliably answer instantly.
• Without heartbeats you must rely on LiveKit participant state/attributes which can be stale after crashes, so you risk false positives.
Recommended minimal implementation for speed
• On page reload: call your backend endpoint that:
a. Looks up DB record (fast in a single DB read).
b. If needed, queries LiveKit participant attributes (single API call).
c. Returns a boolean: active vs not active.
• Use that boolean to decide immediately.
If you want, I can provide a concise Node.js snippet for that backend endpoint (DB check + LiveKit participant read). Which DB are you using?