Our product has a problem migrating completely to LiveKit Cloud for agent deployment due to the configured “Graceful Shutdown” period of 1 hour MAX.
As described here, the old session will go through graceful shutdown where the maximum time old sessions will get to complete themselves is 1 hour max but for our product it can go up to 2 hours max atm.
Currently we have to keep old sessions active unless they are finished before destroying old deployments while completely cutting off the traffic to old deployments as soon as a new deployment becomes active and we do this through “explicit agent dispatch”.
We are planning to move to LiveKit cloud for agent deployments but this is becoming a serious blocker. Please provide a solution for this.
yeah we are having this issue, please provide a solution
Hi @Ayush_Joshi @Rishabh_Singh , this is Drain timeout and you’re right that although there is no maximum mentioned in the documentation at Server options | LiveKit Documentation , the effective maximum will be 1 hour for LiveKit Cloud deployments.
This is a hard cap currently I’m afraid, with only select exceptions being made at the Enterprise tier.
In general, we want to do everything we can to enable customers to move to LiveKit cloud for agent hosting, so I will bring this up with our team, but I wouldn’t expect any changes to be made in the short term.
No changes in the “short term”? In that case can you suggest a temporary workaround? Is it possible to detect graceful shutdown, save the state and then create a new room to continue with the previous participants smoothly?
Honestly, I couldn’t think of anything off the top of my head except to suggest to make the update during a low-traffic time of day. Claude however didn’t give up so easily, it made what I think is a good suggestion of checking for when participant_left and when it’s an agent, dispatch a new agent to that same room to continue where the previous agent left off. The job will land on an instance that is not currently draining.
I’ll copy the output verbatim because it makes sense, and the caveats are sensible. I haven’t tested this myself:
The room doesn’t belong to the agent; when the old instance is killed at the 1-hour drain limit, the agent participant leaves, but the room and the humans in it stay (including SIP callers, whose call stays up as long as the SIP participant is in the room). That makes the more practical workaround an agent handoff within the same room: a fresh agent job is dispatched into the existing room, picks up the saved state, and carries on. Because the draining instances stop accepting new jobs, an explicit dispatch with the same agent_name will land on the new deployment. They’re already using explicit dispatch, so this fits their setup.
Here’s how I’d structure it:
- Checkpoint continuously instead of detecting shutdown. Relying on catching the drain kill at the last moment is fragile, because the job may be terminated hard. It’s safer to persist state on every turn, for example on
conversation_item_added: the serialized chat context (chat_ctx.to_dict() in Python), session userdata, and any workflow or tool state. Store it in Redis or their DB keyed by room name. Small pieces could go in room metadata, but an external store is cleaner for a full transcript.
- Trigger the re-dispatch from outside the agent. Their backend listens for the
participant_left webhook. When the departing participant is the agent, the room still has human participants, and the session isn’t marked as finished, the backend calls CreateDispatch for that room. This works whether the old job exited cleanly or was killed at the drain limit. A shutdown callback in the old job that dispatches its own replacement could act as a faster path, but I wouldn’t depend on it alone.
- Rehydrate on entry. In the new job’s entrypoint, check the store for existing state for that room. If there is some, rebuild the
ChatContext and userdata and skip the normal greeting. A short line like “Sorry about that, I’m back. We were talking about X” covers the gap.
- Mark sessions done explicitly. The agent sets a “completed” flag when a conversation ends normally, so the webhook handler doesn’t revive agents in rooms that were wrapping up anyway.
Caveats to give them honestly: there will be a gap of a few seconds while the new agent is dispatched and connects, and any utterance in progress at the cut is lost. Turn-level checkpointing means at most one exchange needs repeating. Anything held only in memory, such as open external connections or partially executed tool calls, has to be designed to resume or be idempotent.