boundless-author-7811
05/04/2026, 10:18 AMlivekit.agents._exceptions.APIConnectionError: Failed to connect to Gemini Live
how to end call upon receiving the errortall-airline-16756
05/04/2026, 10:18 AMtall-airline-16756
05/04/2026, 10:18 AMAPIConnectionError: Failed to connect to Gemini Live and you want to end the call when that happens.
You can catch that exception and trigger a graceful shutdown. Two common options:
• Use the EndCallTool (recommended if your agent already exposes tools):
a. Add EndCallTool to the agent tools.
b. In the exception handler, call the end-call tool to run the shutdown/cleanup sequence.
• Manually shut down the session:
◦ Wrap the failing call in try/except and call session.shutdown() (or ctx.shutdown() / session.shutdown(drain=True) to finish in-flight speech) inside the except block.
Example patterns:
from livekit.agents._exceptions import APIConnectionError
try:
# code that connects to Gemini Live
except APIConnectionError:
# Option A: call EndCallTool (if available)
end_call_tool.run() # or however your tool is invoked
# Option B: manual shutdown
session.shutdown(drain=True)
If the error occurs inside a tool, raise a ToolError so the agent can produce a farewell before shutdown:
from livekit.agents.llm import ToolError
try:
# tool logic
except APIConnectionError:
raise ToolError("Could not connect to Gemini. Ending the call.")
If you want, share the snippet where the error is thrown (which function / tool, and whether you use EndCallTool), and I’ll suggest the exact placement for the try/except and shutdown call.
Sources: EndCallTool | LiveKit Documentation | Job lifecycle | LiveKit Documentation | The ReAct Pattern for Voice Agents and How AI Agents Think, Act, and Respon..., with interactive elementsboundless-author-7811
05/04/2026, 10:20 AMtall-airline-16756
05/04/2026, 10:20 AMuv run agent.py console) to test tools and logic locally without connecting to the cloud.
• If you want the call to end when connection fails (once connected): catch APIConnectionError and either invoke your EndCallTool or call session.shutdown(drain=True) / ctx.shutdown() in the except block. If the agent never connects, add that try/except around the connect call so you can still trigger shutdown behavior or log and exit cleanly.
Share which environment you’re using and the code snippet where the error is raised and I’ll give exact changes to catch the error and end the call.
Sources: The ReAct Pattern for Voice Agents and How AI Agents Think, Act, and Respon... | Tool definition and use | LiveKit Documentation | Google AI and LiveKit | LiveKit Documentation | Server startup modes | LiveKit Documentation | https://docs.livekit.io/agents/start/voice-ai.md|, with interactive elementsboundless-author-7811
05/04/2026, 10:25 AMtall-airline-16756
05/04/2026, 10:25 AMdelete_room_on_close and how it relates to ending a call when you get APIConnectionError. Short answer: it controls room cleanup after the session ends, but it does not handle connection failures itself. Use it together with a shutdown or EndCallTool to ensure the room is deleted when you end the call.
What delete_room_on_close does
• Python-only RoomOptions flag. When True, LiveKit will delete the room immediately when the agent session closes.
• Default is False.
• It cleans up the room after a normal or programmatic session end; it does not retry or recover from external API connection failures.
Recommended pattern for your error case
1. Catch the connection error around your connect or tool call.
2. Trigger a session shutdown or EndCallTool to end the call.
3. If you want the room removed automatically, start the session with delete_room_on_close=True.
Example (Python):
from livekit.agents import room_io
from livekit.agents._exceptions import APIConnectionError
await session.start(agent=my_agent, room=room,
room_options=room_io.RoomOptions(delete_room_on_close=True))
try:
# code that connects to Gemini Live
except APIConnectionError:
# gracefully end the call and let delete_room_on_close remove the room
await session.shutdown(drain=True)
Notes
• If you raise a ToolError on failure, the agent can speak a farewell before shutdown.
• Node.js SDK does not have delete_room_on_close — you must delete the room manually via the server API.
If you want, paste the snippet where the error is thrown and tell me whether you use Python or Node.js; I’ll show the exact spot to add the try/except and shutdown call.
Sources: Agent session | LiveKit Documentation | Build a salary negotiation coach with a LemonSlice avatar | LiveKit | Job lifecycle | LiveKit Documentation | EndCallTool | LiveKit Documentation, with interactive elements