thousands-hamburger-60747
10/30/2025, 6:06 AMtall-airline-16756
10/30/2025, 6:06 AMtall-airline-16756
10/30/2025, 6:07 AMthousands-hamburger-60747
10/30/2025, 6:07 AMtall-airline-16756
10/30/2025, 6:08 AMfrom livekit.agents.llm import FallbackAdapter
# Define primary and secondary services
primary_llm = ... # Your primary LLM service
secondary_llm = ... # Your secondary LLM service
# Create the fallback adapter
llm_fallback = FallbackAdapter(
llm=[primary_llm, secondary_llm],
attempt_timeout=5.0, # Timeout for each attempt
max_retry_per_llm=0, # No retries, fallback immediately
retry_interval=0.5, # Interval between retries
retry_on_chunk_sent=False # Do not retry after chunks are sent
)
3. Monitor Service Availability:
It’s a good idea to keep an eye on service availability. You can do this by handling events like so:
# Monitor availability changes
def on_availability_changed(event):
print(f"LLM {event.llm.label} availability: {event.available}")
llm_fallback.on("llm_availability_changed", on_availability_changed)
4. Handle Recovery:
One of the great things is that the adapter automatically monitors and recovers any failed services for you. So, you don’t need to worry about manually resetting the fallback state during your session.
5. Use the Adapter in Your Application:
Finally, integrate the fallback adapter into your application logic to ensure all requests are routed through it smoothly. For example:
# Use the fallback adapter for generating responses
response = llm_fallback.generate(prompt="Your input prompt")
If you’d like, I’m here to help with more examples or guidance, especially for STT or TTS services. Just let me know!thousands-hamburger-60747
10/30/2025, 6:09 AMtall-airline-16756
10/30/2025, 6:10 AM