is it a correct import `from livekit.agents.llm im...
# ask-ai
h
is it a correct import
from livekit.agents.llm import FallbackAdapter
can you take me to a valid example
t
Thanks for reaching out, aditi. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You're on the right track with the import `from livekit.agents.llm import FallbackAdapter`—it’s indeed the correct way to bring in the
FallbackAdapter
, which is a useful part of the LiveKit Agents framework. Here’s a friendly example to help you see how it works:
Copy code
from livekit.agents.llm import FallbackAdapter
from livekit.plugins import openai, anthropic

# Initialize primary and backup LLMs
primary_llm = openai.LLM(model="gpt-4o")
backup_llm = anthropic.LLM(model="claude-3-opus-20240307")

# Create the FallbackAdapter with automatic failover
fallback_llm = FallbackAdapter(
    llm=[primary_llm, backup_llm],      # Ordered list: primary first, then fallbacks
    attempt_timeout=10.0,               # Timeout per request attempt
    max_retry_per_llm=1,                # Retry once per LLM before failing over
    retry_interval=0.5,                 # Delay between retry attempts
    retry_on_chunk_sent=False,          # Do not retry if partial response was already sent
)

# Use the fallback-enabled LLM in a chat context
stream = fallback_llm.chat(chat_ctx=my_chat_context, tools=my_tools)
This configuration helps by automatically switching between LLMs if needed, monitoring their health, and recovering smoothly. If you have any questions or want to explore this further, just let me know—I’m here to help! Sources: livekit-agents/livekit/agents/tts/__init__.py | Events and error handling | LiveKit docs