colossal-lock-10836
09/02/2025, 5:16 PMtall-airline-16756
09/02/2025, 5:16 PMtall-airline-16756
09/02/2025, 5:17 PMctx.shutdown() inside the agent’s entrypoint function. This will gently disconnect the agent from the room but keep everyone else connected. Here’s a quick example:
async def entrypoint(ctx: JobContext):
# Perform work
...
# Disconnect the agent from the room
ctx.shutdown(reason="Session ended")
• Ending the Session for Everyone: If you want to close things down for all participants, use the server API delete_room. This will disconnect everyone and remove the room. Check out this example:
from livekit import api
async def entrypoint(ctx: JobContext):
# Perform work
...
# End the session for everyone
api_client = api.LiveKitAPI(
os.getenv("LIVEKIT_URL"),
os.getenv("LIVEKIT_API_KEY"),
os.getenv("LIVEKIT_API_SECRET"),
)
await api_client.room.delete_room(
api.DeleteRoomRequest(room=ctx.job.room.name)
)
If you have any questions or want me to walk you through anything, just let me know—I’m here to help!
Sources: basics/exit_message.py | Job lifecycle | LiveKit Docs | Inside a session | LiveKit Docs | Agents telephony integration | LiveKit Docs | livekit-plugins/livekit-plugins-aws/livekit/plugins/aws/experimental/realti...colossal-lock-10836
09/02/2025, 5:17 PMtall-airline-16756
09/02/2025, 5:18 PMsession.aclose() inside a function tool to gracefully close the agent’s session while keeping the room open for others. Check out this example:
@function_tool\nasync def end_session(self):\n """End the agent session."""\n await self.session.drain()\n await self.session.aclose()
• Ending the Session for Everyone: If you want to wrap things up for all participants, use the server API delete_room. Here’s an example of how to do that:
@function_tool\nasync def end_call(self, ctx: RunContext):\n """End the call for all participants."""\n # Let the current speech finish\n await ctx.wait_for_playout()\n # Say goodbye\n await ctx.session.generate_reply(instructions="Goodbye!")\n # Delete the room\n job_ctx = get_job_context()\n await job_ctx.api.room.delete_room(api.DeleteRoomRequest(room=job_ctx.room.name))
If you have any questions or want a hand with setting this up, just let me know—I’m here to help!
Sources: Agents telephony integration | LiveKit Docs | Agent speech and audio | LiveKit Docs | telephony/sip_lifecycle.py | Job lifecycle | LiveKit Docs | agent.pycolossal-lock-10836
09/02/2025, 5:19 PMtall-airline-16756
09/02/2025, 5:20 PM@function_tool
async def end_session(self):
"""End the agent session."""
# Let any ongoing speech finish
await self.session.drain()
# Then close the session
await self.session.aclose()
2. Ending the Session for Everyone:
@function_tool
async def end_call(self, ctx: RunContext):
"""End the call for all participants."""
# Wait for the current speech to finish
await ctx.wait_for_playout()
# Say a friendly goodbye
await ctx.session.generate_reply(instructions="Goodbye!")
# Finally, delete the room
job_ctx = get_job_context()
await job_ctx.api.room.delete_room(api.DeleteRoomRequest(room=job_ctx.room.name))
If you’d like me to walk you through anything else or need more examples, just let me know—I’m here to help!