Handoff waits for the previous realtime session to close (up to 5 s with GPT-Live) before the next agent starts
What happens
On a handoff where the realtime session can't be reused, AgentSession._update_activity awaits activity.drain() and activity.aclose() of the old activity before it starts the new one (voice/agent_session.py). For a model without mutable_instructions, such as GPT-Live, any handoff to an agent with different instructions takes this path, so the old session's close sits on the caller's path.
With GPT-Live, aclose() sends session.close and waits for session.closed, which carries the session's final usage, for up to _SESSION_CLOSE_TIMEOUT = 5.0 s (llm/_realtime/gpt_live.py). Meanwhile the caller hears nothing: the new agent hasn't started yet.
How often, in production
We run GPT-Live on Azure, with two or three agents per call (gates, then the main agent). Handoffs timed from update_agent() to the end of the activity update task:
- median 0.39 s, p90 1.67 s, max 6.13 s;
- a few percent hit the 5 s timeout.
So it's usually short, but it's dead air that the new agent doesn't need: the old session has nothing more to say to the caller once session.close is sent.
There's also a cost side. When the timeout fires, the final session.closed is dropped and the voice seconds since the last session.usage.updated are never reported. Raising the timeout fixes the usage but makes the handoff silence longer, so today you have to trade one for the other.
Proposal
Let the old realtime session finish closing in the background on a handoff:
- after
session.closeis sent,AgentActivityhands the session's remaining close to a background task and the new activity starts right away; - the old session's
metrics_collectedkeeps being forwarded untilsession.closed(or a timeout), so its final usage still reaches the session's metrics; AgentSession.aclose()awaits the pending background closes, so the usage is complete before the session ends.
For a few seconds two realtime sessions are open at once. That counts against provider concurrency limits, so it may need to be opt-in.
Would you accept a PR along these lines, or is there a reason the close has to finish before the next activity starts?
Checked this against main (d5e9e5c), and the path is as you describe. A couple of details that matter for the fix:
Why the wait is there today. It's on purpose, for the usage. AgentActivity._close_session awaits rt_session.aclose() and only then removes the metrics_collected listener, with a comment saying so ("after aclose, so a model that reports its final usage while closing is still counted"):
https://github.com/livekit/agents/blob/d5e9e5cb19faba9bafa05717bfc5b89395656b47/livekit-agents/livekit/agents/voice/agent_activity.py#L1672-L1675
And _update_activity awaits drain() then aclose() inside _activity_lock, before self._activity is swapped and the new one started:
https://github.com/livekit/agents/blob/d5e9e5cb19faba9bafa05717bfc5b89395656b47/livekit-agents/livekit/agents/voice/agent_session.py#L1972-L1974
So moving the close to the background has to keep that listener attached until the background close finishes. Otherwise it fixes the dead air and silently loses the usage, which is the same trade-off you have now.
Only the realtime session needs to go to the background. The rest of _close_session (STT/TTS/VAD listeners, audio recognition) is quick. The slow part is the asyncio.wait_for(session_closed_fut, _SESSION_CLOSE_TIMEOUT) in GPT-Live's _close_ws. So the smallest change is in _close_session: when the activity is closing for a handoff, hand rt_session.aclose() to a tracked task and remove the metrics listener in that task, after the close.
Three things I'd watch in that change:
- Who owns the task. It has to outlive the old activity, so it belongs on
AgentSession, in a set thatAgentSession.aclose()awaits (with a bound). If it's left toJobContext's pending tasks instead, it's cancelled during_cleanup_job, after the session has already closed, which is the timing discussed in #8366, and the final usage would be lost again. - Spans.
_close_sessionclears_realtime_spansright after the close. If the finalsession.usage.updatedarrives after that,_on_metrics_collectedstill collects the usage, but there's no span to attach it to. Probably fine, worth a line in the PR. - Session end. If the whole
AgentSessioncloses during a handoff, the background close and the new activity's close run together. Awaiting the set inaclose()covers it, but a test for "handoff, then immediate session close" would catch a regression.
On opt-in: agreed, because of provider concurrency limits. Though the overlap is bounded by the same 5 s timeout that's in the caller's path today, so it trades a few seconds of dead air for a few seconds of an extra open session.
Happy to review or test the PR when it's up.
Thanks, this matches what we found, and the listener/ownership points are exactly the parts that would make or break it. We'll wait for a maintainer's view on whether this belongs in core (and as opt-in) before writing the PR.