how can i have the agent end the call, but actuall...
# ask-ai
g
how can i have the agent end the call, but actually only call shutdown after the agent speaks what's pending. should i do this via tool calling? if so how to deal with tool calling first before llm/speech gen?
t
Thanks for reaching out, Emily. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — you're asking how to ensure the agent only ends the call after finishing any pending speech, and whether to use a tool call for that. Short answer and options: • You do not need tool calling just to wait for speech to finish. Capture the speech handle and await playback, then shut down. ◦ Example (Python):
Copy code
handle = session.say("Goodbye! Thank you for calling.", allow_interruptions=False)
    await handle  # waits for playout
    await session.shutdown()
• Example (Node):
Copy code
const handle = session.say('Goodbye!', { allowInterruptions: false });
    await handle.waitForPlayout();
    await ctx.shutdown();
• Use tool calling only if you want the LLM to decide when to end the call (e.g., after a final confirmation). In that case create an
end_call
tool that: a. Calls
ctx.wait_for_playout()
(or
waitForPlayout()
on the handle) to wait for current/queued speech. b. Calls session shutdown. ◦ Example tool (Python):
Copy code
@function_tool
    async def end_call(self, ctx: RunContext):
        await ctx.wait_for_playout()
        await self.session.shutdown()
Key settings: • Set
allow_interruptions=False
on the final message so it cannot be cut off. • If multiple queued speech items exist, awaiting the handle or
wait_for_playout()
ensures all queued playout completes before shutdown. If you want, tell me whether your code runs inside an LLM tool runner (so I can give the exact tool signature) or share which SDK/language you're using and I’ll provide a concrete snippet for your setup. Sources: Agent speech and audio | LiveKit docs