In my entrypoint function, what are important orde...
# ask-ai
w
In my entrypoint function, what are important orderings that need to happen? What needs done before running ctx.connect()? What needs done before starting the agent session? What about after those calls?
t
Thanks for reaching out, Alex. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Here are the important orderings and steps for your entrypoint function: • Before `ctx.connect()`: Perform logging and any initial setup tasks. • Before starting the agent session: ◦ Call
ctx.connect()
to establish the connection to the room. ◦ Use
ctx.wait_for_participant()
to wait for a participant to join. ◦ Initialize user-specific data (e.g.,
userdata = UserSessionData()
). ◦ Create and configure the agent session (e.g.,
session = AgentSession[UserSessionData](userdata=userdata)
). • After starting the agent session: ◦ Start the session with
session.start()
, passing the room and agent instance. ◦ Optionally, generate an initial response (e.g.,
session.generate_reply()
). ◦ Set up additional functionalities like participant handlers or metrics collection if needed. Following this sequence ensures proper setup and functionality of your agent session. Sources: examples/voice_agents/realtime_joke_teller.py
w
When do I need to register callbacks and event handlers?
t
Hey there! Just a quick tip: it’s best to register your callbacks and event handlers during the room setup phase, before you connect to the room. This way, you won’t miss any events as soon as the connection kicks in. Here’s a simple step-by-step: 1. Start by initializing the room and setting up all your callbacks and event handlers using methods like
registerTextStreamHandler
,
registerByteStreamHandler
, or
on()
. 2. Once everything’s registered, go ahead and connect to the room with
room.connect()
. 3. After connecting, your callbacks will automatically handle any incoming events. Here’s a quick example to make it clearer:
Copy code
# Register event handlers before connecting
room = Room()

# Example: Register a participant connected handler
@room.on("participant_connected")
def on_participant_connected(participant):
    print(f"Participant {participant.identity} connected.")

# Connect to the room after registering handlers
await room.connect(url="<ws://localhost:7880>", token="your_token")
If you’d like me to walk you through more examples or clarify anything, just let me know—I’m here to help! Sources: livekit-rtc/livekit/rtc/room.py | Room &amp; Participant Management | livekit/livekit_composite | DeepWiki | packages/livekit-rtc/src/room.ts | callback.go
w
Great! What about session event handlers? Like "close" and "error"?
t
Just a quick tip: it’s best to register session event handlers like
"close"
and
"error"
right when you initialize the
AgentSession
. That way, they’re all set and ready to catch events as soon as your session kicks off. Example:
Copy code
from livekit.agents import AgentSession

@session.on("close")
def on_close(event: CloseEvent):
    # Handle session closure
    pass

@session.on("error")
def on_error(event: ErrorEvent):
    # Handle errors
    pass
Details: • The
"close"
event fires when the session ends—whether that’s from a user action, an explicit closure, room deletion, or an error. • The
"error"
event gives you the scoop on any errors, including where they came from (like LLM, STT, TTS, etc.). If you want to dive deeper or have any questions, just give me a shout—I’m here to help!
w
Do i do it before or after session.start?
t
Just a quick tip: it’s best to register session event handlers like
"close"
and
"error"
before you call
session.start()
. That way, you’re all set to catch any events that might pop up while the session is getting going or running. Here’s a simple example to illustrate:
Copy code
# Register event handlers before starting the session
@session.on("close")
def on_close(ev: CloseEvent):
    print(f"Agent Session closed, reason: {ev.reason}")

@session.on("error")
def on_error(ev: ErrorEvent):
    print(f"Error occurred: {ev.message}")

# Start the session
await session.start(agent=MyAgent(), room=ctx.room)
If anything’s unclear or you want to dive deeper, just let me know—I’m here to help! Sources: livekit-agents/livekit/agents/voice/agent_activity.py | Events and error handling | LiveKit Docs