when shutdown request comes, I want to agent to st...
# ask-ai
a
when shutdown request comes, I want to agent to stop everything like generating response from LLM and all such activity. How can I do that?
t
Thanks for reaching out, Hiren. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm here to help you smoothly handle shutdown requests and stop all activities like LLM response generation. Here are some friendly steps you can follow: 1. Graceful Shutdown: You can use
session.shutdown()
to gently stop all tasks. By default, it lets ongoing operations finish nicely (
drain=True
), but if you need to stop immediately, you can set
drain=False
. 2. Interrupt Ongoing LLM Generation: If you want to stop real-time LLM responses right away, just call
session.interrupt()
. This sends a cancellation event to the server-side LLM to halt the process. 3. Custom Cleanup: To tidy up external resources like databases or logs, you can register shutdown callbacks with
add_shutdown_callback()
. This helps keep everything neat and organized. 4. Stop TTS Playout: To cancel any ongoing speech playback, use
session.interrupt()
, or if you prefer to wait for the speech to finish,
SpeechHandle.wait_for_playout()
is your friend. If you'd like, I’m happy to provide more details or walk you through a specific example—just let me know! Sources: livekit-agents/livekit/agents/voice/agent_activity.py | Job lifecycle | LiveKit Docs | Operational Guidance & Best Practices | livekit/livekit_composite | DeepWik... | Distributed Setup | LiveKit docs
a
even after doing ctx.shutdown(reason="close the call") , shutdown callback function is being called but again it shows "input stream attached" immediately after "input stream detached" what does that mean?? if shutdown callback ran it should stop everything and simply follow the scripts written in callback function
t
I understand that seeing the sequence where "input stream attached" appears after "input stream detached" during shutdown can be a bit confusing. This usually happens because of how events overlap and update asynchronously within LiveKit's architecture. Here’s a little more detail to help clarify: • During shutdown, the
_ParticipantInputStream
class handles lifecycle events like
track_subscribed
or
track_unpublished
. Sometimes, if a new track gets subscribed or re-published while everything is shutting down, it might briefly reattach before the final shutdown completes. • The
on_detached()
and
on_attached()
methods adjust internal flags, but occasionally, pending events can cause the state to flip back and forth, which is why you see that log sequence. • Importantly, this behavior is usually harmless and doesn’t signal any real problem since the underlying data channels are still being properly closed. To help avoid this, I recommend making sure the shutdown process happens in the right order—like removing event listeners before closing streams or canceling tasks that forward frames. If you'd like, I’m here to guide you through the implementation details or answer any other questions you might have! Sources: livekit-agents/livekit/agents/voice/transcription/synchronizer.py | agents/src/voice/room_io/room_io.ts
a
Yes, please help me stop all the activity once the shutdown requested
t
To make sure all activities stop right away when you request a shutdown, here’s a friendly step-by-step guide you can follow: 1. Call `session.shutdown(drain=False)`: This will immediately initiate the shutdown without waiting for any ongoing tasks to finish. 2. Remove Event Listeners: Before you call
shutdown
, it’s a good idea to unregister any event listeners so no further state changes happen. For example:
room.remove_event_listener("track_subscribed", handler)
3. Cancel Pending Tasks: Make sure to explicitly cancel tasks that handle forwarding frames or processing input/output streams. For instance:
await asyncio.gather(task1.cancel(), task2.cancel(), return_exceptions=True)
4. Use Shutdown Callbacks: You can register a callback to tidy up resources, ensuring it runs during shutdown:
Copy code
def on_shutdown(reason: str):
    print(f"Shutting down: {reason}")
    # Add cleanup logic here
ctx.add_shutdown_callback(on_shutdown)
5. Interrupt Ongoing LLM/TTS: If there’s any active LLM response generation or TTS playback, use
session.interrupt()
to stop it immediately. If you’d like, I’m happy to provide a full code example or explain any of these steps in more detail. Just let me know!