fancy-planet-81885
11/05/2025, 9:17 PMlogger.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()tall-airline-16756
11/05/2025, 9:17 PMtall-airline-16756
11/05/2025, 9:19 PMaclose() 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):
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 Docsfancy-planet-81885
11/05/2025, 9:24 PMasync 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()tall-airline-16756
11/05/2025, 9:26 PMawait speech.wait_for_playout() is fine, but also log on playback-finished to confirm it fires.
â—¦ Example:
@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:
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:
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