I have setup a livekit agent worker which suspends...
# ask-ai
p
I have setup a livekit agent worker which suspends after 5 mins of inactivity. it only wakes up if accessed through an http endpoint and not when a sip call is made to connect to it. how can i enable the sip trunk to first hit the http endpoint to wake the suspended machine and then actually route the call via livekit sip.
t
Thanks for reaching out, Vaneeza. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks for reaching out! To better assist you, could you please let me know if you're using LiveKit Cloud or a self-hosted deployment? Also, is the agent worker suspension handled by LiveKit directly, or is it managed by an external orchestrator? Understanding this will really help us find the best way to integrate SIP trunks with HTTP endpoints for your needs. Looking forward to your reply! Sources: Using Wavix to accept and make calls | LiveKit Docs | README.md | packages/sip--um-actually/README.md | Agent-assisted warm transfer | LiveKit Docs | SIP APIs | LiveKit Docs
p
i'm self-hosting the livekit agent (python worker) on fly.io and the suspension of the agent worker is handled by fly.io, not livekit. Fly.io proxy watches for HTTP/TCP connections. After ~5 minutes of no connections, marks as "excess capacity". and it suspends the machine to save costs.
t
I'm glad you're looking into enabling your SIP trunk to wake up a suspended LiveKit agent worker hosted on Fly.io. To help you with this, you can set up a dispatcher service that works hand-in-hand with Fly.io's autoscaling and your SIP provider's webhook features. Here's a simple overview of how you might approach this: • Create a dispatcher service: This service will be running continuously, ready to listen for HTTP requests. It can interact with Fly.io's API or autoscaling triggers to wake up those suspended workers. • Configure your SIP provider: You’ll want to set up a webhook in your SIP provider that sends an HTTP request to your dispatcher just before routing calls to LiveKit. • Wake up the worker: The dispatcher then triggers Fly.io’s wake-up process and waits until the worker is ready before letting the SIP call go through. To give you an idea, here’s a straightforward example using a Python-based dispatcher pattern that handles incoming HTTP requests and launches workers:
Copy code
class SIPWorkerDispatcher:
    async def handle_incoming_call(self, sip_call_info):
        # Wake up suspended Fly.io worker
        await self.wake_fly_worker(sip_call_info.room_name)
        
        # Wait for worker to be ready
        await self.wait_for_worker_ready()
        
        # Allow SIP call to proceed to LiveKit
        return {"status": "worker_ready"}
Many SIP providers, like Twilio, support webhooks that trigger before the call routing happens. You can easily point these webhooks to your dispatcher service. To make sure we tailor this perfectly for your setup, could you please share which SIP provider you're using and any details you have about the wake-up timing for your Fly.io workers? That way, I can help you refine this even further. Sources: packages/sip--um-actually/README.md | README.md | Agents telephony integration | LiveKit Docs | SIP trunk setup | LiveKit Docs | agents/src/http_server.ts
p
my sip provider is telnyx and im using telnyx's FQDN connection to route calls to livekit. For the wakeup timing, when a http health endpoint is hit, the suspended fly.io machine wakes up and consequently the livekit agent worker starts running on that machine after a few seconds. Now i want this to happen before the call routing to livekit, otherwise it won't be able to connect to the agent, because the agent wouldn't exist at that point.
t
I understand you want to make sure the Fly.io machine wakes up before routing calls to LiveKit, and I’m here to help you with that! To achieve this, you’ll need to set up Telnyx to trigger an HTTP webhook before the call gets routed. This depends on Telnyx’s platform supporting this feature, as it happens before LiveKit’s SIP trunk integration. Telnyx does offer call control features and webhook settings that might let you configure a pre-call webhook. I recommend checking out their documentation or reaching out to their support team to see if this is something you can set up. Once the webhook is triggered, it can call the HTTP health endpoint on Fly.io, waking up your machine so the LiveKit agent worker is ready before the call is routed. If you’d like, I’m more than happy to help you with the LiveKit part of the setup, like configuring the SIP trunk or dispatch rules. Just let me know, and we can walk through it together! Sources: Create and configure Telnyx SIP trunk | LiveKit Docs | Secure trunking | LiveKit Docs
p
i have set webhook timeout in telnyx fqdn sip trunk connection to be 25 secs. And when sip call is routed, it first wakesup the server through that endpoint/webhook. and then i get this error. the call does get forwarded but these errors initially: {"message": "assignment for job AJ_EmEJE8jCKWvp timed out", "level": "WARNING", "name": "livekit.agents", "job_request": "<livekit.agents.job.JobRequest object at 0x7f3e483c6dd0>", "agent_name": "Alex_44", "timestamp": "2025-10-13T203141.281288+00:00"} 2025-10-13T203142.015 app[e825477aee9608] iad [info] {"message": "job_request_fnc failed", "level": "ERROR", "name": "livekit.agents", "exc_info": "Traceback (most recent call last):\n File \"/home/appuser/.local/lib/python3.11/site-packages/livekit/agents/worker.py\", line 886, in _job_request_task\n await self._opts.request_fnc(job_req)\n File \"/home/appuser/.local/lib/python3.11/site-packages/livekit/agents/worker.py\", line 69, in _default_request_fnc\n await ctx.accept()\n File \"/home/appuser/.local/lib/python3.11/site-packages/livekit/agents/job.py\", line 548, in accept\n await self._on_accept(accept_arguments)\n File \"/home/appuser/.local/lib/python3.11/site-packages/livekit/agents/worker.py\", line 857, in _on_accept\n raise AssignmentTimeoutError() from None\nlivekit.agents._exceptions.AssignmentTimeoutError", "job_request": "<livekit.agents.job.JobRequest object at 0x7f3e483c6dd0>", "agent_name": "Alex_44", "timestamp": "2025-10-13T203141.281724+00:00"} 2025-10-13T203142.015 app[e825477aee9608] iad [info] {"message": "assignment for job AJ_EmEJE8jCKWvp timed out", "level": "WARNING", "name": "livekit.agents", "job_request": "<livekit.agents.job.JobRequest object at 0x7f3e483c7a10>", "agent_name": "Alex_44", "timestamp": "2025-10-13T203142.014959+00:00"} 2025-10-13T203142.016 app[e825477aee9608] iad [info] {"message": "job_request_fnc failed", "level": "ERROR", "name": "livekit.agents", "exc_info": "Traceback (most recent call last):\n File \"/home/appuser/.local/lib/python3.11/site-packages/livekit/agents/worker.py\", line 886, in _job_request_task\n await self._opts.request_fnc(job_req)\n File \"/home/appuser/.local/lib/python3.11/site-packages/livekit/agents/worker.py\", line 69, in _default_request_fnc\n await ctx.accept()\n File \"/home/appuser/.local/lib/python3.11/site-packages/livekit/agents/job.py\", line 548, in accept\n await self._on_accept(accept_arguments)\n File \"/home/appuser/.local/lib/python3.11/site-packages/livekit/agents/worker.py\", line 857, in _on_accept\n raise AssignmentTimeoutError() from None\nlivekit.agents._exceptions.AssignmentTimeoutError", "job_request": "<livekit.agents.job.JobRequest object at 0x7f3e483c7a10>", "agent_name": "Alex_44", "timestamp": "2025-10-13T203142.015210+00:00"} 2025-10-13T203142.016 app[e825477aee9608] iad [info] {"message": "assignment for job AJ_EmEJE8jCKWvp timed out", "level": "WARNING", "name": "livekit.agents", "job_request": "<livekit.agents.job.JobRequest object at 0x7f3e4842d210>", "agent_name": "Alex_44", "timestamp": "2025-10-13T203142.016026+00:00"} 2025-10-13T203142.016 app[e825477aee9608] iad [info] {"message": "job_request_fnc failed", "level": "ERROR", "name": "livekit.agents", "exc_info": "Traceback (most recent call last):\n File \"/home/appuser/.local/lib/python3.11/site-packages/livekit/agents/worker.py\", line 886, in _job_request_task\n await self._opts.request_fnc(job_req)\n File \"/home/appuser/.local/lib/python3.11/site-packages/livekit/agents/worker.py\", line 69, in _default_request_fnc\n await ctx.accept()\n File \"/home/appuser/.local/lib/python3.11/site-packages/livekit/agents/job.py\", line 548, in accept\n await self._on_accept(accept_arguments)\n File \"/home/appuser/.local/lib/python3.11/site-packages/livekit/agents/worker.py\", line 857, in _on_accept\n raise AssignmentTimeoutError() from None\nlivekit.agents._exceptions.AssignmentTimeoutError", "job_request": "<livekit.agents.job.JobRequest object at 0x7f3e4842d210>", "agent_name": "Alex_44", "timestamp": "2025-10-13T203142.016128+00:00"} 2025-10-13T203229.871 app[e825477aee9608] iad [info] 🔔 Health check/Wake webhook from 172.19.1.65 - Machine ready for SIP routing
t
The
AssignmentTimeoutError
happens when the LiveKit agent worker times out while waiting for a job assignment, which often occurs because of the delay in waking up the Fly.io machine. Let’s walk through some steps that might help resolve this: 1. Increase the assignment timeout: By default, the timeout is set to 7.5 seconds. You can give the worker more time to wake up and initialize by adjusting the configuration like this:
Copy code
WorkerOptions(
    entrypoint_fnc=entrypoint,
    agent_name="your-sip-agent",
    max_retry=16,
    drain_timeout=1800,
    initialize_process_timeout=20.0  # Increase initialization timeout
)
2. Check your agent dispatch configuration: It’s important to make sure the
agent_name
in your worker configuration exactly matches the
agent_name
in the SIP dispatch rule. This helps ensure calls are routed correctly. 3. Add connection recovery handling: Including error handling can help monitor and recover from any connection issues during the wake-up phase. For example:
Copy code
@session.on("error")
def on_error(ev: ErrorEvent):
    if ev.error.recoverable:
        return
    logger.info(f"Session is closing due to unrecoverable error {ev.error}")
4. Pre-wake the machine: If you can, try configuring the Telnyx webhook to trigger a bit earlier. This way, the machine has enough time to be fully ready before the SIP call comes through. Could you please check if the
agent_name
is set correctly in both your worker and dispatch rule? And if you’d like, I’m here to help guide you through increasing the timeout or making any of these adjustments. Just let me know! Sources: Agent dispatch | LiveKit Docs | agent.py | examples/voice_agents/error_callback.py | SIP troubleshooting guide | LiveKit Docs
p
yes the agent name is setup correctl because the call gets routed
t
It looks like the agent name is set up correctly and the call is being routed, so the hiccup might be happening because the worker takes a bit of time to initialize after the Fly.io machine wakes up. Here are a few friendly suggestions to help smooth things out: 1. Increase the assignment timeout: You can tweak the worker configuration to give it a little extra time to get ready after waking up. For example:
Copy code
WorkerOptions(
    entrypoint_fnc=entrypoint,
    agent_name="your-sip-agent",
    initialize_process_timeout=20.0  # Increase initialization timeout
)
2. Pre-wake the machine earlier: Try adjusting the Telnyx webhook timing so it triggers a bit earlier, making sure the machine is fully awake before the SIP call comes through. 3. Monitor worker readiness: It’s a good idea to use the health check endpoint to confirm the worker is all set before routing the call. This can really help avoid those timing glitches. If you’d like, I’m here to help you implement these changes or troubleshoot further if the issue keeps popping up. Just let me know!
p
given the error how much timeout should be increased?
t
Thanks for sharing those details! To find the right timeout increase, it’s helpful to think about how long it takes for your Fly.io machine to wake up and for the LiveKit agent worker to get going. From what you described, the machine wakes up within a few seconds after the HTTP health endpoint is hit, which is great to know. To give it a comfortable buffer, you might want to try increasing the timeout to around 20-30 seconds. Here’s an example of how you could update the worker configuration:
Copy code
WorkerOptions(
    entrypoint_fnc=entrypoint,
    agent_name="your-sip-agent",
    initialize_process_timeout=30.0  # Set timeout to 30 seconds
)
If you still run into issues, no worries at all! You can tweak the timeout further based on what you observe. And please feel free to reach out if you’d like a hand with monitoring the timing or adjusting these settings—I’m here to help!
p
what do these params do and do i need to change them as well? max_retry=16, drain_timeout=1800,
t
I’d love to help you understand what the parameters
max_retry
and
drain_timeout
do: • `max_retry`: This sets the maximum number of times the worker will try to reconnect to the LiveKit server if the connection drops. If you’re facing occasional network hiccups or temporary disconnections, increasing this value might help. Usually, the default setting works well unless you notice frequent connection issues. • `drain_timeout`: This determines how long (in seconds) the worker waits for ongoing jobs to finish before it shuts down. A longer timeout means active jobs won’t be cut off during shutdown. The default of 1800 seconds (30 minutes) is typically just right, but you can adjust it if you need a shorter or longer shutdown period. Most of the time, you won’t need to tweak these settings unless you’re running into reconnection troubles or jobs getting interrupted during shutdown. Please feel free to reach out if you’d like me to explain anything further or help you adjust these parameters!