I want to run the LiveKit agent inside a FastAPI a...
# ask-ai
b
I want to run the LiveKit agent inside a FastAPI application, we are running FastAPI with uvicorn, we want to start a thread that runs the worker. Is it safe? The SIGTERM is going to reach LiveKit safely?
t
Thanks for reaching out, Martín. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Running the LiveKit agent inside a FastAPI application with Uvicorn is definitely possible, but I’d gently suggest avoiding running the agent in a thread. This is because signal handling, like SIGTERM, doesn’t work well in a background thread since the LiveKit worker’s signal handlers are only set up in the main thread. A smoother approach would be to run the LiveKit agent in its own subprocess or container. This way, signal handling and lifecycle management work more reliably. You can still have FastAPI act as a dispatcher or control plane for the agent. Also, don’t forget to set the
drain_timeout
and deployment grace periods (like Kubernetes’
terminationGracePeriodSeconds
) so the agent has enough time to finish up any ongoing tasks during shutdown. If you’d like, I’m here to help you with setting this up or answer any questions you have! Sources: agents/src/cli.ts | livekit-plugins/livekit-plugins-browser/livekit/plugins/browser/proc_main.p... | Job lifecycle | LiveKit Docs | Deploying to custom environments | LiveKit Docs