Is it possible to use fallbacks to prevent 'failed...
# ask-ai
c
Is it possible to use fallbacks to prevent 'failed to generate LLM completion' errors?
t
Thanks for reaching out, Matin. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
c
Copy code
{
  "type": "log",
  "level": "warning",
  "logger": "livekit.agents",
  "details": {
    "file": "llm.py",
    "line": 208,
    "level": "warning",
    "logger": "livekit.agents",
    "message": "failed to generate LLM completion, retrying in 0.1s",
    "function": "_main_task",
    "traceback": "Traceback (most recent call last):\n  File \"/usr/local/lib/python3.11/site-packages/livekit/agents/llm/llm.py\", line 190, in _main_task\n    return await self._run()\n           ^^^^^^^^^^^^^^^^^\n  File \"/usr/local/lib/python3.11/site-packages/livekit/agents/inference/llm.py\", line 339, in _run\n    raise APITimeoutError(retryable=retryable) from None\nlivekit.agents._exceptions.APITimeoutError: Request timed out. (body=None, retryable=True)\n"
  },
  "message": "failed to generate LLM completion, retrying in 0.1s",
  "timestamp": "2025-10-03T02:16:00.270504+00:00",
  "traceback": "Traceback (most recent call last):\n  File \"/usr/local/lib/python3.11/site-packages/livekit/agents/llm/llm.py\", line 190, in _main_task\n    return await self._run()\n           ^^^^^^^^^^^^^^^^^\n  File \"/usr/local/lib/python3.11/site-packages/livekit/agents/inference/llm.py\", line 339, in _run\n    raise APITimeoutError(retryable=retryable) from None\nlivekit.agents._exceptions.APITimeoutError: Request timed out. (body=None, retryable=True)\n"
}
t
Absolutely! You can use the
FallbackAdapter
to gracefully handle those 'failed to generate LLM completion' errors by automatically switching to backup LLM providers if the primary one runs into trouble. This handy feature is available in Python for LLM, STT, and TTS components. Here’s a simple example to help you get started:
Copy code
from livekit.agents import llm, stt, tts
from livekit.plugins import assemblyai, deepgram, elevenlabs, openai, groq

session = AgentSession(
    stt=stt.FallbackAdapter(
        [
            assemblyai.STT(),
            deepgram.STT(),
        ]
    ),
    llm=llm.FallbackAdapter(
        [
            openai.LLM(model="gpt-4o"),
            openai.LLM.with_azure(model="gpt-4o", azure_endpoint="...", api_key="..."),
        ]
    ),
    tts=tts.FallbackAdapter(
        [
            elevenlabs.TTS(),
            groq.TTS(),
        ]
    ),
)
You also have the flexibility to tweak settings like
attempt_timeout
,
max_retry_per_llm
, and
retry_interval
to fine-tune how it behaves. Just a heads-up: if all the configured LLMs fail, an
APIConnectionError
will be raised after it finishes trying all options. Please feel free to reach out if you'd like any help setting this up or if you have more questions—I’m here to support you! Sources: Events and error handling | LiveKit Docs | Docs::Agents | livekit-agents/livekit/agents/llm/llm.py
It looks like the error log shows that the LLM request timed out and is currently retrying. To help avoid this kind of issue in the future, you might find the
FallbackAdapter
really useful. It lets you set up automatic failover to backup LLM providers, so if one provider runs into trouble, the system smoothly switches to another one from your list. Here’s a friendly example of how you can set up the
FallbackAdapter
in Python:
Copy code
from livekit.agents import llm
from livekit.plugins import openai

session = AgentSession(
    llm=llm.FallbackAdapter(
        [
            openai.LLM(model="gpt-4o"),
            openai.LLM.with_azure(model="gpt-4o", azure_endpoint="...", api_key="..."),
        ]
    )
)
Plus, you have the flexibility to customize settings like
attempt_timeout
,
max_retry_per_llm
, and
retry_interval
to fine-tune how retries happen. If you'd like, I’m here to walk you through configuring these parameters or to help with handling specific errors. Just let me know!
c
what is the difference between the attempt_timeout and the retry_interval?
t
I'm happy to help explain the
attempt_timeout
and
retry_interval
parameters in the `FallbackAdapter`—they each play an important role: • attempt_timeout: This sets the maximum time (in seconds) allowed for one attempt to connect with a service (like LLM, STT, or TTS). If it takes longer than this, the attempt is considered unsuccessful, and the adapter will move on to the next provider available. • retry_interval: This defines the waiting time (in seconds) between retry attempts. It adds a little pause before trying the same service again, which is really helpful when checking if a previously failed service has recovered or is available again. So, to sum it up:
attempt_timeout
is about how long to wait for each individual attempt, and
retry_interval
is the break you take between those attempts. Please feel free to reach out if you'd like me to explain anything further or if you need a hand with anything else! Sources: tests/test_tts_fallback.py