i want to implement a tool call "end_call" here is...
# ask-ai
b
i want to implement a tool call "end_call" here is the psuedocode 1. generate_reply() 2. sleep(5 sec) 3. close_soon() however, i want the sleep step be interruptable, is it possible?
t
Thanks for reaching out, Jura. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You're asking if the 5‑second sleep in your end_call tool can be interrupted — yes, it can. Use an interruptible wait (instead of raw sleep) and check the speech handle's interruption flag. Two simple patterns: 1) Use the reply's playout wait with timeout
Copy code
handle = ctx.session.generate_reply(instructions="Goodbye. Hanging up in 5s...")

try:
    await asyncio.wait_for(handle.wait_for_playout(), timeout=5.0)
except asyncio.TimeoutError:
    pass
else:
    if handle.interrupted:
        await ctx.session.generate_reply(instructions="Okay, I'll stay on the line.")
        return "Interrupted"
ctx.session.close_soon()
return "Call ended"
2) Poll every second and check
handle.interrupted
Copy code
handle = ctx.session.generate_reply(instructions="Goodbye. Hanging up in 5s...")
for _ in range(5):
    await asyncio.sleep(1)
    if handle.interrupted:
        await ctx.session.generate_reply(instructions="I'll stay on the line.")
        return "Interrupted"
ctx.session.close_soon()
return "Call ended"
Which pattern fits your setup better — the single timed wait or the per-second poll? I can adapt the example to your existing code style. Sources: Agent speech and audio | LiveKit docs | Turn detection and interruptions | LiveKit Docs | Tool definition and use | LiveKit docs