what does ctx.wait_for_participant() do ?
# ask-ai
r
what does ctx.wait_for_participant() do ?
t
Thanks for reaching out, Saurabh. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — good question. You're asking what
ctx.wait_for_participant()
does; here's a concise explanation and a short example. What it does •
ctx.wait_for_participant()
pauses the agent’s execution until at least one participant joins the LiveKit room. • When a participant joins, the method returns that first participant object so the agent can inspect properties (for example
identity
) or begin interacting with them. Why you’d use it • Use this when your workflow needs a real user or participant present before continuing (e.g., to send a greeting, start media operations, or collect participant-specific info). Simple example
Copy code
participant = await ctx.wait_for_participant()
print(f"Participant {participant.identity} has joined the room")
# Now you can access participant attributes or start interaction
If you want, tell me whether you need: • Behavior when multiple participants join (how to track subsequent participants), or • How to time out or cancel waiting if no one joins within a certain period. Sources: Job lifecycle | LiveKit Documentation
r
I want to wait until SIP participant joins
t
Thanks — you want to wait until a SIP participant joins. You can do that by using
ctx.wait_for_participant()
and then checking the participant’s
kind
, looping until you get a SIP participant. Below are concise examples and a couple of options (looping vs. event-based) so you can pick what fits your workflow. Python (simple loop)
Copy code
from livekit import rtc
from livekit.agents import JobContext

async def entrypoint(ctx: JobContext):
    while True:
        participant = await ctx.wait_for_participant()
        if participant.kind == rtc.ParticipantKind.PARTICIPANT_KIND_SIP:
            print(f"SIP participant joined: {participant.identity}")
            break
        # otherwise continue waiting for the next participant
Node.js / TypeScript (simple loop)
Copy code
import { ParticipantKind } from 'livekit-server-sdk';

export default defineAgent({
  entry: async (ctx: JobContext) => {
    let participant = await ctx.waitForParticipant();
    while (participant.kind !== ParticipantKind.SIP) {
      participant = await ctx.waitForParticipant();
    }
    console.log(`SIP participant joined: ${participant.identity}`);
  },
});
Event-based alternative • Listen for the
participant_connected
event and inspect
participant.kind
. This avoids awaiting repeatedly and may be cleaner if you already use room events. Timeout / cancellation considerations • If you need to stop waiting after a certain time, add a timeout or cancellation check around the loop so your job doesn’t block indefinitely. If you’d like, tell me which of these you prefer and: • Whether you need a timeout example, or • If you want to filter further using participant metadata or identity (I can show that). Sources: Job lifecycle | LiveKit Documentation | SIP Lifecycle Management Agent | LiveKit Documentation