I am getting this error quite often: I have used ...
# ask-ai
m
I am getting this error quite often: I have used cartesia sonic 3 as the TTS model for my voice agent (self-hosted)
Copy code
{
  "level": 50,
  "time": 1765515482624,
  "pid": 347,
  "hostname": "ip-10-0-6-33.ap-south-1.compute.internal",
  "jobID": "AJ_cZxwNkUDfC5Q",
  "err": {
    "type": "APIConnectionError",
    "message": "failed to generate TTS completion after 4 attempts",
    "stack": "APIConnectionError: failed to generate TTS completion after 4 attempts\n at SynthesizeStream.mainTask (file:///app/node_modules/@livekit/agents/dist/tts/tts.js:73:19)\n at process.processTicksAndRejections (node:internal/process/task_queues:95:5)",
    "body": null,
    "retryable": false,
    "name": "APIConnectionError"
  },
  "msg": "failed to generate TTS completion after 4 attempts"
}
t
Thanks for reaching out, Soumyadeep. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — I’m looking at the TTS error you pasted ("failed to generate TTS completion after 4 attempts"). I’ll help narrow this down and get you unstuck. Quick checklist and next steps (please tell me which items you’ve already checked): 1. Which integration are you using? ◦ LiveKit Inference (no plugin) or the Cartesia plugin (
@livekit/agents-plugin-cartesia
)? 1. If using the Cartesia plugin: ◦ Confirm
CARTESIA_API_KEY
is set on the host where the agent runs. 1. Network & connectivity ◦ From the agent host, can you reach Cartesia API (e.g., resolve/connect to api.cartesia.ai)? Any firewalls/egress rules or proxies in front of the agent? 1. Voice / descriptor validity ◦ Are you using a built-in Cartesia voice ID? If using the Inference descriptor, is it formatted like
cartesia/sonic-3:voice_id
? 1. Agent resource/host health ◦ Any CPU/memory spikes or OOMs around the timestamps of the error? Provide short logs (30–60s) before and after one error occurrence if possible. 1. Frequency & pattern ◦ Does the error happen for all TTS requests or intermittently for specific prompts/voices/rooms? If you’d like, share: • The exact TTS config snippet you use (mask any secrets). • A small log excerpt (timestamps + a few lines before/after the error). What I can do next once you reply: • If it’s an auth issue, I’ll list exact env vars/locations to check. • If it’s networking, I’ll suggest concrete tests you can run from the host and what to look for. • If retries/fallbacks are needed, I can show how to add a
FallbackAdapter
or alternative TTS flow. Which integration are you using (Inference or plugin), and can you paste the TTS config (with secrets removed)? Sources: How to detect when an agent has finished speaking | Cartesia TTS | LiveKit docs | Cartesia TTS plugin guide | LiveKit docs | Models | LiveKit Docs | Events and error handling | LiveKit docs
m
1. I am using livekit inference 2. - 3. no idea about this 4. Yes 5. Memory increased from 65 to 73 and then it plateaued 6. No it happened only when an LLM tool was executed
t
Thanks — this is helpful. Since you’re using LiveKit Inference, the most likely causes are temporary network/API connectivity when the LLM tool runs or resource contention when that tool executes. Let’s narrow it down with a few focused checks. Please run these from the agent host and share results or short log excerpts you see around the error time (mask secrets): 1) Network reachability (basic) • ping api.cartesia.ai (or run a DNS lookup) • curl -v https://api.cartesia.ai/health or
GET /
if available 2) Network connectivity under load • While you trigger the LLM tool (reproduce the failure), run a continuous ping or traceroute to api.cartesia.ai and note packet loss or spikes in latency. 3) Capture a short log window • Collect 30–60s of logs around a failure (include timestamps). Include any lines about LLM tool execution start/end and TTS start attempts. 4) Resource checks during failure • Run
top
or
htop
while reproducing and note CPU, memory, and load averages. • Check for file descriptor exhaustion:
ulimit -n
and
lsof | wc -l
. 5) TTS descriptor confirmation • Confirm the exact descriptor used in inference (format should be
cartesia/sonic-3:voice_id
). Paste the line (mask voice id if you want). 6) Frequency detail • Does the failure always coincide with running that specific LLM tool? If yes, which tool or external API does the LLM call (briefly)? If you prefer, paste: • The TTS descriptor line and a 30–60s log snippet (timestamps + lines before/after). Once I see the network test outputs and logs, I’ll point to the most likely root cause and exact next steps (auth/network fix, resource tuning, or adding a fallback TTS).
m
also when I end a call with my voice agent (via SIP) why is the memory not freed?? it increases and gets plateaued after each call! and then I get OOM errors
t
Thanks — I hear you. You’re seeing two related problems: intermittent Cartesia TTS APIConnectionErrors when the LLM tool runs, and memory that grows after SIP calls end and eventually causes OOMs. Let’s focus on the memory cleanup first (since OOMs are urgent) and tie it to the TTS problem after. What I’m responding to: why memory isn’t freed after ending a SIP call and how to stop the plateau/OOMs. Immediate checks and actions (please run and share results where noted): 1) How the call is being ended • Do you end calls by calling the DeleteRoom API, or by having participants leave / sending a SIP BYE? Confirm which method you use. 2) Ensure agent shutdown lifecycle is invoked • If your agent runs per-call, call
AgentSession.shutdown(drain=True)
or
session.aclose()
after the call ends so the agent drains audio and closes I/O. If you rely only on participants leaving, the agent process may remain alive. 3) Confirm room deletion/disconnect • After call end, verify the room no longer exists in the LiveKit server. If rooms remain, resources stay allocated. 4) Monitor which process grows • Check which process holds memory after call end: run
ps aux --sort=-rss | head -n 10
or use `top`/`htop`. Tell me whether memory growth is in: ◦ the agent process, ◦ the LiveKit server process, ◦ or another helper/service (SIP gateway, media gateway). 5) Capture a short log window around call end • Provide 30–60s of logs spanning call termination (include SIP BYE, room close, agent shutdown events). Look for
shutdown
,
close
, or error events. 6) Resource metrics to collect • Before/during/after a call reproduce: memory RSS, process count, open file descriptors (
lsof | wc -l
), and
ulimit -n
. 7) Check for unclosed handles in your agent • Ensure streams, sockets, file descriptors, and timers started by the agent or your tool are explicitly closed when the call ends. If you use custom LLM tool code, confirm it closes HTTP connections/streams and doesn’t keep references to audio buffers. 8) Short-term mitigation • If you cannot immediately find the leak, restart the agent process after call end as a temporary guardrail to free memory. If you can answer these now it’ll narrow the root cause: • Which method do you use to terminate calls (DeleteRoom, SIP BYE, participants leaving)? • Which process shows the memory increase? • Attach the TTS descriptor line and a 30–60s log snippet (mask secrets) covering call end + the OOM or the next plateau. Next steps I’ll take once you share that info: • If the agent process holds memory, I’ll walk you through checking for unclosed streams and the exact shutdown calls to add. • If LiveKit server or SIP gateway is the culprit, I’ll guide you on room deletion checks and gateway cleanup steps. • I’ll also show how to correlate the LLM tool execution with the TTS APIConnectionError if they occur together. Would you like to start by telling me which termination method you use and which process shows memory growth? Sources: Job lifecycle | LiveKit docs | Events and error handling | LiveKit docs