is there anyway to gracefully shut down the agent ...
# ask-ai
m
is there anyway to gracefully shut down the agent without deleting the room?
t
Thanks for reaching out, Alfred. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Could you please help me understand what you mean by "agent"? Are you talking about a specific part, like a participant, a bot, or maybe a server process in LiveKit? Knowing this will really help me guide you in the best way possible.
m
i mean agent session
t
I'm glad you're looking to gracefully shut down an agent session in LiveKit without deleting the room! You can simply use the
session.shutdown()
or
session.aclose()
method on your
AgentSession
instance. This way, the agent will disconnect smoothly, and the other participants will remain unaffected. Here’s a friendly example in Python to guide you:
Copy code
await self.session.generate_reply(instructions="say goodbye to the user")
self.session.shutdown()
Also, to make sure the room stays intact after the agent session ends, just set
delete_room_on_close=False
in your
RoomInputOptions
configuration like this:
Copy code
session = AgentSession(
    stt=deepgram.STT(),
    llm=openai.LLM(),
    tts=cartesia.TTS(),
    vad=silero.VAD.load(),
    input_options=RoomInputOptions(delete_room_on_close=False)
)
And if you'd like, you can set up an
on_close
event listener to take care of any cleanup tasks after the session ends:
Copy code
@session.on("close")
def on_close(ev: CloseEvent):
    print(f"Agent Session closed, reason: {ev.reason}")
    # Perform any necessary cleanup here
This way, the agent disconnects gracefully, any cleanup you need happens smoothly, and the room stays ready for everyone else. If you have any questions or need further help, I’m here for you! Sources: Job lifecycle | LiveKit Docs | Agent Session & Activity Management | livekit/livekit_composite | DeepWiki | livekit-agents/livekit/agents/voice/room_io/room_io.py | Events and error handling | LiveKit Docs