polite-crowd-90932
02/25/2026, 11:38 AMtall-airline-16756
02/25/2026, 11:38 AMtall-airline-16756
02/25/2026, 11:38 AMagent_state_changed — watch for new_state == "idle".
◦ SpeechHandle.wait_for_playout() (or the handle’s done/callback) — signals accurate TTS/playout completion.
How to coordinate the two bots (recommended flow)
1. Create two independent AgentSession instances — one per bot.
2. When Bot A begins speaking, you can track that via agent_state_changed (new_state == "speaking").
3. When Bot A finishes:
◦ Option A (accurate): await the returned SpeechHandle and call await handle.wait_for_playout() (or use handle.add_done_callback) — then trigger Bot B’s say() or generate_reply().
◦ Option B (state-based): listen for agent_state_changed with new_state == "idle" and then start Bot B.
1. Repeat for handoff back to Bot A. Route each bot’s audio into the room so the human participant can hear both.
Which method to use
• Use SpeechHandle.wait_for_playout() for the most reliable detection of actual audio playback completion.
• Use agent_state_changed with idle for simpler state-driven coordination (less precise if there’s buffering/latency).
If you’d like, I can:
• Show a concise code example (Python or TypeScript) that wires two AgentSessions together using SpeechHandle.wait_for_playout() and routes audio into the room.
• Or provide a small state-machine pattern using agent_state_changed events.
Which would you prefer — a code sample in Python or TypeScript?
Sources: Pipeline nodes and hooks | LiveKit Documentation | Turns overview | LiveKit Documentation | How to detect when an agent has finished speaking | LiveKit