ripe-arm-36009
12/10/2025, 6:02 AMtall-airline-16756
12/10/2025, 6:02 AMtall-airline-16756
12/10/2025, 6:02 AMclose event to detect failures.
Example (TypeScript):
session.on(voice.AgentSessionEventTypes.AgentStateChanged, (ev) => {
if (ev.newState === 'listening') {
// session initialized and ready to accept input
}
});
session.on(voice.AgentSessionEventTypes.Close, (ev) => {
if (ev.error) {
// initialization failed or session closed with error
}
});
Example (Python):
@session.on("agent_state_changed")
def on_agent_state_changed(ev: AgentStateChangedEvent):
if ev.new_state == "listening":
# session initialized and ready
If you want, tell me which SDK/language and whether you need client-side UI hooks or server-side monitoring and I’ll give a tailored snippet.
Sources: Turn detection and interruptions | LiveKit Docs | Agent session | LiveKit docsripe-arm-36009
12/10/2025, 6:03 AMtall-airline-16756
12/10/2025, 6:03 AMagent_state_changed handler runs whenever the agent's state changes. That means you will see the new_state == "listening" branch each time the agent transitions into the listening state — for example on initial startup, after reconnects, or any time the agent moves from another state into listening. The event also fires for other state changes, so you can monitor all transitions from the same handler.
If you want to avoid duplicate work when listening happens multiple times, tell me whether you need a one-time init action or repeated handling and I’ll suggest a simple pattern (e.g., a boolean guard or a state timestamp).ripe-arm-36009
12/10/2025, 6:05 AMtall-airline-16756
12/10/2025, 6:06 AMlistening transition (a simple boolean or timestamp). Also handle the close event to detect initialization failure. Example Python pattern:
initialized = False
@session.on("agent_state_changed")
def on_agent_state_changed(ev: AgentStateChangedEvent):
global initialized
if not initialized and ev.new_state == "listening":
initialized = True
# one-time init action: agent is successfully initialized
@session.on("close")
def on_close(ev):
global initialized
if not initialized and ev.error:
# one-time failure action: initialization failed
If you want automatic reset on a full reconnect or a time-based retry, say which behavior you prefer and I’ll provide that variant.ripe-arm-36009
12/10/2025, 6:38 AMtall-airline-16756
12/10/2025, 6:38 AMagent_state_changed events and recording occurrences. Below is a simple Python pattern that:
• Performs a one-time initialization action on the first listening.
• Detects subsequent returns (re-initializations) and counts them.
• Handles a close event to detect failures.
initialized = False
return_count = 0
@session.on("agent_state_changed")
def on_agent_state_changed(ev: AgentStateChangedEvent):
global initialized, return_count
if ev.new_state == "listening":
if not initialized:
initialized = True
# one-time init action
print("Agent initialized (first time).")
else:
return_count += 1
# agent has returned (reconnected / re-initialized)
print(f"Agent returned. return_count={return_count}")
@session.on("close")
def on_close(ev):
global initialized
if not initialized and ev.error:
# failed to initialize
print("Initialization failed:", ev.error)
else:
# agent/session closed after being initialized
print("Session closed.")
If you prefer a time-based debounce (ignore short flaps) or automatic reset of initialized on a full reconnection, tell me which behavior you want and I’ll provide that variant.ripe-arm-36009
12/10/2025, 6:39 AMtall-airline-16756
12/10/2025, 6:39 AMagent_state_changed event to detect entering/exiting the speaking state, and use playback_finished on the audio output if you need the precise end of audio playback.
Python example:
@session.on("agent_state_changed")
def on_agent_state_changed(event):
if event.new_state == "speaking":
print("Agent started speaking")
elif event.old_state == "speaking" and event.new_state != "speaking":
print("Agent stopped speaking")
# optional: detect when audio playback fully finishes
if session.output and session.output.audio:
@session.output.audio.on("playback_finished")
def on_playback_finished(ev):
print(f"Playback finished (interrupted={ev.interrupted})")
Node.js/TypeScript example:
session.on(voice.AgentSessionEventTypes.AgentStateChanged, (ev) => {
if (ev.newState === "speaking") {
console.log("Agent started speaking");
} else if (ev.oldState === "speaking" && ev.newState !== "speaking") {
console.log("Agent stopped speaking");
}
});
// optional playback finished
if (session.output?.audio) {
session.output.audio.on("playback_finished", (ev) => {
console.log("Playback finished, interrupted=", ev.interrupted);
});
}
If you want debouncing (ignore short flaps), counting returns, or a one-time speaking detection, tell me which behavior and I’ll provide a compact pattern.
Sources: How to detect when an agent has finished speaking | Turn detection and interruptions | LiveKit Docs