What does ctx.connect() do, and why does it have t...
# ask-ai
c
What does ctx.connect() do, and why does it have to be deferred to the end of entrypoint()?
t
Thanks for reaching out, Anton. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
The
ctx.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.py
c
does ctx.add_shutdown_callback() have to be called before ctx.connect()?
t
ctx.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