explain about add_participant_entrypoint in detail
# ask-ai
h
explain about add_participant_entrypoint in detail
t
Thanks for reaching out, Abhishek. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm happy to walk you through the
add_participant_entrypoint
function in LiveKit's Agents framework! This function lets you register an asynchronous function that runs whenever a new participant joins a room. It’s designed to also trigger for participants who are already in the room when your agent connects, so you can be sure all participants are handled smoothly and consistently. Here are some key features to keep in mind: • It accepts a coroutine with parameters for
JobContext
and
rtc.RemoteParticipant
, which means you can perform actions tailored to each participant. • You can filter participants by type (like standard or SIP) using the
kind
parameter, giving you more control. • To avoid confusion, it prevents duplicate entrypoints by raising an error if you try to add the same function more than once. • Each participant gets its own task that’s automatically cleaned up once done, making resource management easier. • Just a heads-up: make sure to register this before calling
ctx.connect()
so you don’t miss any join events. Here’s a quick example to illustrate how it works:
Copy code
async def participant_task(ctx: JobContext, p: rtc.RemoteParticipant):
    <http://logger.info|logger.info>(f"Participant joined: {p.identity}")

async def entrypoint(ctx: JobContext):
    ctx.add_participant_entrypoint(entrypoint_fnc=participant_task, kind=rtc.ParticipantKind.PARTICIPANT_KIND_STANDARD)
    await ctx.connect(auto_subscribe=AutoSubscribe.SUBSCRIBE_ALL)
If you’re diving into more advanced scenarios, you can filter participants, manage stateful interactions, or even use room event listeners for even finer control. Please don’t hesitate to ask if you’d like me to explain any part in more detail or help you set things up! Sources: complex-agents/ivr-agent/agent.py | examples/primitives/participant_entrypoint.py | Job lifecycle | LiveKit Docs | agents/src/job.ts | Building an Automated IVR Menu Caller | LiveKit Docs
h
can I run avatar session inside add_participant_entrypoint's participant_task
t
Absolutely, you can run an avatar session inside the
participant_task
defined in
add_participant_entrypoint
. This task gives you access to the
JobContext
and
RemoteParticipant
, which are just what you need to initialize and start an avatar session smoothly. Here’s a friendly example for you:
Copy code
async def participant_task(ctx: JobContext, participant: rtc.RemoteParticipant):
    session = AgentSession(
        # Configure STT, LLM, TTS, etc.
    )

    avatar = hedra.AvatarSession(
        avatar_id="...",  # Specify avatar ID
    )
    await avatar.start(session, room=ctx.room)
    await session.start(
        room=ctx.room,
        # Additional options
    )

# Register the entrypoint
ctx.add_participant_entrypoint(entrypoint_fnc=participant_task)
This method works well because the avatar session setup relies on the
JobContext
and room, both of which you can easily access within the task. If you have any questions or want me to walk you through any part of this, just let me know—I’m here to help! Sources: livekit-plugins/livekit-plugins-simli/livekit/plugins/simli/avatar.py | Virtual avatar integrations | LiveKit Docs
h
from dotenv import load_dotenv from livekit import agents from livekit.agents import AgentSession, Agent, RoomInputOptions from livekit.plugins import ( openai, cartesia, deepgram, noise_cancellation, silero, ) from livekit import rtc from livekit.plugins.turn_detector.multilingual import MultilingualModel load_dotenv(".env.local") class Assistant(Agent): def __init__(self) -> None: super().__init__(instructions="You are a helpful voice AI assistant.") async def entrypoint(ctx: agents.JobContext): print("User joining....") async def participant_task_1(ctx: agents.JobContext, p: rtc.RemoteParticipant): # You can filter out participants you are not interested in # if p.identity != "some_identity_of_interest": # return print("helloooooooooooo-------------------------------") print("\n----------p ---------------") print(p) print("---------------------------\n") print(f"participant task 1 starting for {p.identity}") session = AgentSession( stt=deepgram.STT(model="nova-3", language="multi"), llm=openai.LLM(model="gpt-4o-mini"), tts=cartesia.TTS(model="sonic-2", voice="f786b574-daa5-4673-aa0c-cbe3e8534c02"), vad=silero.VAD.load(), turn_detection=MultilingualModel(), ) await session.start( room=ctx.room, agent=Assistant(), room_input_options=RoomInputOptions( # For telephony applications, use
BVCTelephony
instead for best results noise_cancellation=noise_cancellation.BVC(), ), ) await session.generate_reply( instructions="Greet the user and offer your assistance." ) # Do something with p.attributes, p.identity, p.metadata, etc. # my_stuff = await fetch_stuff_from_my_db(p) # Do something # await asyncio.sleep(60) print(f"participant task done for {p.identity}") ctx.add_participant_entrypoint(entrypoint_fnc=participant_task_1) print("\n--------------GREETED --------------------\n") if name == "__main__": agents.cli.run_app(agents.WorkerOptions(entrypoint_fnc=entrypoint)) will this code work?
t
It looks like your code is on the right track and should work just fine! The
add_participant_entrypoint
function is meant to register asynchronous tasks for participants, and creating an
AgentSession
within the participant task is totally supported. Here are a few important things to keep in mind: • Make sure to call
ctx.add_participant_entrypoint
before
ctx.connect()
. • The
AgentSession.start()
method needs a valid
room
reference and
agent
instance, both of which you have in your code. • The framework is designed to handle dynamic session creation for each participant, just like in the official examples. If you run into any specific challenges or have questions while running your code, please don’t hesitate to reach out—I’m here to help! Sources: evaluating-agents/agent_to_test.py | Job lifecycle | LiveKit Docs | examples/voice_agents/realtime_joke_teller.py