Overview
In realtime voice conversations, a model API failure can leave the agent unable to continue. Fallback strategies let you define backup providers that automatically take over when the primary provider fails.
Choose a fallback mechanism based on where your models come from:
| Mechanism | Use it when | Model types | Where fallback runs | SDKs |
|---|---|---|---|---|
| LiveKit Inference fallback | Your models come from LiveKit Inference. | STT, TTS | LiveKit Inference | Python, Node.js |
| Fallback adapter | You use plugins, or you need LLM fallback. | STT, TTS, LLM | Your agent process | Python, Node.js |
| Realtime model fallback adapter | You use a realtime model. | Realtime | Your agent process | Python |
You can combine mechanisms in one session. For example, use LiveKit Inference fallback for STT and TTS, and a fallback adapter for a plugin LLM.
A successful fallback doesn't raise an AgentSession error event. The session emits an error event only when no provider can serve the request. In Node.js, a TTS instance that fails after it starts sending audio also causes an error event. To learn more, see TTS behavior.
LiveKit Inference fallback
If you use LiveKit Inference, you can configure fallback models directly with the fallback parameter on inference.STT and inference.TTS. Fallback logic runs server-side in the LiveKit Inference service, so your agent code doesn't need to manage retries or health checks.
from livekit.agents import AgentSession, inferencesession = AgentSession(stt=inference.STT(model="deepgram/nova-3",language="en",fallback=[{"model": "assemblyai/universal-streaming"},],),tts=inference.TTS(model="fishaudio/s3-pro",voice="fa4c9eb3dccc4806b382b40d61c6b10a",language="en",fallback=[{"model": "cartesia/sonic-3","voice": "9626c31c-bec5-4cca-baa8-f8ba9e84c8bc",},],),# ... llm, etc.)
import { inference, voice } from '@livekit/agents';const session = new voice.AgentSession({stt: new inference.STT({model: 'deepgram/nova-3',language: 'en',fallback: [{ model: 'assemblyai/universal-streaming' }],}),tts: new inference.TTS({model: 'fishaudio/s3-pro',voice: 'fa4c9eb3dccc4806b382b40d61c6b10a',language: 'en',fallback: [{model: 'cartesia/sonic-3',voice: '9626c31c-bec5-4cca-baa8-f8ba9e84c8bc',},],}),// ... llm, etc.});
The model in the top-level parameter is the primary. LiveKit Inference tries models in the following order:
- The primary model.
- For a custom voice, the same voice on the other providers where the clone is ready.
- The models in
fallback, in the order you list them.
Fallback entries
The fallback parameter accepts a single entry or a list. Each entry takes one of the following forms:
- String: a model ID, such as
"deepgram/nova-3". For TTS, add a voice after a colon:"provider/model:voice". A TTS string without a voice uses the model's default voice. - Object: a
modelfield, plusvoicefor TTS. To pass provider-specific options to a fallback model, addextra_kwargs(Python) orextraKwargs(Node.js).
tts = inference.TTS(model="fishaudio/s3-pro",voice="fa4c9eb3dccc4806b382b40d61c6b10a",fallback=[# String form"rime/coda:celeste",# Object form with provider-specific options{"model": "cartesia/sonic-3","voice": "9626c31c-bec5-4cca-baa8-f8ba9e84c8bc","extra_kwargs": {"speed": "fast"},},],)
const tts = new inference.TTS({model: 'fishaudio/s3-pro',voice: 'fa4c9eb3dccc4806b382b40d61c6b10a',fallback: [// String form'rime/coda:celeste',// Object form with provider-specific options{model: 'cartesia/sonic-3',voice: '9626c31c-bec5-4cca-baa8-f8ba9e84c8bc',extraKwargs: { speed: 'fast' },},],});
Behavior
LiveKit Inference tries the next model in the chain when the current model fails. It stops when a model serves the request, every model has failed, or the client disconnects.
TTS
LiveKit Inference moves to the next TTS model when the current model fails, returns an error, or stops producing audio. It doesn't fall back when your agent cancels the request or the call ends. LiveKit Inference skips a fallback model that doesn't support your model options or the session language.
If a model fails after it starts sending audio, LiveKit Inference replays the whole turn from the start on the next model. Audio that already reached the listener can't be recalled, so the listener might hear the start of the utterance twice, the second time in the fallback model's voice. Very long turns can't be replayed. If the model fails during one, the turn ends with an error.
LiveKit Inference doesn't probe failed models in the background. After a model fails, it stays out of the chain until the chain starts again from the primary model. The chain starts again in the following cases:
- A turn ends with an error, for example after every model fails.
- The voice, model, language, or model options change.
- The agent reconnects.
LiveKit Inference also skips a model that stalls for a short time, unless no other model is available.
Every model in the chain uses the sample rate and encoding of the session. A model in fallback never inherits the primary model's voice. If a fallback entry has no voice, LiveKit Inference uses that model's default voice.
LiveKit Inference bills each turn once, to the model that serves it, even when the turn is replayed on a fallback model. A fallback model can have a different rate than your primary model.
To know when a fallback model serves a turn, listen for the fallback_activated event on inference.TTS. The event includes the provider, model, and voice that now serve the turn, and the reason for the switch. To learn more, see Fallback events.
Fallback adapter
The fallback adapter runs fallback logic directly in your agent process using plugins. Use it when you need LLM fallback support, or when you're connecting to providers that aren't available through LiveKit Inference.
from livekit.agents import AgentSession, llm, stt, ttsfrom livekit.plugins import assemblyai, cartesia, deepgram, inworld, openaisession = AgentSession(stt=stt.FallbackAdapter([deepgram.STT(),assemblyai.STT(),]),llm=llm.FallbackAdapter([openai.responses.LLM(model="gpt-4o"),openai.LLM.with_azure(model="gpt-4o", ...),]),tts=tts.FallbackAdapter([cartesia.TTS(...),inworld.TTS(...),]),)
import { llm, stt, tts, voice } from '@livekit/agents';import * as assemblyai from '@livekit/agents-plugin-assemblyai';import * as cartesia from '@livekit/agents-plugin-cartesia';import * as deepgram from '@livekit/agents-plugin-deepgram';import * as inworld from '@livekit/agents-plugin-inworld';import * as openai from '@livekit/agents-plugin-openai';const session = new voice.AgentSession({stt: new stt.FallbackAdapter({sttInstances: [new deepgram.STT(), new assemblyai.STT()],}),llm: new llm.FallbackAdapter({llms: [new openai.LLM({ model: 'gpt-4o' }),openai.LLM.withAzure({ model: 'gpt-4o' }),],}),tts: new tts.FallbackAdapter({ttsInstances: [new cartesia.TTS(), new inworld.TTS()],}),});
The first instance in each list is the primary. Subsequent instances are tried in order if it fails.
Behavior
The fallback adapter switches providers when a request to a provider fails after its retries. This includes connection failures, timeouts, and HTTP errors. The adapter applies partial output guards to avoid disrupting output that the user has already started receiving:
- STT: no partial output guard. The adapter switches to the next provider on any error.
- TTS: if audio has already been pushed to the speaker, the adapter doesn't switch to a backup provider mid-utterance, because the backup would repeat speech the user already heard. The utterance ends early. What happens next depends on the SDK, as described in TTS behavior.
- LLM: if text or tool calls have already been streamed to the user, the adapter raises the error rather than restarting the response with a different model. Set
retry_on_chunk_sent=Trueonllm.FallbackAdapterto override this and allow mid-stream fallback.
By default, the adapter uses the first available LLM in the list. If that LLM fails, the adapter switches to the next LLM and probes the failed LLM in the background. When the failed LLM recovers, it becomes the primary LLM again.
Use sticky mode to stay on the current LLM until it fails, even after a higher-priority LLM recovers. This prevents repeated switches when a provider is intermittently available. Set sticky=True (Python only):
llm.FallbackAdapter([openai.responses.LLM(model="gpt-4o"),openai.LLM.with_azure(model="gpt-4o", ...),],sticky=True,)
TTS behavior
The TTS adapter retries each instance before it moves to the next one. To change the number of retries, set max_retry_per_tts (Python) or maxRetryPerTTS (Node.js). The default is 2.
In both SDKs, any error from an instance before it sends audio makes the adapter move to the next instance. The Python and Node.js adapters differ in the following ways:
| Behavior | Python | Node.js |
|---|---|---|
| Failure after audio starts | Marks the instance as unavailable, logs a warning, and ends the utterance early without raising an error. | Raises a non-retryable APIConnectionError, which AgentSession reports as an error event. The instance stays available. |
| Recovery probes | When a request skips an unavailable instance or fails on it before audio starts, the adapter synthesizes that request's text on the instance in the background. It probes once per request. | The adapter synthesizes a short fixed phrase on the failed instance every recoveryDelayMs milliseconds until it succeeds. The default is 1000. |
| Output sample rate | The value of sample_rate, if you set it. Otherwise, the highest sample rate among the instances. | The highest sample rate among the instances. |
If an instance uses a different sample rate, the adapter converts its audio to the output sample rate. All instances must have the same number of channels. Recovery probes are real synthesis requests, so your TTS provider bills for them.
When every instance is unavailable, the adapter tries all of them again, starting with the primary. If they all fail, AgentSession emits an error event.
Availability events
The fallback adapter emits an availability event when it marks a provider as unavailable and again when the provider recovers. The event name depends on the adapter: stt_availability_changed, llm_availability_changed, or tts_availability_changed. The adapter emits these events on itself, not on AgentSession. Register the listener on the adapter instance.
The event has two fields: the affected instance (stt, llm, or tts) and available, which is false after a failure and true after recovery.
import loggingfrom livekit.agents import AgentSession, ttsfrom livekit.plugins import cartesia, inworldlogger = logging.getLogger("agent")fallback_tts = tts.FallbackAdapter([cartesia.TTS(...), inworld.TTS(...)])@fallback_tts.on("tts_availability_changed")def on_tts_availability_changed(ev: tts.AvailabilityChangedEvent):state = "available" if ev.available else "unavailable"logger.warning(f"{ev.tts.label} is {state}")session = AgentSession(tts=fallback_tts,# ... stt, llm, etc.)
import { log, tts, voice } from '@livekit/agents';import * as cartesia from '@livekit/agents-plugin-cartesia';import * as inworld from '@livekit/agents-plugin-inworld';import type { EventEmitter } from 'node:events';// Inside your entry functionconst fallbackTts = new tts.FallbackAdapter({ttsInstances: [new cartesia.TTS(), new inworld.TTS()],});// The TTS event types don't include availability events, so listen through EventEmitterconst emitter: EventEmitter = fallbackTts;emitter.on('tts_availability_changed', (ev: tts.AvailabilityChangedEvent) => {const state = ev.available ? 'available' : 'unavailable';log().warn(`${ev.tts.label} is ${state}`);});const session = new voice.AgentSession({tts: fallbackTts,// ... stt, llm, etc.});
Realtime model fallback adapter
The realtime model fallback adapter switches between realtime models when the active model fails. The chat context carries over to the new model, so the agent picks up the conversation where it left off.
from livekit.agents import AgentSession, llmfrom livekit.plugins import google, openairealtime_llm = llm.RealtimeModelFallbackAdapter([openai.realtime.RealtimeModel(),openai.realtime.RealtimeModel.with_azure(azure_deployment="gpt-realtime"),google.realtime.RealtimeModel(),])session = AgentSession(llm=realtime_llm)
The first model in the list is the primary. Subsequent models are tried in order if it fails.
Behavior
The realtime model fallback adapter switches models only on errors that the model can't recover from. For recoverable errors, the model's plugin reconnects on its own.
When the adapter switches models, it does the following:
- Interrupts the agent and saves what the user already heard to the chat context.
- Starts the next available model and loads the chat context onto it.
- Regenerates the interrupted reply on the new model. Set
regenerate_on_swap=Falseto skip this step. - Drops any user audio that arrives during the switch.
A failed model is unavailable for 10 seconds, after which the adapter can choose it again. Set cooldown to change this duration in seconds. AgentSession receives an unrecoverable error only when no model is left to try.
All models in the list must use the same settings for audio output and server-side turn detection. If they don't match, the adapter raises a ValueError when you create it. Full-duplex models aren't supported.
When a model fails or becomes available again, the adapter emits a realtime_availability_changed event so you can observe the change from your agent code.
Restart the session
You can restart the realtime model session without waiting for an error. The chat context carries over to the new session.
# Restart the current model with a fresh sessionawait realtime_llm.restart_session()# Or switch to the next available modelawait realtime_llm.restart_session(switch_model=True)