i am getting the error: ```livekit.agents._excepti...
# ask-ai
b
i am getting the error:
Copy code
livekit.agents._exceptions.APIConnectionError: Failed to connect to Gemini Live
how to end call upon receiving the error
✅ 1
t
Thanks for reaching out, Utkarsh. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — I see you're getting
APIConnectionError: 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:
Copy code
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:
Copy code
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 elements
b
i am not able to connect to the agent so how can i use the tools?
t
Thanks — I understand you can't connect to the agent and so can't use tools. I can help—quick questions first so I give the right steps: 1. Are you running the agent locally (console) or trying to connect to LiveKit Cloud? 2. Do you see the error immediately on startup or only when a specific tool runs? 3. Can you confirm the env vars: LIVEKIT_URL, LIVEKIT_API_KEY, LIVEKIT_API_SECRET are set for the process? Meanwhile, two practical paths: • If you need to continue developing without a LiveKit connection: run the agent in console mode (
uv 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 elements
b
what about : delete_room_on_close?
t
You're asking about
delete_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):
Copy code
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