What is the order in which things get created duri...
# ask-ai
r
What is the order in which things get created during a calls lifecycle? I have the idea that first the room is created, then a participant joins the room. In what time or moment the agent is created before it joins the room? Tell me in as much detail as possible all the stages I have one more question. I need to provide the Agent that will be created some context that is variable and is based on the participant that is joining the call. Is it possible to first detect the participant that is joining the call and then we pass that information of the participant as context in the system prompt right before creating it?
t
Thanks for reaching out, Santiago. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Let me walk you through the detailed order of events in a call lifecycle in LiveKit: 1. Room Creation: The room gets created as soon as the first participant joins. If it’s not already there, it’ll be set up automatically right then. 2. Participant Joining: Once the room is ready, participants start joining. They move through stages like
JOINING
,
JOINED
, and
ACTIVE
. 3. Agent Creation and Joining: Agents can be sent into the room either automatically or on purpose. When they arrive, they join the room and start interacting with the participants. About your second question—it’s definitely possible to spot when someone joins a call and use their info to give context to an agent’s system prompt before the agent is created. Here’s how you can do it: 1. Use
ctx.wait_for_participant()
to catch when a participant joins the room. 2. Grab the participant’s details, like their identity or any custom attributes they have. 3. Feed that info into the agent’s system prompt to set up the agent with all the right context. This way, your agent starts off with exactly the info it needs, based on who’s in the room. Pretty handy, right? Sources: Room & Participant Management | livekit/livekit_composite | DeepWiki | Anatomy of an Agent | LiveKit Docs
r
Great! Use that function in an example
t
Here’s a friendly example of how you can use
ctx.wait_for_participant()
to notice when someone joins and then pass their details to an agent’s system prompt:
Copy code
async def entrypoint(ctx: JobContext):
    # Connect to the room
    await ctx.connect(auto_subscribe=AutoSubscribe.AUDIO_ONLY)

    # Wait for the first participant to join
    participant = await ctx.wait_for_participant()
    <http://logger.info|logger.info>(f"Participant joined: {participant.identity}")

    # Create a system prompt with participant information
    system_prompt = f"""
    You are an assistant for {participant.identity}. 
    Your purpose is to provide personalized support based on their attributes: {participant.attributes}.
    """

    # Initialize agent with the customized system prompt
    agent = MyAgent(
        instructions=system_prompt,
        tools=[...],  # Your tools here
    )

    # Start the agent session
    session = AgentSession(
        llm=...,  # Your LLM configuration
        agent=agent,
    )
    await session.start(
        room=ctx.room,
        room_input_options=RoomInputOptions(...),
    )
Here’s what’s happening step-by-step: 1. Wait for Participant: The function patiently waits for someone to join using
ctx.wait_for_participant()
. 2. Access Participant Information: Once they join, it grabs their identity and any attributes they have. 3. Customize System Prompt: It then uses that info to craft a personalized system prompt just for them. 4. Initialize and Start Agent: Finally, the agent is set up with that custom prompt and kicked off in the room. This way, the agent tailors its responses based on who’s there, making the experience feel just right for each participant.