Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Heuristic:ALwrity ALwrity Provider Fallback Chain

From Leeroopedia
Knowledge Sources
Domains LLMs, Optimization, Routing
Last Updated 2026-10-24 00:00 GMT

Overview

When a preferred LLM provider or model is unavailable, fall back through a minimal, ordered chain of alternative providers/models rather than exhausting all options or failing immediately.

Description

The `llm_text_gen` function and its provider implementations (HuggingFace, WaveSpeed, Gemini) use a fallback chain pattern for resilience. When the primary provider's API key is unavailable, the function checks which providers have keys configured and falls back to the first available one. When a specific model is not found on HuggingFace, the implementation tries a list of fallback models in order.

The key design choice is minimal fallback: the premium path uses only a small, safe fallback chain (`PREMIUM_HF_MINIMAL_FALLBACK_MODELS = ["openai/gpt-oss-120b:groq"]`) to avoid excessive model hopping, which wastes time and produces inconsistent outputs. Callers can supply their own model policy (`preferred_hf_models`) or an explicit empty list (no fallback, only the requested model).

Usage

Apply this heuristic when:

  • A primary LLM provider may be unavailable (missing key, rate limit, model not found)
  • You want to avoid hard failures while limiting the blast radius of fallback
  • Model consistency matters — too many fallback hops produce inconsistent quality

The Insight (Rule of Thumb)

  • Action: Define an ordered fallback chain of providers/models. Fall back to the first available provider with a configured key. For model-not-found, try a minimal list of safe fallback models.
  • Value: Provider fallback: `google` → `huggingface` (first available). Model fallback: `PREMIUM_HF_MINIMAL_FALLBACK_MODELS = ["openai/gpt-oss-120b:groq"]`. Caller policy: `preferred_hf_models` overrides; empty list = no fallback.
  • Trade-off: Minimal fallback may fail if the fallback model is also unavailable. Aggressive fallback (many models) wastes time and produces inconsistent output.
  • Compatibility: Works with per-tenant provider config; `tenant_provider_config_resolver` determines the primary provider per user.

Reasoning

In a multi-tenant system, different users may have different API keys configured. The fallback chain must be key-aware: falling back to a provider whose key is not set for this tenant is pointless. The function checks `get_api_key(provider, user_id)` for each candidate before selecting.

For model-level fallback (within HuggingFace), the trade-off is between resilience and consistency. Each fallback model has different capabilities, latency, and output style. A long fallback chain means the user might get a response from a much weaker model without knowing it. The minimal chain (`PREMIUM_HF_MINIMAL_FALLBACK_MODELS` with one entry) balances resilience with predictability: at most one hop before failure.

The pattern is explicit in the code: if `preferred_hf_models` is `None` (not provided), use the minimal safe chain. If it's an empty list `[]`, the caller explicitly wants no fallback (only the requested model). If it's a non-empty list, use the first as primary and the rest as fallback.

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment