wonderful-wolf-51568
10/14/2025, 4:46 PMtall-airline-16756
10/14/2025, 4:46 PMtall-airline-16756
10/14/2025, 4:48 PMawait ctx.connect().
• Initialize the Agent and AgentSession.
• Start the session asynchronously with asyncio.create_task().
• If you're making outbound calls, use create_sip_participant() to kick off the call.
• Finish setting things up and let the entrypoint return.
After the entrypoint finishes, the ongoing call and session are managed asynchronously through the AgentSession and event-driven processes. If you'd like, I’m here to help with more details or examples—just let me know!
Sources: complex-agents/nutrition-assistant/agent.py | Job lifecycle | LiveKit Docs | livekit-agents/livekit/agents/voice/agent_activity.py | agent.pywonderful-wolf-51568
10/14/2025, 4:50 PMtall-airline-16756
10/14/2025, 4:51 PMon_exit() coroutine in your agent class. This method is called automatically during the drain process, making it a perfect spot to add any cleanup logic, like ending calls. Here’s a simple example to get you started:
class MyAgent(VoiceAssistant):
async def on_exit(self):
logger.info("Agent is shutting down. Performing cleanup...")
# Add call-ending logic here
• Another helpful tip is to use ctx.add_shutdown_callback() to register a shutdown callback for any extra cleanup tasks you might have. Here’s how you might do that:
async def entrypoint(ctx: JobContext):
async def on_shutdown():
logger.info("Shutting down session")
# Optionally, end the call for all participants
# await ctx.room.context.delete_room()
ctx.add_shutdown_callback(on_shutdown)
And just a little heads-up: if you want to end the call for everyone involved, using the delete_room API is the way to go. Simply shutting down the agent session won’t disconnect participants—they’ll stay in the call until they hang up themselves.
Please feel free to reach out if you’d like me to walk you through this with more examples or if anything feels unclear. I’m here to help!
Sources: Agent Session & Activity Management | livekit/livekit_composite | DeepWiki | livekit-agents/livekit/agents/voice/agent_activity.py | Agents telephony integration | LiveKit Docs | Job lifecycle | LiveKit Docs | How to detect and handle silence during callswonderful-wolf-51568
10/14/2025, 7:04 PMtall-airline-16756
10/14/2025, 7:06 PMshutdown_process_timeout period.
• Monitor and Handle Timeout Exceptions: Catching and logging timeout exceptions can really help you keep track of any incomplete shutdowns. Here’s a simple example:
try:
await worker.drain(timeout=worker_opts.drain_timeout)
except asyncio.TimeoutError:
logger.warning("Drain timeout exceeded, some jobs may not have completed gracefully")
# Add custom cleanup logic here
• Use Tiered Timeout Configuration: You can adjust drain_timeout and shutdown_process_timeout based on how your jobs behave. For instance:
# For long-running operations
WorkerOptions(
drain_timeout=3600, # 1 hour
shutdown_process_timeout=120.0, # 2 minutes per process
)
• Custom Shutdown Logic: Using session.shutdown(drain=True) lets current operations finish before closing, which is really helpful.
• Signal-Based Escalation: And if a graceful shutdown doesn’t work, you can always send a second termination signal (like Ctrl+C) to force an immediate shutdown.
If you’d like, I’m happy to dive deeper into any of these strategies or help you implement them. Just let me know!
Sources: Job lifecycle | LiveKit Docs | Worker options | LiveKit Docs | Worker lifecycle | LiveKit Docs | examples/voice_agents/session_close_callback.py | agents/src/worker.ts