how to disable agent just after a participant remo...
# ask-ai
f
how to disable agent just after a participant removed?
t
Thanks for reaching out, Taras. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You can disable the agent by listening for the participant-removed event and shutting down the agent session when the target participant is removed. Which environment are you using — Node.js or Python? I can give the exact code for your setup. Example (Node.js):
Copy code
room.on('participantLeft', async (participant) => {
  if (participant.identity === 'target-participant-identity') {
    // gracefully stop the agent
    ctx.shutdown('Participant removed, agent shutting down');
  }
});
Example (Python):
Copy code
@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 docs
f
how do I ensure my callbacks are run after this?
t
You're asking how to ensure your callbacks run after you disable the agent when a participant is removed — correct? If so, register your post-shutdown work with the agent shutdown hook and then trigger a graceful shutdown when the participant leaves. Choose one of these patterns (tell me Node.js or Python and I’ll adapt): Node.js
Copy code
export 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
Copy code
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