calm-eye-57311
10/01/2025, 1:12 PMtall-airline-16756
10/01/2025, 1:12 PMtall-airline-16756
10/01/2025, 1:13 PMctx.connect() method is what helps you connect the agent to the specified room, making the agent an active participant there. This connection lets you access important properties like ctx.room.local_participant, which represents the agent within the room.
It's a good idea to defer calling ctx.connect() until the end of entrypoint(). This way, you make sure all the essential setup steps are completed before the agent joins. These steps include:
• Setting up external services like Egress for recording
• Registering event listeners, such as track_subscribed
• Getting session management structures or background tasks ready
• Starting AgentSession instances that handle the connection behind the scenes
By waiting to connect, you help prevent issues like missing important events (for example, tracks becoming available before listeners are set up) and ensure everything is in place when the agent becomes active in the room. If you have any questions about this process, feel free to ask—I’m here to help!
Sources: livekit-agents/livekit/agents/ipc/job_proc_lazy_main.py | agents/src/job.ts | Docs::Agents | Inside a session | LiveKit Docs | complex-agents/drive-thru/drivethru_agent.pycalm-eye-57311
10/01/2025, 1:16 PMtall-airline-16756
10/01/2025, 1:16 PMctx.add_shutdown_callback() doesn’t actually need to be called before ctx.connect(). You can safely call it either before or after. Its main role is to register a callback that runs during shutdown, and this registration works independently from the connection process. I hope this helps clarify things for you!
Sources: tests/test_ipc.py | Session recording and transcripts | LiveKit Docs | Inside a session | LiveKit Docs