what's a good pattern from Client Side (using Pyth...
# ask-ai
a
what's a good pattern from Client Side (using Python SDK), to detect that the last audio chunk of the agent was already sent?
t
Thanks for reaching out, Sebastián. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! To figure out exactly when the agent’s last audio chunk has been sent using the Python SDK, you can listen for the
playback_finished
event from the
AudioOutput
in the agent session. This event fires right after the final audio frame plays, so it’s the best way to know when the agent has fully finished speaking. Here’s a quick example to get you started:
Copy code
from livekit.agents.voice.io import PlaybackFinishedEvent

@session.output.audio.on("playback_finished")
def _on_playback_finished(ev: PlaybackFinishedEvent):
    logger.info(f"Playback finished: {ev.interrupted}")
Just a heads-up:
session.output.audio
will be
None
before the session kicks off or before you manually start
RoomIO
. If you’re working with web clients, a neat trick is to set up an RPC (Remote Procedure Call) in your agent that broadcasts the
playback_finished
event to the room. That way, web clients can easily listen for when the agent wraps up speaking. One more thing — the
final
parameter in transcription events only tells you the transcription is done, not that the audio playback has finished. So for the most reliable detection, always count on the
playback_finished
event. Sources: Bringing AI avatars to voice agents | How to detect when an agent has finished speaking
a
how reliable is this compared to using the agent state changed event?
t
Hey there! Just a quick heads-up: the
playback_finished
event is actually way more reliable than the
agent_state_changed
event when it comes to knowing exactly when the last bit of audio has finished playing. That’s because
playback_finished
is made specifically to signal the exact moment audio playback wraps up. Plus, it gives you handy details like the
playback_position
(how much audio played) and whether the playback was
interrupted
. On the other hand, the
agent_state_changed
event switches the agent into the "listening" state, but this can happen before the audio is fully done playing. It basically means the agent is ready to hear new input, but it doesn’t guarantee all the audio has finished. So if you rely on
agent_state_changed
, you might end up acting too soon or having things not quite line up between the audio and the agent’s state. If you want things to sync up perfectly—like updating your UI or kicking off actions only after the audio is totally done—the
playback_finished
event is definitely your best bet. Meanwhile,
agent_state_changed
works better for keeping track of the bigger conversational picture rather than the exact timing of audio playback. Sources: How to detect when an agent has finished speaking | livekit-agents/livekit/agents/voice/background_audio.py | Bringing AI avatars to voice agents