suppose that gemini realtime made a tool call in t...
# ask-ai
r
suppose that gemini realtime made a tool call in the middle of the voice conversation. then I have to close the connection if a certain parameter is returned as true in the tool response. how would i do that?
t
Thanks for reaching out, Gaurav. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — I’m responding to your question about closing a LiveKit voice connection when a Gemini Realtime tool call returns a parameter set to true. Short answer: have the tool return a structured result (e.g.,
{ should_end_call: true }
), check that result in your agent/handler immediately after the tool call, then shut down the agent session and (optionally) delete the LiveKit room to end the call for all participants. Example patterns you can follow: 1) Agent/tool returns the flag • Tool returns JSON like
{ "should_end_call": true, "reason": "safety" }
. 2) Act on the result (Python-style pseudocode)
Copy code
result = await analyze_user_request(context, query)
if result.get("should_end_call"):
    # optional: send final reply to user
    await context.session.generate_reply(instructions="Ending the call now.")
    # end the LiveKit room for all participants
    await job_ctx.api.room.delete_room(api.DeleteRoomRequest(room=job_ctx.room.name))
    # gracefully shut down the agent session (drain finishes speech)
    await context.session.shutdown(drain=True)
3) Node.js equivalent • Use the agent context/tool result check, call
await ctx.session.generateReply(...)
if needed, call
RoomServiceClient.deleteRoom(...)
to remove the room, then
await ctx.shutdown()
to end the session. Key notes • Return a clear boolean flag from the tool so the agent can react deterministically. • Use
session.shutdown(drain=True)
(or
ctx.shutdown()
in Node) to let in-flight speech finish before closing. • Deleting the room ends the call for everyone; shutting down the session ends the agent’s connection only. If you want, tell me: • which SDK/language you’re using (Python or Node), and • whether you want to end the call for everyone or only disconnect the agent — I’ll provide a concrete code snippet for that environment. Sources: Agents telephony integration | LiveKit docs | Gemini Live API plugin | LiveKit docs