faint-arm-51818
12/03/2025, 11:26 AMtall-airline-16756
12/03/2025, 11:26 AMtall-airline-16756
12/03/2025, 11:26 AMroom.on('participantLeft', async (participant) => {
if (participant.identity === 'target-participant-identity') {
// gracefully stop the agent
ctx.shutdown('Participant removed, agent shutting down');
}
});
Example (Python):
@room.on("participant_left")
async def on_participant_left(participant):
if participant.identity == "target-participant-identity":
# gracefully stop the agent session
await session.aclose()
Would you like the sample adapted to match how you identify the participant (identity, SID, or role) or integrated into an AgentSession lifecycle?
Sources: Connecting to LiveKit | LiveKit docs | Webhooks | LiveKit docs | Job lifecycle | LiveKit docs | Handling events | LiveKit docs | Text and transcriptions | LiveKit docsfaint-arm-51818
12/03/2025, 11:28 AMtall-airline-16756
12/03/2025, 11:29 AMexport default defineAgent({
entry: async (ctx: JobContext) => {
ctx.addShutdownCallback(async () => {
// your callbacks / cleanup here
});
ctx.room.on('participantLeft', (participant) => {
if (participant.identity === 'target') {
ctx.shutdown('Participant removed');
}
});
}
});
Python
async def entrypoint(ctx: JobContext):
async def on_shutdown():
# your callbacks / cleanup here
ctx.add_shutdown_callback(on_shutdown)
@ctx.room.on("participant_left")
async def on_participant_left(participant):
if participant.identity == "target":
ctx.shutdown("Participant removed")
Notes:
• Use ctx.addShutdownCallback / ctx.add_shutdown_callback to register callbacks that will run during shutdown.
• Call ctx.shutdown() (or session.shutdown() / session.aclose() in some setups) when you detect the participant removal to start the shutdown flow that runs those callbacks.
Do you want this adapted for identifying the participant by SID or role, or integrated into an existing AgentSession snippet?
Sources: Job lifecycle | LiveKit docs