B2BB2B LLM
Ettevõtluse ülevaade

Kuidas kavandada LLM-i tagavarasid ilma latentsus-, kulu- või väljundlepinguid rikkumata

Praktiline arhitektuur LLM-i varumarsruutimiseks, mis eraldab korduskatsed varudest, klassifitseerib pakkuja vead, kaitseb struktureeritud väljundlepinguid, kontrollib kulusid ja annab meeskondadele piisavalt telemeetria, et tõestada töökindluse paranemist.

Fallback is not just a backup model list. For engineering teams using several LLM providers, fallback is a routing policy that decides when a request may move from one provider or model to another, what must stay invariant, and how the system proves the alternate path made the application more reliable.

The reader problem is usually simple: reduce failed requests and lower tail latency without silently changing output quality, schema validity, safety behavior, or cost. The solution is more specific than “try another provider.” A reliable LLM fallback strategy needs error classification, request deadlines, model compatibility rules, cost limits, circuit breakers, and observability at the provider and route level.

Start With A Fallback Policy, Not Retry Code

Retries and fallback solve different problems. A retry asks the same provider to complete the same request after a transient failure. A fallback asks a different model, provider, region, or route to complete the work instead. Mixing those concerns creates systems that are hard to reason about during incidents.

Fact: Reliability guidance from cloud infrastructure providers commonly recommends bounded exponential backoff, jitter, maximum retry limits, idempotency checks, and avoiding retries at multiple layers because retry storms can worsen overload. Major LLM providers also document transient errors, rate limits, overload responses, timeouts, and SDK retry behavior.

Recommendation: implement one retry layer for each request path. If the application gateway retries, downstream services should not also retry independently unless the retry ownership is explicit. Every retry should consume the same caller deadline, not create a new open-ended timeout.

A minimal policy should answer these questions before any code is written:

  • Which failures are retryable on the same provider?
  • Which failures allow fallback to another provider or model?
  • Which failures must never trigger fallback?
  • What output contract must remain identical?
  • What is the maximum allowed cost increase?
  • How much of the request deadline may be spent before fallback is disallowed?
  • Which telemetry proves whether fallback helped or hid a defect?

Classify Errors Before Routing Around Them

Fallback is dangerous when every non-200 response is treated the same. A provider timeout is not the same as a malformed request. A rate limit is not the same as a safety refusal. An output validation failure is not the same as a network failure.

A practical taxonomy should include at least these classes:

  • Retryable transport failure: connection reset, DNS issue, temporary network failure, or gateway timeout.
  • Provider overload: documented overload or server-side transient failure.
  • Rate limit: request, token, acceleration, or quota pacing limit, especially when a Retry-After value is provided.
  • Provider timeout: the provider accepted the request but did not complete within its service window.
  • Invalid request: malformed payload, unsupported parameter, invalid schema, or model-incompatible option.
  • Authentication or configuration error: bad key, disabled account, missing entitlement, or exhausted account-level quota.
  • Safety refusal or block: the provider intentionally declines or blocks content based on safety policy.
  • Output validation failure: the model responded, but the response does not satisfy the application contract.

Recommendation: retry only transient failures inside the caller’s latency budget. Allow fallback only for error classes that the product and platform teams have explicitly approved. Do not fall back on authentication errors, malformed requests, deterministic schema mistakes, or safety refusals unless the product defines a specific alternate safe behavior.

This distinction matters because fallback can otherwise hide bugs. If a schema is invalid for the primary provider and happens to be ignored by a fallback provider, the system may appear healthier while returning less constrained output. That is not reliability; it is contract drift.

Use Deadlines, Not Just Timeouts

Tail latency problems often come from retries that are technically bounded but practically too slow. A request with a 15-second user-facing budget should not spend 14 seconds failing on the primary route and then start a full fallback attempt that cannot complete in time.

Use a request deadline that is passed through the entire stack. Each routing decision should check remaining time before it retries or falls back. For example:

request_deadline_ms = 6000
primary_attempt_timeout_ms = 3500
retry_backoff_budget_ms = 500
minimum_fallback_budget_ms = 1800

if remaining_deadline_ms < minimum_fallback_budget_ms:
    return controlled_failure

The exact numbers depend on workload. A chat completion route may reserve enough time for one alternate model. A background extraction job may allow multiple attempts. A real-time autocomplete path may prefer a fast failure over a late fallback answer.

Recommendation: define latency budgets by route class, not globally. A low-latency UI route, a batch classification route, and a long-context analysis route should not share the same retry and fallback envelope.

Define Model Classes By Workload

Vendor names are a poor abstraction for fallback. The application cares about workload properties: context length, reasoning depth, tool support, structured output behavior, latency, data handling constraints, and cost. Fallback routes should be organized around those properties.

Useful model classes include:

  • Fast draft: short responses, low latency, moderate quality tolerance.
  • Structured extraction: strict JSON output, schema validation, low tolerance for format drift.
  • Reasoning: multi-step analysis, higher quality requirements, higher acceptable latency.
  • Long-context: large documents, high token windows, careful truncation rules.
  • Low-cost batch: asynchronous or non-urgent jobs optimized for unit economics.
  • Premium escalation: expensive but capable models used only when policy allows.

For each class, list allowed primary models, allowed fallback models, and forbidden downgrade paths. A long-context request should not fall back to a short-context model that silently truncates the prompt. A structured extraction task should not fall back to a model that cannot satisfy the schema constraints. A premium reasoning task may allow fallback to another premium model but not to a fast draft model unless the user experience explicitly supports a degraded answer.

For teams building an enterprise LLM API, these classes should live in configuration or policy data rather than scattered across application services. That keeps routing behavior auditable and easier to change during provider incidents.

Protect Structured Output Contracts

Structured output fallback is one of the easiest places to create subtle production bugs. Providers differ in how they support JSON mode, JSON Schema, tool calls, refusal fields, safety feedback, finish reasons, and schema subsets. A schema that works with one model is not guaranteed to be portable to another.

Fact: Some provider documentation describes strict schema adherence for supported models, while other documentation notes support for only a subset of JSON Schema. Provider responses may also include refusal fields, prompt feedback, safety ratings, finish reasons, or no candidates when a prompt is blocked or invalid.

Recommendation: treat output validation as part of the route, not an optional caller concern. The gateway should validate the final response before returning it to the application. If validation fails, the route should classify the failure and decide whether a fallback attempt is allowed.

There are two workable patterns:

Use A Portable Schema Subset

Keep schemas to the intersection of features supported across approved fallback providers. This improves portability and lowers operational complexity. The trade-off is that you may lose richer provider-specific constraints.

Use Provider-Specific Schema Adapters

Maintain adapters that translate the application contract into each provider’s supported schema format. This preserves capability but increases testing and maintenance burden. It also requires a compatibility matrix so engineers know which routes can fall back safely.

A basic compatibility record might look like this:

{
  "route": "invoice_extraction_v3",
  "contract": "invoice_json_schema_v3",
  "primary": "provider_a.structured_model",
  "fallbacks": ["provider_b.extraction_model"],
  "disallowed": ["provider_c.fast_model"],
  "requires": ["json_schema", "enum", "required_fields"],
  "validation": "strict_before_return"
}

Recommendation: never return fallback output just because it is well-formed text. Return it only after it satisfies the same application-level contract expected from the primary route.

Control Cost Before Incidents Happen

Fallback often shifts traffic to more expensive models at exactly the moment when traffic is elevated or a provider is degraded. Without cost controls, a reliability mechanism can become a spend incident.

Set fallback rules that include:

  • Maximum fallback price multiplier per route.
  • Allowed model tiers by organization, workspace, or environment.
  • Daily or hourly fallback spend budgets.
  • Separate policy for interactive and batch workloads.
  • Rules for whether fallback may cross provider, region, or model family boundaries.

Recommendation: measure cost per successful response, not only raw request cost. A more expensive fallback may be justified if it prevents a user-visible failure on a critical route. The same fallback may be unacceptable for low-priority batch work.

Cost policy should also be visible to the organizations using the platform. Teams may want to allow premium fallback for production customer support workflows while blocking it for development, testing, or internal experimentation. That is a governance decision, not just a routing decision.

Add Circuit Breakers Per Provider, Model, Region, And Error Class

Naive fallback can hammer a degraded provider. Every failed primary request triggers another primary attempt, then another fallback attempt, while other services do the same. Circuit breakers reduce repeated pressure and make routing behavior more predictable.

A circuit breaker should be specific enough to avoid overreacting. Do not mark an entire provider unhealthy because one model in one region is returning structured output failures. Track health by provider, model, region, route, and error class where possible.

A simple circuit breaker state machine is enough for many teams:

  • Closed: normal traffic flows to the route.
  • Open: route is temporarily avoided after error thresholds are exceeded.
  • Half-open: limited probe traffic tests recovery before full traffic resumes.

Thresholds should account for volume. Ten failures in a minute may be meaningful for a low-volume route but irrelevant for a route handling thousands of requests per minute. Use minimum sample sizes, rolling windows, and error-class filters.

Recommendation: circuit breakers should not only count HTTP failures. They should also track output validation failures, timeout rates, rate-limit rates, and refusal or block patterns separately. Those classes imply different operational responses.

Log The Routing Decision, Not Just The Final Error

Fallback that improves user experience can still make incidents harder to diagnose. If logs only show the final successful provider, engineers lose the evidence that the primary provider was failing.

Each request should record:

  • Original provider, model, and route.
  • Fallback provider, model, and route when used.
  • Error class that triggered retry or fallback.
  • Provider request IDs when available.
  • Retry count and backoff duration.
  • Deadline consumed before each attempt.
  • Output validation result.
  • Estimated cost for each attempt and final cost.
  • Whether the final answer came from primary or fallback.
  • Organization, workspace, environment, and application metadata where policy allows.

This telemetry supports both engineering and commercial governance. It also connects fallback behavior to AI API cost control when different teams, products, or customers have different reliability and budget policies.

Measure Whether Fallback Actually Helped

Raw success rate is not enough. A fallback strategy can increase final 200 responses while making p99 latency worse, doubling cost, or returning less valid structured output.

Measure at least these metrics by route:

  • Primary success rate.
  • Final success rate after retry and fallback.
  • p50, p95, and p99 latency before and after fallback.
  • Fallback invocation rate.
  • Cost per successful response.
  • Contract-valid response rate.
  • Safety refusal or block rate by provider.
  • Quality regression signals from application-specific evaluation.

Prediction: as more companies use multi-provider LLM infrastructure, fallback policies will become more like traffic engineering and less like ad hoc exception handling. Teams will expect policy controls by workspace, environment, and workload class, with audit logs explaining why a model was selected.

Implementation Checklist

Use this checklist before enabling fallback on a production route:

  • Define the route’s latency budget and request deadline.
  • Classify provider errors into retryable, fallback-eligible, and terminal classes.
  • Document which failures must not trigger fallback.
  • List approved primary and fallback models by workload class.
  • Verify context length, tool support, schema support, and safety behavior for each fallback.
  • Validate every final response against the application contract.
  • Set maximum retry count, backoff, jitter, and Retry-After handling.
  • Configure circuit breakers by provider, model, region, and error class.
  • Set cost multipliers and fallback spend limits.
  • Log request IDs, error classes, attempts, validation results, latency, and cost.
  • Review p95 and p99 latency after rollout, not only average latency.
  • Test provider outage, rate limit, invalid schema, safety refusal, and output validation failure scenarios.

Actionable Conclusion

A good LLM fallback strategy is a governed routing policy. It separates retry from fallback, classifies failures precisely, preserves output contracts, respects latency deadlines, and makes cost visible before incidents happen.

The key design principle is invariance. Decide what must remain true when traffic moves from the primary model to a fallback: schema validity, safety behavior, context handling, tool support, cost limits, and observability. Then allow fallback only when those invariants can be preserved.

Start with one high-value route rather than a global fallback switch. Define its error taxonomy, compatible fallback models, validation rules, circuit breakers, and metrics. Run failure drills against that route. If final success rate improves without unacceptable p99 latency, contract-valid response loss, or cost increase, expand the policy to the next workload class.

Related reading

FAQ

Korduma kippuvad küsimused

Kas igal LLM-i taotlusel peaks olema varumudel?
Ei. Varundus on kõige kasulikum marsruutide puhul, kus kättesaadavus on oluline ja ühilduv alternatiivne mudel suudab säilitada sama väljundlepingu, ohutuskäitumise, latentsuseelarve ja kulupoliitika. Mõned marsruudid peaksid selle asemel kiiresti ebaõnnestuma.
Millised vead ei tohiks vallandada LLM-i varu?
Autentimisvead, valesti vormindatud päringud, toetamata parameetrid, deterministlikud skeemivead ja ohutustest keeldumised peaksid tavaliselt olema lõplikud. Selliste juhtumite tagavara võib varjata toote vigu või tekitada juhtimisriski, välja arvatud juhul, kui on heaks kiidetud selgesõnaline alternatiivne käitumine.
Mille poolest erineb uuesti proovimine tagavarast?
Proovi uuesti saadab sama päringu uuesti samale pakkujale, tavaliselt mööduva tõrke korral. Backback saadab päringu teisele mudelile, pakkujale, piirkonnale või marsruudile. Korduskatse peaks toimuma piiratud tähtaja jooksul; tagavara peaks toimuma ainult siis, kui poliitika, ühilduvus, järelejäänud aeg ja eelarve seda võimaldavad.
Kuidas peaksid meeskonnad mõõtma varuedu?
Mõõtke lõpliku edukuse määra koos p95 ja p99 latentsusajaga, tagasikutsumise määra, eduka vastuse kulu, lepingu kehtivusaja vastuse määra ja kvaliteetsete regressioonisignaalidega. Ainuüksi töötlemata päringu õnnestumine võib varjata aeglasemat, kallimat või vähem usaldusväärset väljundit.