flaky-scooter-85113
12/03/2025, 4:57 PMtall-airline-16756
12/03/2025, 4:58 PMtall-airline-16756
12/03/2025, 4:58 PMctx.shutdown('reason') from the agent entrypoint to gracefully disconnect and run shutdown hooks.
If you mean removing everyone (including the agent) by tearing down the room, use the RoomServiceClient.deleteRoom(roomName) call.
Which of those fits your use case, or share a short snippet of how your agent is started so I can give exact code?
Sources: Job lifecycle | LiveKit docsflaky-scooter-85113
12/03/2025, 4:59 PMtall-airline-16756
12/03/2025, 4:59 PMctx.shutdown(<reason>) from the agent’s entrypoint. That gracefully disconnects the agent, runs shutdown hooks, and leaves the room intact for other participants.
Example:
export default defineAgent({
entry: async (ctx: JobContext) => {
// perform any final work you need, then eject the agent
ctx.shutdown('Agent ejected from room');
},
});
If you want, tell me whether you need the agent to finish certain async tasks before shutdown (I can show a safe pattern to await them before calling ctx.shutdown).flaky-scooter-85113
12/03/2025, 5:00 PMtall-airline-16756
12/03/2025, 5:00 PMctx.shutdown('reason') when ready — this runs shutdown hooks and leaves the room intact.
If it's external: I can show the TypeScript AgentServiceClient call to stop the job if you provide whether you have job/session IDs and which SDK (TypeScript/REST) you prefer.
Tell me which option applies and whether you want graceful vs immediate; I’ll give the minimal code snippet.
Sources: Job lifecycle | LiveKit docsflaky-scooter-85113
12/03/2025, 5:06 PMtall-airline-16756
12/03/2025, 5:07 PMJobContext (or AgentSession) accessible where you later decide to eject the agent.
• When ready, call ctx.shutdown('reason') to trigger graceful shutdown and run onExit hooks.
Example pattern:
let storedCtx: JobContext | null = null;
export default defineAgent({
entry: async (ctx: JobContext) => {
storedCtx = ctx;
// normal agent work...
await ctx.waitUntilDone(); // or other long-running logic
},
onExit: async (ctx) => {
// cleanup that must run on shutdown
},
});
// somewhere else in this process, when you want to eject:
if (storedCtx) {
storedCtx.shutdown('Kick for SIP call'); // graceful: runs onExit
}
2) From an external controller/service (if your tool runs outside the agent process)
• Use the Agents control API to stop the job/session for that agent. Stopping the agent job triggers the agent's shutdown lifecycle so onExit runs.
• Call the AgentServiceClient (or equivalent control endpoint) with the agent's job/session identifier to request a graceful stop.
If you want, tell me:
• Do you run the SIP handoff tool inside the same agent process or in an external controller?
• Do you already have access to the `JobContext`/session reference or the agent job/session ID?
I’ll provide the exact snippet for the option you use.flaky-scooter-85113
12/03/2025, 5:08 PMtall-airline-16756
12/03/2025, 5:09 PMsession, you can trigger a graceful shutdown so onExit runs. Use the session shutdown API from the Agents Framework.
If you're in TypeScript and have the `AgentSession`/`JobContext` instance, call:
// graceful: allows in-flight work and runs onExit hooks
await session.shutdown(/* optional reason */ 'Kick for SIP handoff');
If you prefer an immediate disconnect (no drain), use:
await session.aclose();
Notes:
• Use shutdown() when you want onExit and shutdown hooks to run.
• Ensure any async work you need finished is awaited before or allow shutdown() to drain if supported.
If you want, paste the shape/type of your session object (or a short snippet where it’s stored) and I’ll give the exact minimal code to call from your handoff logic.flaky-scooter-85113
12/03/2025, 5:10 PMProperty 'aclose' does not exist on type 'AgentSessionunknown'. Did you mean 'close' ?tall-airline-16756
12/03/2025, 5:10 PMshutdown() for a graceful exit (it triggers onExit and shutdown hooks). close() is an immediate disconnect and won't run onExit.
Example TypeScript using your stored `session`:
// graceful: runs onExit and lets the agent drain
await session.shutdown('Kick for SIP handoff');
If you need an immediate disconnect (no onExit):
await session.close();
Do you want a pattern to await in-flight tasks before calling shutdown() or to guard against multiple shutdown calls?flaky-scooter-85113
12/03/2025, 5:11 PMtall-airline-16756
12/03/2025, 5:11 PMawait session.shutdown({ drain: true }); to gracefully eject the agent and ensure its onExit runs.
• aclose() is a Python async method; it does not exist on the TypeScript AgentSession.
• Don’t use close() on AgentSession in TypeScript — the supported, documented method for graceful termination is shutdown().
If you want, I can show a minimal snippet that awaits in-flight tasks and guards against multiple shutdown calls. Which would help you more: a short pattern for awaiting tasks, or a safe "call-once" shutdown wrapper?
Sources: Pipeline nodes and hooks | LiveKit docs | Job lifecycle | LiveKit docs | Agent session | LiveKit docsflaky-scooter-85113
12/03/2025, 5:12 PMProperty 'shutdown' does not exist on type 'AgentSession<unknown>'.tall-airline-16756
12/03/2025, 5:13 PMAgentSession in the TypeScript Agents SDK does not have shutdown() (and aclose() is Python-only).
• To gracefully eject an agent so its onExit runs, call ctx.shutdown(reason) on the JobContext that started the session.
Minimal example pattern (store ctx so you can call it later):
let storedCtx: JobContext | null = null;
export default defineAgent({
entry: async (ctx: JobContext) => {
storedCtx = ctx;
// start session / do work
},
onExit: async () => {
// cleanup logic that must run on graceful eject
},
});
// later, when you want to kick the agent:
if (storedCtx) {
storedCtx.shutdown('Kick for SIP handoff'); // triggers onExit and graceful cleanup
}
If you need a safe "call-once" wrapper or a pattern to trigger this from an external controller, tell me whether your controller runs inside the same process or externally and I’ll provide the exact snippet.
Sources: Job lifecycle | LiveKit docsflaky-scooter-85113
12/03/2025, 5:13 PMtall-airline-16756
12/03/2025, 5:13 PMsession.shutdown() (it does not exist on AgentSession in the TypeScript SDK).
• aclose() is a Python async method and not available in TypeScript.
• session.close() is not the documented way to trigger the agent lifecycle hooks; ctx.shutdown() is.
Minimal example pattern:
let storedCtx: JobContext | null = null;
export default defineAgent({
entry: async (ctx: JobContext) => {
storedCtx = ctx;
// start session / normal work
},
onExit: async () => {
// cleanup that must run on graceful eject
},
});
// later, when you want to eject:
if (storedCtx) {
storedCtx.shutdown('Kick for SIP handoff'); // graceful — runs onExit
}
Why trust this: the JobContext is responsible for job lifecycle and graceful termination in the TypeScript agents flow; calling ctx.shutdown() triggers shutdown hooks and the agent's exit lifecycle. I can paste the exact doc excerpt or give a "call-once" wrapper if you want—which would you prefer?flaky-scooter-85113
12/03/2025, 5:16 PMtall-airline-16756
12/03/2025, 5:17 PMctx.shutdown(...).
B) You can modify the agent code — I’ll give an RPC example where the agent registers an RPC method the external controller can call to ask it to ctx.shutdown(...).
C) You cannot modify the agent code — you accept a forceful removal fallback (server RemoveParticipant), which will disconnect the agent but will not guarantee onExit runs.
Reply A, B, or C and which language (TypeScript or Python). I’ll provide the exact minimal code.
Sources: Job lifecycle | LiveKit docs | Pipeline nodes and hooks | LiveKit docs | Agent session | LiveKit docs | Managing participants | LiveKit docs | Text and transcriptions | LiveKit docsflaky-scooter-85113
12/03/2025, 5:18 PMtall-airline-16756
12/03/2025, 5:19 PMctx.shutdown() from outside the agent, so pick one of two reliable patterns I can give exact TypeScript snippets for:
A) Polling/flag (Redis, DB, etc.): agent periodically checks a shared key and calls ctx.shutdown(...) when set. Best if you want simple reliability and the agent can be modified.
B) RPC: agent registers an RPC method the external tool calls to ask the agent to call ctx.shutdown(...) immediately. Best for low-latency control.
Or C) Force-remove (server RemoveParticipant) — works without changing agent code but does NOT guarantee onExit() runs.
Which option do you want (A, B, or C)? I’ll return a minimal, copy-paste TypeScript example for that option.flaky-scooter-85113
12/03/2025, 5:19 PMtall-airline-16756
12/03/2025, 5:20 PMctx.shutdown(...), triggering onExit.
Agent (TypeScript, inside your defineAgent):
import { defineAgent, JobContext } from '@livekit/agents';
import { voice } from '@livekit/agents';
export default defineAgent({
entry: async (ctx: JobContext) => {
const agent = new voice.Agent({ instructions: 'You may be ejected via RPC.' });
// register RPC method on the agent's local participant
ctx.room.localParticipant?.registerRpcMethod('shutdown_agent', async () => {
console.log('RPC shutdown requested');
ctx.shutdown('shutdown_requested_via_rpc'); // graceful: triggers onExit
return { status: 'ok', message: 'Agent shutting down' };
});
const session = new voice.AgentSession({ /* config */ });
await session.start({ agent, room: ctx.room });
// keep agent running as usual...
},
onExit: async (ctx: JobContext) => {
// guaranteed to run on graceful shutdown
console.log('onExit running: cleanup and final messages here');
// e.g., await ctx.session?.generateReply({ instructions: 'Goodbye.' });
},
});
External caller (any participant that can perform RPC; TypeScript example):
// assume `localParticipant` is a connected LiveKit Participant instance
const res = await localParticipant.performRpc({
destinationIdentity: 'agent-identity', // agent's identity in the room
method: 'shutdown_agent',
payload: '',
responseTimeout: 10000,
});
console.log('RPC response:', res);
Notes and tips
• Ensure the agent's identity is known/stable so the caller targets the right participant.
• Confirm the agent has registered the RPC before calling (race protection on connect).
• RPC triggers the agent itself to run ctx.shutdown(...), so onExit runs reliably.
• If you want, I can provide a tiny "wait-for-rpc-registration" snippet or a call-once guard for ctx.shutdown. Which would you prefer? Sources: Tool definition and use | LiveKit docs | Agents (Node.js) | livekit/livekit_composite | DeepWiki | agents/src/voice/agent.ts | packages/livekit-rtc/src/participant.ts | README.mdflaky-scooter-85113
12/03/2025, 5:24 PMtall-airline-16756
12/03/2025, 5:25 PMflaky-scooter-85113
12/03/2025, 5:26 PMflaky-scooter-85113
12/03/2025, 5:26 PM