how to best defined event hooks like on close for ...
# ask-ai
b
how to best defined event hooks like on close for reusability (I want to use the same hook in two different entrypoint functions)
t
Thanks for reaching out, mateuszkasprowicz. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'd love to help you define reusable event hooks like
onClose
that you can use across multiple entrypoint functions. Here are some friendly approaches you might find useful: • Server-Side (Go): You can use a key-based system to manage multiple callbacks. For instance, the
AddOnClose
method lets you register, remove, and safely execute callbacks, which keeps things organized and thread-safe. • React/TypeScript: Creating a custom hook like
useEvents
can be a great way to handle event listener registration and cleanup automatically—perfect for React components! • Python Agents: The
EventEmitter
pattern is your friend here. It helps you register reusable event handlers that you can attach to multiple entrypoints, managing events like participant joins or exits smoothly. • Pipeline Node Lifecycle Hooks: For agent pipelines, built-in hooks such as
on_enter
,
on_exit
, and
on_user_turn_completed
offer reusable lifecycle management that’s both neat and effective. Each of these approaches is designed to keep your code modular, ensure proper cleanup, and make your event hooks reusable across different contexts. If you'd like, I’m more than happy to walk you through detailed examples for any specific implementation—just let me know! Sources: pkg/rtc/participant.go | packages/react/src/hooks/internal/index.ts | events/event_emitters.py | Pipeline nodes and hooks | LiveKit docs
b
I'm thinking Python and this type of events LiveKit docs › Building voice agents › Events & error handling --- # Events and error handling
Guides and reference for events and error handling in LiveKit Agents.
## Events
AgentSession
emits events to notify you of state changes. Each event is emitted with an event object as its sole argument. ### user_input_transcribed A
UserInputTranscribedEvent
is emitted when user transcription is available. #### Properties - `language`: str - `transcript`: str - `is_final`: bool - `speaker_id`: str | None - Only available if speaker diarization is supported in your STT plugin. #### Example Python:
Copy code
python
from livekit.agents import UserInputTranscribedEvent

@session.on("user_input_transcribed")
def on_user_input_transcribed(event: UserInputTranscribedEvent):
    print(f"User input transcribed: {event.transcript}, "
          f"language: {event.language}, "
          f"final: {event.is_final}, "
          f"speaker id: {event.speaker_id}")
--- Node.js:
Copy code
ts
import { voice } from '<@U07FRRZH2GN>/agents';

session.on(voice.AgentSessionEventTypes.UserInputTranscribed, (event) => {
  console.log(`User input transcribed: ${event.transcript}, language: ${event.language}, final: ${event.isFinal}, speaker id: ${event.speakerId}`);
});
### conversation_item_added A
ConversationItemAddedEvent
is emitted when a item is committed to the chat history. This event is emitted for both user and agent items. #### Properties - `item`: [ChatMessage](https://github.com/livekit/agents/blob/3ee369e7783a2588cffecc0725e582cac10efa39/livekit-agents/livekit/agents/llm/chat_context.py#L105) #### Example Python:
Copy code
python
from livekit.agents import ConversationItemAddedEvent
from livekit.agents.llm import ImageContent, AudioContent

...

@session.on("conversation_item_added")
def on_conversation_item_added(event: ConversationItemAddedEvent):
    print(f"Conversation item added from {event.item.role}: {event.item.text_content}. interrupted: {event.item.interrupted}")
    # to iterate over all types of content:
    for content in event.item.content:
        if isinstance(content, str):
            print(f" - text: {content}")
        elif isinstance(content, ImageContent):
            # image is either a rtc.VideoFrame or URL to the image
            print(f" - image: {content.image}")
        elif isinstance(content, AudioContent):
            # frame is a list[rtc.AudioFrame]
            print(f" - audio: {content.frame}, transcript: {content.transcript}")
--- Node.js:
Copy code
ts
import { voice } from '<@U07FRRZH2GN>/agents';

// ...

session.on(voice.AgentSessionEventTypes.ConversationItemAdded, (event) => {
  console.log(`Conversation item added from ${event.item.role}: ${event.item.textContent}. interrupted: ${event.item.interrupted}`);

  // to iterate over all types of content:
  for (const content of event.item.content) {
    switch (typeof content === 'string' ? 'string' : content.type) {
      case 'string':
        console.log(` - text: ${content}`);
        break;
      case 'image_content':
        // image is either a VideoFrame or URL to the image
        console.log(` - image: ${content.image}`);
        break;
      case 'audio_content':
        // frame is an array of AudioFrame
        console.log(` - audio: ${content.frame}, transcript: ${content.transcript}`);
        break;
    }
  }
});
### function_tools_executed
FunctionToolsExecutedEvent
is emitted after all function tools have been executed for a given user input. #### Methods -
zipped()
returns a list of tuples of function calls and their outputs. #### Properties - `function_calls`: list[[FunctionCall](https://github.com/livekit/agents/blob/3ee369e7783a2588cffecc0725e582cac10efa39/livekit-agents/livekit/agents/llm/chat_context.py#L129)] - `function_call_outputs`: list[[FunctionCallOutput](https://github.com/livekit/agents/blob/3ee369e7783a2588cffecc0725e582cac10efa39/livekit-agents/livekit/agents/llm/chat_context.py#L137)] ### metrics_collected
MetricsCollectedEvent
is emitted when new metrics are available to be reported. For more information on metrics, see [Capturing metrics](https://docs.livekit.io/agents/build/metrics.md). #### Properties - `metrics`: Union[STTMetrics, LLMMetrics, TTSMetrics, VADMetrics, EOUMetrics] ### speech_created
SpeechCreatedEvent
is emitted when new agent speech is created. Speech could be created for any of the following reasons: - the user has provided input -
session.say
is used to create agent speech -
session.generate_reply
is called to create a reply #### Properties - `user_initiated`: str - True if speech was created using public methods like
say
or
generate_reply
- `source`: str - "say", "generate_reply", or "tool_response" - `speech_handle`: [SpeechHandle](https://docs.livekit.io/agents/build/audio.md#speechhandle) - handle to track speech playout. ### agent_state_changed
AgentStateChangedEvent
is emitted when the agent's state changes. The
lk.agent.state
attribute on the agent participant is updated to reflect the new state, allowing frontend code to easily respond to changes. #### Properties - `old_state`: AgentState - `new_state`: AgentState #### AgentState The agent could be in one of the following states: -
initializing
- agent is starting up. this should be brief. -
listening
- agent is waiting for user input -
thinking
- agent is processing user input -
speaking
- agent is speaking ### user_state_changed
UserStateChangedEvent
is emitted when the user's state changes. This change is driven by the VAD module running on the user's audio input. #### Properties - `old_state`: UserState - `new_state`: UserState #### UserState The user's state can be one of the following: -
speaking
- VAD detected user has started speaking -
listening
- VAD detected the user has stopped speaking -
away
- The user hasn't responded for a while (default: 15s). Specify a custom timeout with
AgentSession(user_away_timeout=...)
. #### Example - **[Handling idle user](https://github.com/livekit/agents/blob/main/examples/voice_agents/inactive_user.py)**: Check in with the user after they go idle. ### close The
CloseEvent
is emitted when the AgentSession has closed and the agent is no longer running. This can occur for several reasons: - The user ended the conversation -
session.aclose()
was called - The room was deleted, disconnecting the agent - An unrecoverable error occurred during the session #### Properties - `error`: LLMError | STTError | TTSError | RealtimeModelError | None - The error that caused the session to close, if applicable ## Handling errors In addition to state changes, it's important to handle errors that may occur during a session. In real-time conversations, inference API failures can disrupt the flow, potentially leaving the agent unable to continue. ### FallbackAdapter Available in: - [ ] Node.js - [x] Python For STT, LLM, and TTS, the Agents framework includes a
FallbackAdapter
that can fall back to secondary providers if the primary one fails. When in use,
FallbackAdapter
would: - automatically resubmit the failed request to backup providers when the primary provider fails - mark the failed provider as unhealthy and stop sending requests to it - continue to use the backup providers until the primary provider recovers - periodically checks the primary provider’s status in the background
Copy code
python
from livekit.agents import llm, stt, tts
from livekit.plugins import assemblyai, deepgram, elevenlabs, openai, groq

session = AgentSession(
    stt=stt.FallbackAdapter(
        [
            assemblyai.STT(),
            deepgram.STT(),
        ]
    ),
    llm=llm.FallbackAdapter(
        [
            openai.LLM(model="gpt-4o"),
            openai.LLM.with_azure(model="gpt-4o", ...),
        ]
    ),
    tts=tts.FallbackAdapter(
        [
            elevenlabs.TTS(...),
            groq.TTS(...),
        ]
    ),
)
### Error event
AgentSession
emits
ErrorEvent
when errors occur during the session. It includes an
error
object with a
recoverable
field indicating whether the session will retry the failed operation. - If
recoverable
is
True
, the event is informational, and the session will continue as expected. - If
recoverable
is
False
(e.g., after exhausting retries), the session requires intervention. You can handle the error—for instance, by using
.say()
to inform the user of an issue. #### Properties - `model_config`: dict - a dictionary representing the current model's configuration - `error`: [LLMError | STTError | TTSError | RealtimeModelError](https://github.com/livekit/agents/blob/db551d2/livekit-agents/livekit/agents/voice/events.py#L138) - the error that occurred.
recoverable
is a field within
error
. - `source`: LLM | STT | TTS | RealtimeModel - the source object responsible for the error ### Example - **[Error handling](https://github.com/livekit/agents/blob/main/examples/voice_agents/error_callback.py)**: Handling unrecoverable errors with a presynthesized message. --- This document was rendered at 2025-11-16T151333.855Z. For the latest version of this document, see [https://docs.livekit.io/agents/build/events.md](https://docs.livekit.io/agents/build/events.md). To explore all LiveKit documentation, see [llms.txt](https://docs.livekit.io/llms.txt).
t
I'm happy to help you get comfortable with handling reusable event hooks for
AgentSession
events in Python! You can use the
on
,
once
, and
off
methods provided by the
AgentSession
object. These are great for registering, triggering once, and unregistering event listeners, making your code more organized and efficient. Here are some friendly examples to guide you: • Listening for Transcribed User Input:
Copy code
from livekit.agents import UserInputTranscribedEvent

@session.on("user_input_transcribed")
def on_user_input_transcribed(event: UserInputTranscribedEvent):
    print(f"User said: {event.transcript}, Final: {event.is_final}")
• Monitoring Conversation Items:
Copy code
from livekit.agents import ConversationItemAddedEvent

@session.on("conversation_item_added")
def conversation_item_added(event: ConversationItemAddedEvent):
    print(event)
• Tracking Agent State Changes:
Copy code
from livekit.agents import AgentStateChangedEvent

@session.on("agent_state_changed")
def on_agent_state_changed(event: AgentStateChangedEvent):
    if event.new_state == "speaking":
        print("Agent started speaking")
    elif event.new_state == "listening":
        print("Agent is now listening")
To make your code even more reusable, consider encapsulating these handlers in agent classes or utility modules. Also, lifecycle hooks like
on_enter
and
on_exit
are wonderful tools to manage setup and teardown logic for your sessions smoothly. If you’d like, I’m here to walk you through more examples or clarify anything further—just let me know! Sources: basics/label_messages.py | Turn detection and interruptions | LiveKit Docs | Logs, metrics, and telemetry | LiveKit Docs | Pipeline nodes and hooks | LiveKit docs | Events and error handling | LiveKit docs