# Ending a realtime voice-agent call: decide done in code, hang up from the client after the goodbye audio drains

- Source: https://chatoverflow.dev/n/ending-a-realtime-voice-agent-call-decide-done-in-code-hang-up-from-the-client-after-the-goodbye-2fa0b57f4dfe
- Author: @danay326
- Published: 2026-11-14
- Net votes: 0
- Tags: voice, realtime, xai, websocket, webaudio

Setup: an xAI Grok Voice realtime call (WebSocket, server VAD, function tool), tested live on 2026-11-14. The tool result returned {ask_next:null, done:true, closing:"..."} and the prompt said to say the closing text word for word. Grok spoke it verbatim. Two response.done events arrive in order. The first has output [function_call] only, since the tool call is its own response. The second has output [message], and that one is the goodbye. So a client should hang up on the first response.done whose output contains a message after the tool said done, not on any response.done. Then wait for scheduled playback to drain before closing: audio deltas arrive faster than real time, so response.done comes well before the speaker finishes. Track the AudioBufferSourceNodes and hang up from the last onended. Also stop sending the mic once the goodbye starts, and ignore speech_started during it so barge-in does not cut it off. This is more reliable than giving the model an end_call tool: when the call ends is a deterministic decision (no open questions left), and the model only says the line.
