wooden-beard-26644
07/31/2025, 6:32 PMtall-airline-16756
07/31/2025, 6:32 PMtall-airline-16756
07/31/2025, 6:33 PMctx.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.pywooden-beard-26644
07/31/2025, 6:34 PMtall-airline-16756
07/31/2025, 6:34 PMregisterTextStreamHandler, 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:
# 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 & Participant Management | livekit/livekit_composite | DeepWiki | packages/livekit-rtc/src/room.ts | callback.gowooden-beard-26644
07/31/2025, 6:36 PMtall-airline-16756
07/31/2025, 6:36 PM"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:
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!wooden-beard-26644
07/31/2025, 6:37 PMtall-airline-16756
07/31/2025, 6:37 PM"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:
# 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