AI engineering

LLM Gateways: Model Routing, Fallback and Cost

Gateway responsibilities are separated into a common request schema, model catalogue, authorisation, quotas and cost accounting. Routing uses task requirements, context length, data classification and latency targets, while fallback paths are tested for schema and quality compatibility. Retries and complete task costs extend token-price arithmetic. Cache isolation, state-changing operations and staged rollout define the constraints on cost optimisation.

On this page

What responsibilities belong to the gateway?

A gateway can provide unified request schemas, identity, a model catalog, routing policies, quotas, cost records, provider error handling and output validation. It should not absorb every application's business logic. Centralize model infrastructure decisions while keeping domain rules within the owning service. Stable errors and traceable request IDs improve developer experience. Adding a model should not silently break existing application contracts. Without a clear boundary, a gateway can become a difficult-to-maintain monolith.

How should model routing rules be defined?

A first routing policy can use observable fields such as task type, required schema, context size, data classification and latency target. A learned difficulty classifier can be added later, but its errors become part of the system. “Short request means small model” is a weak rule: a brief legal question may be harder than a long generic summary. Compare routing policies on the same labeled quality and cost evaluations. Data-residency and privacy requirements may be hard constraints on provider selection.

Model routing through a gatewayRequest identityRouting policyProvider modelValidation + fallbackTrace + billing
Figure 1. A common contract and policy constrain model selection and fallback; output validation accompanies cost accounting.

What belongs in a cost model?

A basic call cost is input tokens × input unit price plus output tokens × output unit price. Cached tokens, tool calls, images, audio, retries and provider-specific billing units can change that total. Prices change over time, so version the pricing catalog and record its source date. Cost per thousand requests is misleading when request lengths differ. Measure cost and verified quality by task category, including fallback and retry overhead.

Why is fallback more than picking another model?

Calling an alternative provider during an outage can improve availability, but the second model may differ in tool schemas, context limits, structured outputs and safety behavior. Fallback must satisfy contract compatibility and quality tests. Retrying an agent run after an uncertain side effect can duplicate transactions; reconcile state first. For some tasks, degraded service or a clear error is safer than a less capable replacement. Critical business rules must remain in provider-independent application code.

Caching strategy and data privacy

Prefix, full-response and semantic caches store information under different assumptions. Reusing a user-specific response depends on identity, role, document version and tenant boundaries. Semantic similarity does not establish equal security conditions either. A high hit rate is not beneficial when it returns another user’s answer or an outdated record. Expiry and invalidation determine the correctness cost accompanying the speed gain.

Distinctions that matter in designSmall model: quality gateLarge model: marginal benefitCache: consistency gateFallback: contract compatibility
Figure 2. Cheap calls, low completed-task cost, compatible fallback and safe caching are separate objectives.

Rate limits, quotas and budgets

A gateway can enforce per-user or per-team request counts, token budgets, concurrency and provider quotas. Hard limits may abruptly reject traffic; softer warning policies suit some workflows. Input token estimates carry uncertainty and must be reconciled against billed usage after completion. Failed retries still consume quotas. Mixing critical interactive traffic with batch jobs can create fairness problems. Dashboards should distinguish estimated from realized spend.

Which routing metrics should be measured?

A routing policy that reduces cost should show which subgroups experience quality changes. Track completion, schema validity, evidence support, p95 latency, errors and fallback rates by route. Outages and price changes affect providers differently. Compare the same task mix: if easy requests go to cheaper models and hard requests to expensive models, raw per-model scores are confounded. Shadow evaluation can compare identical requests across candidate routes, subject to privacy constraints.

Introducing a gateway in stages

A common single-provider API separates the gateway contract from later routing decisions. When a second model is added, shadow execution can expose behavioural differences before its responses reach users. Limited traffic makes the latency and cost effects easier to observe. Rollback is more than restoring a model name: tool contracts, output schemas and caches must remain compatible. Cost reductions become interpretable only in the context of the tasks where they occur.

Support questions routed to a cheaper model

Illustrative scenario; not a measured project result.

Imagine a support application using a small model for routine classification and a stronger model for contract comparisons. If the smaller model fails on specialized Turkish terminology, token count alone is a poor routing signal. Provider failures can trigger expensive retries; migrating an agent run with side effects between models also threatens state consistency. This is an illustrative gateway design.

A cheap call versus a low-cost completed task

Routing to a cheaper model can reduce the first call’s price. Extra retries, longer responses or human intervention can change the total cost of completing the task. Fallback may improve availability while changing schema support, tool capabilities or data boundaries. A gateway is valuable because it makes these choices visible in one place, not merely because it selects the lowest token price.

Frequently asked questions

Are a gateway and LLMOps the same?

No. A gateway centralizes model access; LLMOps also spans evaluation, deployment and data processes.

Does routing to a cheaper model reduce quality?

Sometimes; test quality on comparable task distributions.

Should every provider outage trigger automatic fallback?

No. Side effects, permissions and incompatible contracts may call for controlled failure.

References and further reading

  1. OpenAI — API Pricing
  2. vLLM documentation
  3. OpenAI — Agents SDK
  4. OpenTelemetry — Documentation