after ctx.shutdown() the complete call is getting ...
# ask-ai
h
after ctx.shutdown() the complete call is getting disconnected. including the other 2 sip participants. I want only AI to leave the call
t
Thanks for reaching out, aditi. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To make sure that only the AI agent leaves the call after
ctx.shutdown()
without disconnecting other SIP participants, here’s a gentle approach you can follow: 1. Use
await session.aclose()
to gracefully close the AI agent's session. 2. Try to avoid calling
delete_room
, as this will disconnect everyone, including SIP callers. Here’s a friendly example to guide you:
Copy code
async def entrypoint(ctx: agents.JobContext):
    session: AgentSession | None = None
    try:
        await ctx.connect(auto_subscribe=AutoSubscribe.AUDIO_ONLY)

        shutdown_future = asyncio.Future()

        def _on_shutdown(reason: str):
            if not shutdown_future.done():
                shutdown_future.set_result(None)

        ctx.add_shutdown_callback(_on_shutdown)

        participant = await ctx.wait_for_participant(
            kind=rtc.ParticipantKind.PARTICIPANT_KIND_STANDARD
        )

        session = AgentSession(
            # Configure LLM, STT, TTS, etc.
        )
        await session.start(room=ctx.room, agent=MyAgent())

        done, _ = await asyncio.wait(
            [shutdown_future],
            return_when=asyncio.FIRST_COMPLETED,
        )

    finally:
        if session:
            await session.aclose()  # This disconnects only the AI agent
This way, the AI agent can leave the call smoothly while your SIP participants stay connected. If you have any questions or need a hand with anything else, I’m here to help! Sources: Agent-assisted warm transfer | LiveKit Docs | Agents telephony integration | LiveKit Docs | Job lifecycle | LiveKit Docs | SIP overview | LiveKit Docs
h
I tried await session.aclose(), this is also disconnecting everybody
t
If
await session.aclose()
is disconnecting all participants, it might be due to a configuration setting that needs a little adjustment. Let’s walk through some important points to check together: 1. Make sure
delete_room_on_close=False
: When this option is set to
True
in
RoomInputOptions
, the whole room gets deleted as soon as the agent session closes. Double-check your setup like this:
Copy code
python room_input_options=RoomInputOptions(delete_room_on_close=False)
2. Look out for custom shutdown hooks: Sometimes, if your code has event listeners (such as
participant_disconnected
) that call
ctx.delete_room()
without distinguishing between agent and non-agent participants, it might unintentionally delete the room when the agent leaves. It’s a good idea to ensure these listeners are properly filtering participants. 3. Consider setting
close_on_disconnect=False
if it fits your use case
: This helps prevent the agent session from closing automatically when the linked participant disconnects. Just keep in mind, it won’t affect manual calls to
aclose()
. Take a moment to review these settings, and if you’re still running into trouble, please don’t hesitate to reach out. I’m here to help you get this sorted! Sources: Job lifecycle | LiveKit Docs | livekit-plugins/livekit-plugins-deepgram/livekit/plugins/deepgram/tts.py | Agents telephony integration | LiveKit Docs
h
how to write code for 'RoomInputOptions.close_on_disconnect=False' in python ?
t
To set
RoomInputOptions.close_on_disconnect=False
in Python, you can easily configure it when starting an
AgentSession
. Here’s a simple example to guide you:
Copy code
from livekit.agents import AgentSession, RoomInputOptions

# Start the agent session with close_on_disconnect disabled
session = AgentSession()
await session.start(
    room=ctx.room,
    agent=MyAgent(),
    room_input_options=RoomInputOptions(close_on_disconnect=False),
)
This way, your agent session will stay active even if the linked participant disconnects. If you have any questions or need a hand with anything else, just let me know—I’m here to help! Sources: Development Tools | livekit/livekit_composite | DeepWiki | examples/voice_agents/session_close_callback.py
h
immediately after ai session close the room is disconnecting "{"message": "AI session closed", "level": "INFO", "name": "root", "pid": 15, "job_id": "AJ_4soPxBnXujvd", "timestamp": "2025-10-13T160258.892424+00:00"} {"message": "process exiting", "level": "INFO", "name": "livekit.agents", "reason": "", "pid": 15, "job_id": "AJ_4soPxBnXujvd", "timestamp": "2025-10-13T160258.932839+00:00"} {"message": "livekit:rtc engine453livekit:rtc_engine - received session close: \"signal client closed: \\\"stream closed\\\"\" UnknownReason Resume", "level": "WARNING", "name": "livekit", "pid": 15, "job_id": "AJ_4soPxBnXujvd", "timestamp": "2025-10-13T160258.936866+00:00"}"
t
I understand how frustrating it can be when the room disconnects right after the AI session closes. This usually happens because the
delete_room_on_close
option is turned on. To keep the room open, you can set up your
RoomInputOptions
like this:
Copy code
await session.start(
    agent=MyAgent(),
    room=ctx.room,
    room_input_options=RoomInputOptions(
        delete_room_on_close=False  # Prevents automatic room deletion
    ),
)
By doing this, your room will stay active even after the AI session ends. If you have any questions or need more help, please don’t hesitate to ask—I’m here for you! Sources: Connecting to LiveKit | LiveKit Docs | Development Tools | livekit/livekit_composite | DeepWiki | Job lifecycle | LiveKit Docs | livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/stt.py