how to detect if the agent speech is interrupted b...
# ask-ai
n
how to detect if the agent speech is interrupted by the user speech?
t
Thanks for reaching out, Tareq. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm happy to share that LiveKit offers automatic interruption detection to help identify when a user’s speech overlaps with an agent’s. This is done using Voice Activity Detection (VAD) and, if you choose, Speech-to-Text (STT). You can easily set up and keep an eye on interruptions with these options: • Configuration: You can tailor how interruptions work by adjusting parameters like
allow_interruptions
,
min_interruption_duration
, and
min_interruption_words
. • Event Monitoring: Keep track of interruptions by listening to events such as
agent_state_changed
or
playback_finished
. • Chat Context: The
interrupted
flag in the conversation history helps you spot any responses that didn’t finish completely. • Manual Control: If you want to take charge directly, you can use
session.interrupt()
to stop agent speech whenever needed. If you’d like, I’m here to provide detailed examples or guide you through the implementation—just let me know! Sources: Turn detection and interruptions | LiveKit Docs | Docs::Agents | livekit-agents/livekit/agents/voice/agent_session.py | VoicePipelineAgent | LiveKit Docs | pipeline-llm/interrupt_user.py
n
Yes please help me with the event monitoring
t
To help you monitor interruptions using events, here are a couple of friendly approaches you can try: • Agent State Change Event: You can listen for the
agent_state_changed
event to notice when the agent switches from speaking to listening because of an interruption:
Copy code
@session.on("agent_state_changed")
def on_agent_state_changed(ev):
    if ev.new_state == "listening" and ev.old_state == "speaking":
        print("Agent was interrupted by user speech")
• Playback Finished Event: Another way is to use the
playback_finished
event, which helps detect if playback was interrupted. This event includes an
interrupted
flag you can check:
Copy code
@session.output.audio.on("playback_finished")
def on_playback_finished(ev):
    if ev.interrupted:
        print(f"Speech was interrupted at {ev.playback_position} seconds")
These events are great tools to help your program respond smoothly to interruptions. If anything feels unclear or if you'd like me to walk you through more examples, just let me know—I'm here to help!
n
Show me reference of the Playboy playback finish event
t
I'm happy to help you understand the
playback_finished
event in LiveKit! This event is emitted by the
AudioOutput
class to let you know when an audio playback segment has ended. Here are some important details to keep in mind: • Event Properties: • `playback_position`: This tells you the duration of the audio that has been played, measured in seconds. • `interrupted`: This indicates whether the playback was stopped before it naturally finished. • `synchronized_transcript`: An optional transcript that syncs with the playback, which can be really useful. Here's a Python example to illustrate:
Copy code
@session.output.audio.on("playback_finished")
def _on_playback_finished(ev):
    print(f"Playback finished: interrupted={ev.interrupted}, position={ev.playback_position}")
And here's how you might use it in JavaScript:
Copy code
session.output.audio.on('playbackFinished', (ev) => {
  console.log(`Playback finished at ${ev.playbackPosition}s, interrupted: ${ev.interrupted}`);
});
If you have any questions or would like me to walk you through more examples, please don’t hesitate to ask—I’m here to help! Sources: livekit-agents/livekit/agents/voice/transcription/synchronizer.py | How to detect when an agent has finished speaking | agents/src/voice/io.ts