What is the right way for the agent to end a call ...
# ask-ai
f
What is the right way for the agent to end a call completely (Kicking out the user from the room and destroying the room, not only disconnecting the agent) like this ?
Copy code
logger.info("đź’­ Generating farewell with [END_CALL] marker...")
        speech = self.session.generate_reply(
            instructions=farewell_instruction,
            allow_interruptions=False,
            user_input="No response from user",
        )

        logger.info(f"đź’¬ Farewell speech generated")

        await speech.wait_for_playout()
        await self.session.aclose()
t
Thanks for reaching out, Carlos. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
It sounds like you want the agent to end the call for everyone, not just disconnect itself. In that case, don’t just
aclose()
the session—delete the room. • To end the call for all participants: call the server’s
delete_room
API. This immediately disconnects everyone and destroys the room. Managing rooms • If you only disconnect the agent, the caller may remain in the room hearing silence. Agents telephony integration Example (Python server SDK):
Copy code
from livekit import api as lkapi
from livekit.api import DeleteRoomRequest

# after farewell playout
await speech.wait_for_playout()

# end the session for everyone
await lkapi.room.delete_room(DeleteRoomRequest(room="your-room-name"))

# then close the agent session
await self.session.aclose()
If you prefer to kick a single participant without destroying the room, use
RemoveParticipant
(note: it doesn’t invalidate their token). Managing participants Sources: Job lifecycle | LiveKit Docs | Agents telephony integration | LiveKit Docs | Managing rooms | LiveKit docs | Managing participants | LiveKit docs | Worker lifecycle | LiveKit Docs
f
Is this the right way ? Because it never gets to the "Farewell speech completed" message
Copy code
async def _start_second_idle_timer(self):
        """Start 10-second idle timer after first prompt"""
        try:
            logger.info("⏰ Starting SECOND idle timer (10 seconds)...")
            await asyncio.sleep(10)
            logger.info("⏰ Second idle timer expired - generating farewell with [END_CALL]")
            
        except asyncio.CancelledError:
            logger.info("⏰ Second idle timer cancelled (user spoke)")
            return
        
        # Timer expired naturally - generate farewell with [END_CALL] marker
        # The [END_CALL] marker will be detected in llm_node and trigger call ending
        farewell_instruction = (
            "The user hasn't responded after prompting them. "
            "Say a brief, polite goodbye based on your personality. "
            "Keep it short and friendly - just 1-2 sentences. "
            "Example: 'Fue un gusto asesorarle, que tenga un buen dĂ­a' "
            "IMPORTANT: End your response with [END_CALL] to close the session."
            "IMPORTANT: YOU MUST ALWAYS INCLUDE THE [END_CALL] MARKER IN YOUR RESPONSE"
        )
        
        logger.info("đź’­ Generating farewell with [END_CALL] marker...")
        speech = self.session.generate_reply(
            instructions=farewell_instruction,
            allow_interruptions=False,
            user_input="No response from user",
        )

        logger.info(f"đź’¬ Farewell speech generated")

        await speech.wait_for_playout()

        logger.info("🔚 Farewell speech completed - session closed")
        await self.close_call()
    

    async def close_call(self):
        """Close the call"""
        await lkapi.room.delete_room(self._room.room.room_name)
        logger.info(f"🔚 Call closed - room deleted")
        await self.session.aclose()
t
It sounds like your flow hangs before “Farewell speech completed.” Two likely culprits: not getting a true playout-complete signal, or the delete call shape. Try this minimal adjustment: • Wait for actual audio playout completion: ◦ Either await the returned speech handle, or prefer the playback-finished signal. ◦ In Python,
await speech.wait_for_playout()
is fine, but also log on playback-finished to confirm it fires. â—¦ Example:
Copy code
@self.session.output.audio.on("playback_finished")
    def _on_playback_finished(e):
        logger.info(f"🔊 playback_finished (interrupted={e.interrupted})")
Reference: Detect when an agent finished speaking • Ensure your farewell can’t be interrupted: ◦ You set
allow_interruptions=False
which is correct; keep it to avoid a stray input cancelling playout. Agent speech and audio • Call deleteRoom with the documented request object:
Copy code
from livekit import api as lkapi
  from livekit.api import DeleteRoomRequest

  await lkapi.room.delete_room(DeleteRoomRequest(room=self._room.room.room_name))
Reference: Managing rooms Putting it together:
Copy code
speech = self.session.generate_reply(
    instructions=farewell_instruction,
    allow_interruptions=False,
    user_input="No response from user",
)

await speech.wait_for_playout()
logger.info("🔚 Farewell speech completed")

await lkapi.room.delete_room(DeleteRoomRequest(room=self._room.room.room_name))
logger.info("🔚 Call closed - room deleted")

await self.session.aclose()
If “Farewell speech completed” still doesn’t log: • Add a
playback_finished
handler to see if playout actually completes. • Add session error/close handlers to catch failures during speech: Events and error handling Want me to help wire the
playback_finished
and error handlers into your snippet? Sources: Agent speech and audio | LiveKit docs | How to detect when an agent has finished speaking | Managing rooms | LiveKit docs | Events and error handling | LiveKit docs | Agent Session & Client Integration | livekit/livekit_composite | DeepWiki