miniature-nail-35141
11/30/2025, 8:16 AMtall-airline-16756
11/30/2025, 8:16 AMtall-airline-16756
11/30/2025, 8:16 AMDisconnected event and check the disconnect reason (e.g., network, removed, duplicate identity). Use that to decide whether to attempt reconnect logic or start cleanup.
1. Persist important state in-session
• Keep session/task state in the session userdata object so it’s accessible from shutdown hooks or other server-side callbacks:
◦ Store minimal, serializable fields (user id, current task id, progress, partial results).
1. Register shutdown hooks to save state
• Use add_shutdown_callback() to register a short async function that persists userdata, transcripts, and any task state to your DB or durable store.
• Ensure shutdown hook work completes quickly (default timeout ~60s).
1. On disconnect, perform graceful shutdown
• Call session.shutdown() to run shutdown hooks and finalize session state.
• Optionally call the server API to delete_room() if you need to forcibly end the room and disconnect remaining participants or stop egress/recording.
1. Generate and store session reports
• In on_session_end, call ctx.make_session_report() (or equivalent) and save the report JSON—this includes session ids, conversation history, events, and recording metadata for post-mortem or replay.
1. Recovery / rejoin strategy
• If you want to allow users to rejoin:
◦ Persist enough state (task id, last acknowledged step) so the client can resume.
◦ When the user reconnects, restore userdata into the new session and continue from saved progress.
Summary flow
1. Detect disconnect → 2. Call session.shutdown() → 3. Run shutdown hooks to save userdata and task data → 4. Save session report → 5. Optionally delete room or let it expire.
If you’d like, tell me which SDK/language you’re using (JavaScript, Python, or server API) and I’ll give a short code example for the exact hooks and calls.
Sources: Connecting to LiveKit | LiveKit docs | Job lifecycle | LiveKit docs | Agent session | LiveKit docs | Agents and handoffs | LiveKit docs | Observability data hooks | LiveKit docs