Does LiveKit require immediate agent/session start...
# ask-ai
j
Does LiveKit require immediate agent/session startup, or can we wait for participant connection? We need to fetch user-specific conversation history from our database before creating the Agent, which requires participant metadata (userId, threadId). We're considering this approach: async def entrypoint(ctx: JobContext): # Wait for participant to connect first participant_connected = asyncio.Event() @ctx.room.on("participant_connected") def on_participant_connected(participant): # Extract metadata participant_connected.set() # Wait up to 30 seconds for participant await asyncio.wait_for(participant_connected.wait(), timeout=30.0) # NOW fetch conversation history using participant metadata conversation_history = await fetch_from_database(...) # Create agent with history chat_ctx = ChatContext() # ... populate with history ... agent = Agent(chat_ctx=chat_ctx) # Start session session = AgentSession() await session.start(agent=agent, room=ctx.room) Questions: 1. Will delaying agent/session creation until after participant connects cause issues with LiveKit's infrastructure? 2. Are there timeout constraints we should be aware of? 3. Is there a better pattern for loading user-specific context that requires participant metadata? We want to ensure each user gets their personalized conversation history without breaking LiveKit's expected flow.
t
Thanks for reaching out, Albert. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
LiveKit supports delaying agent/session creation until after a participant connects. Here are the details: • Delaying Agent/Session Creation: You can wait for a participant to join before initializing actions. For example, use
await ctx.wait_for_participant()
to wait for a participant connection. • Timeout Constraints: The default timeout for participant connections is 30 seconds, extendable to 80 seconds for SIP participants if
WaitUntilAnswered
is enabled. Ensure your logic fits within these constraints. • Loading User-Specific Context: Use participant attributes or job metadata to pass and retrieve user-specific data. For example, store metadata in
ctx.job.metadata
and load it before starting the session. This approach ensures personalized conversation history without disrupting LiveKit's flow. Let me know if you need further clarification or examples! Sources: Handling RAG Delays in Voice Agents | LiveKit Docs | pkg/service/sip.go | Making calls using SIP | LiveKit Docs