damp-belgium-53878
07/21/2025, 5:09 PMtall-airline-16756
07/21/2025, 5:09 PMtall-airline-16756
07/21/2025, 5:10 PM(ctx: JobContext, p: rtc.RemoteParticipant).
• Add them using ctx.add_participant_entrypoint(entrypoint_fnc=...) before calling ctx.connect().
• Each callback runs in parallel for every participant that joins.
async def entrypoint(ctx: JobContext):
async def participant_task(ctx: JobContext, p: rtc.RemoteParticipant):
# Your async logic here
await asyncio.sleep(10)
ctx.add_participant_entrypoint(entrypoint_fnc=participant_task)
await ctx.connect(auto_subscribe=AutoSubscribe.SUBSCRIBE_ALL)
2. Shutdown Callbacks
• Declare an async function for cleanup or finalization.
• Add it using ctx.add_shutdown_callback(...).
async def entrypoint(ctx: JobContext):
async def my_shutdown_hook():
# Cleanup logic
...
ctx.add_shutdown_callback(my_shutdown_hook)
3. Other Async Callbacks
• You can also declare async callbacks for agent events, such as metrics collection.
@agent.on("metrics_collected")
def _on_metrics_collected(mtrcs: metrics.AgentMetrics):
# Handle metrics
...
Key requirements:
• All callbacks must be declared as async functions.
• Participant entrypoint callbacks must be added before ctx.connect().
• Shutdown callbacks should complete quickly (default timeout is 60 seconds).
• Each callback type has specific signature requirements.
This approach allows you to run async logic for participants, cleanup, and other agent events in your entrypoint function.
Sources: livekit-agents/livekit/agents/ipc/job_proc_lazy_main.py | Inside a session | LiveKit Docs | chain-of-thought-tts/agent.py