How to Add LLM Failover Without Sharing API Keys
Many teams want better uptime from multiple LLM providers, but do not want to move API keys into a third-party relay. Here is a practical way to design failover while keeping provider accounts, keys, and billing under your control.
Why failover matters for LLM applications
In production, LLM failures rarely look dramatic. More often, they show up as:
- HTTP 429 rate limits
- timeouts
- temporary host errors
- answers that technically return but fail your app’s validation
If your application depends on the result, these are not minor inconveniences. They can create stuck jobs, broken automations, and hidden support load.
Failover is the practice of trying another provider or endpoint when the first attempt does not produce an acceptable result.
Why some teams avoid centralized LLM relays
A common design is to send every request through a gateway, proxy, or orchestration layer. That can be a good fit for some organizations, especially those that want one central control point.
But other teams prefer not to put provider API keys into a shared third-party system. Reasons may include internal security policy, customer architecture, billing clarity, or simply wanting provider invoices to remain directly with the provider account owner.
If that is your situation, a client-side failover pattern is worth considering.
Client-side failover: the basic pattern
In a client-side approach, the logic that selects and retries providers runs inside your own application or worker. Your app still calls providers directly using your own keys.
A minimal flow looks like this:
- Try provider A
- If it times out, returns 429, errors, or fails validation, record the outcome
- Back off briefly
- Retry on provider B
- If all fail, return a result your app can handle explicitly instead of silently dropping the job
This gives you resilience without changing who owns provider accounts or where billing happens.
What should trigger failover?
Most teams start with infrastructure failures only. That is useful, but incomplete. You should usually consider failover when any of these happen:
- HTTP 429: provider is rate-limiting
- timeout: no response in acceptable time
- transport or server error: request could not complete
- validation failure: the response fails your acceptance check
The last point matters because a technically successful response can still be unusable for your application.
Define “accepted answer” early
Before building failover, decide how your app knows an answer is acceptable. Examples:
- response parses as JSON
- required keys exist in the output
- classification is one of approved labels
- tool output matches expected structure
- internal rules say the answer is usable
Without this, failover decisions are weaker because your app can only react to hard errors, not low-quality or invalid outputs.
A practical Python sketch
def run_with_failover(prompt):
providers = [call_provider_a, call_provider_b]
for provider in providers:
try:
text, cost = provider(prompt)
if is_valid_json(text):
return {
"status": "ok",
"provider": provider.__name__,
"cost": cost,
"text": text,
}
except TooManyRequests:
pass
except TimeoutError:
pass
return {
"status": "degraded",
"provider": None,
"cost": None,
"text": None,
}This is a fine starting point. But as traffic grows, teams usually need better ranking, telemetry, and backoff logic.
What production-ready failover often adds
- provider ranking based on recent outcomes
- latency tracking
- cost tracking
- per-provider cooldown after repeated 429s
- an explicit fallback result your application can handle
- visibility into accepted vs rejected outputs
These additions help answer two critical questions: Did we preserve reliability? and What did that reliability cost?
Build vs use a tool
Build your own
Good option if failover is strategic and you have engineering time. You keep full control, but you also own edge cases and maintenance.
Use a framework or orchestration stack
General LLM frameworks can help organize prompts, chains, and model access. Depending on your stack, they may or may not give you the exact cost-per-accepted-answer routing logic you want.
Use a small client-side router
If your main need is provider selection and failover inside your own app, ComputeRouter is one option to review. It is a Python SDK that runs in the client app, keeps customer API keys with the customer, and supports failover. With a BYOK approach, requests use your own provider accounts and keys rather than a shared key pool.
What to log during rollout
| Field | Purpose |
|---|---|
| provider | See which host handled each request |
| latency | Understand speed trade-offs |
| status | Track success, timeout, 429, error, degraded |
| accepted | Separate valid outputs from unusable ones |
| cost | Measure cost per accepted answer |
Even basic logs make post-rollout decisions much easier.
When a client-side approach is not ideal
This pattern is not the best fit for every team. It may be less suitable if:
- you need centralized policy enforcement across many apps
- you want one network hop for all observability and access control
- your architecture already standardizes on an internal gateway
- you use many provider-specific features that are hard to normalize
In those cases, a centralized internal service may still be the right answer.
How to evaluate results honestly
Do not judge failover only by whether errors went down. Also ask:
- Did accepted-answer rate stay stable?
- Did cost per accepted answer improve or worsen?
- How often did failover trigger?
- Did fallback results become easier to handle operationally?
Reliability that doubles your cost may or may not be acceptable. The right answer depends on your workload.
Where ComputeRouter may fit
If you want to test this pattern without handing over provider keys, the free beta for ComputeRouter may be useful. It offers a small Python SDK plus a cloud panel, with BYOK and no key sharing. The beta is free for 20 teams for 2 months. In six live test runs on one workload, the team reported 0 lost jobs and about 47% lower cost per gold-accepted answer than one provider alone. That is a measured result for that test setup, not a guarantee for yours.
Final thought
Failover is easiest to justify when LLM output sits inside an important business workflow. If you want reliability gains without moving keys into a shared relay, client-side failover is a practical architecture to consider.
FAQ
- Does failover always require using different models?
- No. A common pattern is to use the same model from multiple providers and switch hosts when one is unavailable or less effective.
- Can I do failover without a cloud gateway?
- Yes. You can implement the logic in your own application or use a client-side SDK that runs inside your app.
- What should happen if every provider fails?
- Return a fallback result that your app can handle explicitly. Avoid silent drops, because they are hard to detect and debug.