mysterious-nightfall-40832
10/17/2025, 5:10 AMtall-airline-16756
10/17/2025, 5:10 AMtall-airline-16756
10/17/2025, 5:12 AMon_track_subscribed event. The session needs to be up and running before any track subscription events are processed.
2. Add some checks in your on_track_subscribed event handler to confirm that the agent session is active. For example, check if the agent’s activity context isn’t None before moving forward with any operations.
3. Consider adding a short delay or a retry mechanism to give the agent session enough time to fully initialize before processing the event. Sometimes the session needs a moment to become active after restarting.
4. Keep an eye on the agent session state transitions so your event handlers only run when the agent is ready. Using logging can really help track the session’s lifecycle and spot any issues during initialization or cleanup.
5. Make sure to properly clean up the agent session and room connection when ending a call. This helps avoid any leftover references that might cause problems when restarting the session.
If you’d like, I’m here to help you implement these steps or provide more detailed guidance and code examples. Just let me know!
Sources: examples/multiple_connections.py | Node.js Agents Architecture | livekit/livekit_composite | DeepWiki | agents/src/job.ts | src/room/participant/LocalParticipant.ts | livekit-agents/livekit/agents/voice/agent_session.pymysterious-nightfall-40832
10/17/2025, 5:16 AMmysterious-nightfall-40832
10/17/2025, 5:17 AMtall-airline-16756
10/17/2025, 5:17 AMawait session.start(
agent=appointment_scheduling_assistant,
room=ctx.room,
room_input_options=RoomInputOptions(noise_cancellation=noise_cancellation.BVC(),),
)
@ctx.room.on("track_subscribed")
def on_track_subscribed(
track: rtc.Track,
publication: rtc.RemoteTrackPublication,
participant: rtc.RemoteParticipant,
):
# Event handler logic here
1. Add Defensive Checks: Before accessing the agent session or participant metadata, it’s helpful to check if they’re properly initialized. For example:
if not appointment_scheduling_assistant:
logger.warning("[on_track_subscribed] Appointment scheduling assistant is not initialized.")
return
if not participant.metadata:
logger.debug("[on_track_subscribed] No metadata found for participant.")
return
1. Introduce Retry Logic: Sometimes the agent session might not be ready right away, so adding a retry with a short delay can make your code more robust. Here’s how you might do it:
async def handle_track_subscription(track, participant):
for attempt in range(3):
if appointment_scheduling_assistant:
# Process the track
break
else:
logger.warning("Agent session not ready, retrying...")
await asyncio.sleep(1)
1. Monitor Agent Session State: Adding logging to observe the agent session state can be really helpful. This way, you’ll know exactly when it’s active and ready to process events:
@appointment_scheduling_assistant.on("agent_state_changed")
def on_agent_state_changed(event):
logger.info(f"Agent state changed: {event.old_state} -> {event.new_state}")
Implementing these changes should make your agent session more reliable and reduce the chance of errors when handling events. If you have any questions or need further assistance, I’m here to help!mysterious-nightfall-40832
10/17/2025, 5:23 AMtall-airline-16756
10/17/2025, 5:24 AMhandle_track_subscription function—it doesn’t get called automatically, so you’ll need to call it yourself inside the on_track_subscribed event handler whenever a track is subscribed. Here’s a simple way to set that up:
@ctx.room.on("track_subscribed")
def on_track_subscribed(
track: rtc.Track,
publication: rtc.RemoteTrackPublication,
participant: rtc.RemoteParticipant,
):
# Call the handle_track_subscription function asynchronously
asyncio.create_task(handle_track_subscription(track, participant))
By doing this, you’ll make sure the function runs every time a track is subscribed, and it will also manage retries if the agent session isn’t quite ready yet. I hope this helps clarify things!tall-airline-16756
10/17/2025, 5:34 AM