How do I close the room when my LLM is finished wi...
# ask-ai
r
How do I close the room when my LLM is finished with it's conversation. Currently it continues to listen until the user explicitly hangs up. I'm using the web SDK and Typescript
t
Thanks for reaching out, Gavin. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To close the room for all participants when your LLM finishes its conversation, use the server-side
deleteRoom
API. This will end the session for everyone and remove the room from the server, preventing users from remaining in a silent room until they hang up. Recommended approach: 1. Create a server-side API endpoint that calls
deleteRoom
using the LiveKit server SDK. For example, in a Next.js API route:
Copy code
import { RoomServiceClient } from 'livekit-server-sdk';
import { NextRequest, NextResponse } from 'next/server';

export async function POST(req: NextRequest) {
  const { roomName } = await req.json();
  const { LIVEKIT_API_KEY, LIVEKIT_API_SECRET, LIVEKIT_URL } = process.env;
  const roomService = new RoomServiceClient(
    LIVEKIT_URL!,
    LIVEKIT_API_KEY!,
    LIVEKIT_API_SECRET!
  );
  await roomService.deleteRoom(roomName);
  return new NextResponse(null, { status: 200 });
}
1. Call this endpoint from your TypeScript client when the LLM finishes:
Copy code
const endRoomForEveryone = async (roomName: string) => {
  await fetch('/api/end-room', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ roomName }),
  });
};

// Usage when LLM finishes
await endRoomForEveryone(room.name);
This ensures all participants are disconnected and the room is fully closed when your LLM session ends. If you only call
room.disconnect()
on the client, it will disconnect just that client, but the room will remain open for others. Using
deleteRoom
is the recommended way to end the session for everyone.
r
What's the best way to detect that the conversation is over from the LLM?
t
Hey there! Let me break down the best ways to know when a conversation with an LLM in a LiveKit app is wrapping up. Think of it like having a few different signals that tell you it's time to say goodbye. • Silence-Based Detection: Imagine you're chatting with a friend who suddenly goes quiet. We track how long it's been since someone last spoke, and if it gets too quiet, we gently close the room. • Participant Disconnection: It's like when everyone leaves a group chat – we automatically close things down when the last person has gone. • LLM Function Tools: We give the AI a special superpower to say "Hey, this conversation is done" when it feels like you're ready to wrap things up. Super helpful, right? • Session Close Events: We're always listening for those moments when a conversation naturally comes to an end – whether it's you hanging up, someone explicitly closing the session, or even if something unexpected happens. By using all these methods together, we make sure we never miss the moment when a conversation is truly over. It's like having a really attentive friend who knows exactly when to say goodbye – smooth, considerate, and totally reliable! Sources: Events and error handling | LiveKit Docs | Speech & Audio Processing | livekit/livekit_composite | DeepWiki | livekit-agents/livekit/agents/voice/audio_recognition.py