Retries & Fallbacks
The TensorZero Gateway offers multiple strategies to handle errors and improve reliability.
These strategies are defined at three levels: models (model provider routing), variants (variant retries), and functions (variant fallbacks). You can combine these strategies to define complex fallback behavior.
Model Provider Routing
We can specify that a model is available on multiple providers using its routing
field.
If we include multiple providers on the list, the gateway will try each one sequentially until one succeeds or all fail.
In the example below, the gateway will first try OpenAI, and if that fails, it will try Azure.
Variant Retries
We can add a retries
field to a variant to specify the number of times to retry that variant if it fails.
The retry strategy is a truncated exponential backoff with jitter.
In the example below, the gateway will retry the variant four times (i.e. a total of five attempts), with a maximum delay of 10 seconds between retries.
Variant Fallbacks
If we specify multiple variants for a function, the gateway will try different variants until one succeeds or all fail. After each attempt, the gateway will re-sample an unused variant with a probability proportional to its weight (i.e. sampling without replacement).
In the example below, the gateway will first sample and attempt the variants with non-zero weights (GPT-4o Mini or Claude 3.5 Haiku). If all of those variants fail, the gateway will sample and attempt the variants with zero weights (Gemini 1.5 Flash 8B or Grok 2).
Combining Strategies
We can combine strategies to define complex fallback behavior.
The gateway will try the following strategies in order:
- Model Provider Routing
- Variant Retries
- Variant Fallbacks
In other words, the gateway will follow a strategy like the pseudocode below.
Load Balancing
TensorZero doesn’t currently offer an explicit strategy for load balancing API keys, but you can achieve a similar effect by defining multiple variants with appropriate weights. We plan to add a streamlined load balancing strategy in the future.
In the example below, the gateway will split the traffic between two variants (gpt_4o_mini_api_key_A
and gpt_4o_mini_api_key_B
).
Each variant leverages a model with providers that use different API keys (OPENAI_API_KEY_A
and OPENAI_API_KEY_B
).
See Configuration Reference for more details on credential management.
Technical Notes
- For variant types that require multiple model inferences (e.g. best-of-N sampling), the
routing
fallback applies to each individual model inference separately.