colossal-manchester-31772
10/15/2025, 11:34 PMtall-airline-16756
10/15/2025, 11:34 PMtall-airline-16756
10/15/2025, 11:35 PMcolossal-manchester-31772
10/15/2025, 11:36 PMtall-airline-16756
10/15/2025, 11:36 PMcolossal-manchester-31772
10/15/2025, 11:37 PMtall-airline-16756
10/15/2025, 11:38 PMSpeechHandle object to detect when interruptions happen and handle tasks smoothly. For instance, you might want to cancel any ongoing tasks or network requests during an interruption to make sure the agent transitions seamlessly to the next state.
Here’s a friendly Python example showing how to manage tasks while generating speech:
await speech_handle.wait_if_not_interrupted([*tasks])
if speech_handle.interrupted:
await utils.aio.cancel_and_wait(*tasks)
if audio_output is not None:
audio_output.clear_buffer()
await audio_output.wait_for_playout()
This way, any preemptively generated content or ongoing processing is neatly wrapped up, allowing the agent to switch back to listening mode without any hiccups or delays.
When it comes to handing off between agents, the llm.handoff() method is a great tool to transfer control smoothly. Here’s an example of how you might do a context-aware handoff:
async transferToAgent(options: { name: string; ctx: voice.RunContext<userdata> }) {
const { name, ctx } = options;
const userdata = ctx.userData;
const currentAgent = ctx.session.currentAgent;
const nextAgent = userdata.agents[name];
if (!nextAgent) {
throw new Error(`Agent ${name} not found`);
}
userdata.prevAgent = currentAgent;
return llm.handoff({
agent: nextAgent,
returns: `Transferring to ${name}`,
});
}
This method helps you cancel any ongoing speech, keep the conversation history intact, and make sure the handoff to the next agent feels natural and continuous. If you’d like, I’m here to walk you through implementing this workflow or answer any other questions you might have!
Sources: Turn detection and interruptions | LiveKit Docs | Agent speech and audio | LiveKit Docs | livekit-agents/livekit/agents/voice/run_result.py | Bringing AI avatars to voice agents | examples/src/restaurant_agent.tscolossal-manchester-31772
10/15/2025, 11:51 PMtall-airline-16756
10/15/2025, 11:52 PMcolossal-manchester-31772
10/16/2025, 12:02 AMcolossal-manchester-31772
10/16/2025, 12:02 AM