how can I get my agent to speak as soon as the use...
# ask-ai
w
how can I get my agent to speak as soon as the user has connected in an outbound call?
t
Thanks for reaching out, Arthur. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! If you want your agent to start speaking right as the user connects on an outbound call, you can simply use the
agent.say()
method right after the connection is made. Here’s a quick rundown of how to do it: 1. First, set up your agent with explicit dispatch to make sure outbound calls are routed correctly. 2. Next, create a SIP participant to kick off the outbound call to the phone number you want. 3. Then, as soon as the participant connects, use
agent.say()
so your agent can start talking. Here’s a little example to guide you:
Copy code
async def entrypoint(ctx: JobContext):
    # Create the SIP client and dial the phone number
    sip_client = SipClient(
        LIVEKIT_URL,
        LIVEKIT_API_KEY,
        LIVEKIT_API_SECRET
    )
    trunk_id = "your-trunk-id"
    phone_number = "+1234567890"
    room_name = ctx.room.name
    participant_identity = "outbound-call"

    # Create the SIP participant to dial the phone number
    await sip_client.create_sip_participant(
        trunk_id,
        phone_number,
        room_name,
        participant_identity
    )

    # Wait for the participant to connect
    participant = await ctx.wait_for_participant(participant_identity)

    # Initialize your agent
    agent = VoicePipelineAgent(...)

    # Start the agent and have it speak immediately
    agent.start(ctx.room, participant)
    await agent.say("Hello! How can I assist you today?", blocking=True)
This way, your agent will greet the user right when they pick up the call. If you want to dive deeper or have any questions, just let me know—I’m here to help!
w
what does ctx.wait_for_participant method do?
t
The
ctx.wait_for_participant
method in LiveKit is a handy asynchronous function that waits for someone to join a room. When they do, it gives you back a
RemoteParticipant
object representing that participant, so you can easily interact with them. You can even filter by their
identity
and
kind
, and if the participant is already there, it’ll return right away. It’s super useful when you want to kick off actions as soon as someone connects. Sources: examples/voice_agents/realtime_joke_teller.py