mammoth-tiger-73810
07/31/2025, 1:37 PMtall-airline-16756
07/31/2025, 1:37 PMtall-airline-16756
07/31/2025, 1:38 PMimport asyncio
import time
SILENCE_THRESHOLD = 5 # seconds
async def monitor_interaction():
while True:
if time.time() - user_last_spoke_time > SILENCE_THRESHOLD:
<http://logger.info|logger.info>("silent for too long! disconnecting")
try:
await ctx.room.disconnect()
except Exception as e:
logger.exception("Error while ending call")
else:
logger.trace("silence is not enough to disconnect")
await asyncio.sleep(1)
For a more comprehensive solution, you can track when the user starts speaking and reset the silence timer accordingly:
async def entrypoint(ctx: JobContext):
user_last_spoke_time = time.time()
monitor_task = None
async def monitor_interaction():
while True:
if time.time() - user_last_spoke_time > SILENCE_THRESHOLD:
<http://logger.info|logger.info>("silent for too long! disconnecting")
try:
await ctx.room.disconnect()
except Exception as e:
logger.exception("Error while ending call")
else:
logger.trace("silence is not enough to disconnect")
await asyncio.sleep(1)
@agent.on("user_started_speaking")
def on_user_started_speaking(_msg: llm.ChatMessage):
user_last_spoke_time = time.time()
monitor_task = asyncio.create_task(monitor_interaction())
agent.start(ctx.room, participant)
async def on_shutdown():
<http://logger.info|logger.info>("shutting down session")
if monitor_task:
monitor_task.cancel()
ctx.add_shutdown_callback(on_shutdown)
Key components include:
• Silence Threshold: Set a threshold (e.g., 5 seconds) to define how long silence should last before disconnection.
• User Activity Tracking: Use events like user_started_speaking to reset the silence timer.
• Monitoring Loop: Continuously check if the silence threshold is exceeded.
• Call Disconnection: Use ctx.room.disconnect() to terminate the call when necessary.
This approach ensures that calls are automatically disconnected after a period of inactivity, improving efficiency and user experience.
Sources: SIP Integration | livekit/livekit_composite | DeepWiki | How to detect and handle silence during calls | Speech & Audio Processing | livekit/livekit_composite | DeepWiki | agent_extensions/agent_extensions/handlers/inactivity_handler.py