Skip to main content

Fallback between two LLM providers with ai-proxy-multi

Serve one chat endpoint from an on-premises model with a cloud model behind it, force the first to fail, and watch the gateway switch.

By Akif F. Güven AI Gateway

What you will have at the end

A Route, assistant-api, that answers chat completions. Behind it sit two model instances: an on-premises model server as the primary and a cloud model as the fallback. When the primary answers 5xx, the engine sends the same request to the fallback and the client never notices, except in the latency. Traffic Explorer shows which instance answered each call, and the audit log keeps the change with both API keys masked. A token budget with ai-rate-limiting goes on top at the end, with one measured limit of its own: it counted the primary’s tokens and not the fallback’s.

Prerequisites

  • An on-premises model server with an OpenAI-compatible chat endpoint. In this article it is llm-onprem:8080, serving llama-3.3-70b-instruct, and it can be made to answer 503 on demand.
  • An API key for the cloud provider. Here it is an Anthropic key, used for claude-haiku-5-5.
  • A Consumer for the client, support_assistant, with a key-auth credential, and traffic logging turned on under Traffic Explorer › Settings.

Step 1: the Route and the Consumer

Create the Route assistant-api with the path /ai/chat, the method POST and Target › None: the Plugin calls the model providers itself, so the Route has no Upstream. Add key-auth to it, and create the Consumer support_assistant with a key-auth credential on its Credentials tab.

Step 2: two instances in ai-proxy-multi

Add ai-proxy-multi to the Route. Its Instances setting is a list of providers and is edited as JSON in the form; Use example fills in a two-instance skeleton to edit. The two instances of this article:

[
  {
    "name": "onprem-llama",
    "provider": "openai-compatible",
    "priority": 10,
    "weight": 100,
    "auth": { "header": { "Authorization": "Bearer …" } },
    "override": { "endpoint": "http://llm-onprem:8080/v1/chat/completions" },
    "options": { "model": "llama-3.3-70b-instruct" }
  },
  {
    "name": "anthropic-haiku",
    "provider": "anthropic",
    "priority": 0,
    "weight": 100,
    "auth": { "header": { "x-api-key": "…" } },
    "options": { "model": "claude-haiku-5-5", "max_tokens": 256 }
  }
]

The primary has the higher priority, so it is tried first. override.endpoint points the OpenAI-compatible provider at the on-premises server. The cloud instance needs only its provider name, the key and the model: the engine knows the provider’s address and request format.

Under Behavior, tick http_5xx in Fallback strategy: a 5xx from the instance that was picked sends the request to the next one. The form notes which combinations the engine accepts, and the fields under it tune the behaviour: Fallback status codes for other statuses that should fall back, Fallback retry limit for how many more instances one request may try, and Retry failures within so that only fast failures are retried and a slow one is returned to the client instead of doubling the wait.

The Behavior section of the ai-proxy-multi form: Fallback strategy with http_5xx ticked, Fallback status codes, the Logging switches, Fallback retry limit and Retry failures within The fallback is a choice of status codes. 5xx covers the on-premises server restarting; 429 would cover a cloud provider’s own rate limit.

Save the Plugin. The Plugin’s page shows the two instances with their priorities and their auth headers as ••••.

The ai-proxy-multi Plugin on assistant-api: two instances, onprem-llama at priority 10 and anthropic-haiku at priority 0, both auth headers masked, fallback strategy http_5xx Two providers behind one Route. The keys are stored on the engine and shown only to roles that manage Routes.

Step 3: a call on the primary

The client sends an OpenAI-style chat completion to the Route with its API key:

curl -X POST http://localhost:9281/ai/chat -H "apikey: $ASSISTANT_KEY" \
  -H 'Content-Type: application/json' \
  -d '{"messages":[{"role":"user","content":"Where is order ord-10421?"}]}'

The answer comes from the on-premises model in about 50 ms, with "model": "llama-3.3-70b-instruct" and a usage block of 65 tokens.

Step 4: force the primary to fail

The on-premises server was switched into its restarting state, where it answers every completion with 503. The same call to the Route, unchanged, now answers 200 in about 1.6 seconds, with "model": "claude-haiku-5-5" and the cloud model’s own wording. The client sent one request and got one answer; the engine made two attempts.

What Traffic Explorer shows

Filter Traffic Explorer by the path /ai/chat. The Upstream node column is the evidence: the records before the failure name llm-onprem:8080 with latencies of tens of milliseconds, the records after it name api.anthropic.com:443 with latencies of one and a half seconds and more.

Traffic Explorer filtered by /ai/chat: four records answered by llm-onprem:8080 in 28 to 103 ms, then five answered by api.anthropic.com:443 in 1.5 to 1.9 s, all status 200 for the Consumer support_assistant Same Route, same Consumer, same status. The node and the latency tell you which model answered.

Open one of the later records. The Overview reads api.anthropic.com:443 · 2 attempts: the engine tried the on-premises instance, received the 503, and went to the fallback within the same request. The timing bar shows 1 ms in the gateway and the rest waiting for the model.

The request record of a fallback call made from inside the lab: Upstream node api.anthropic.com:443 with 2 attempts, 11 ms in the gateway and 1.65 s in the upstream Two attempts, one record. The first attempt is why the fallback exists; the second is why the client saw a 200.

The Route’s Analytics tab counts the same calls and their backend time, so the switch is visible there too.

What the audit log keeps

The change that put the two instances on the Route is one audit record. Its diff shows every setting, and both keys are masked on the after side; the record names the two masked paths that changed.

The audit record of the Route update that added ai-proxy-multi: the instances, their endpoints and models in the diff, the Authorization and x-api-key values masked, and the two masked paths named under the diff What changed, who changed it, and never the key. The provider endpoint and the model names are in the clear because they are configuration, not secrets.

Token limits on top with ai-rate-limiting

A fallback to a paid model needs a cap. Add ai-rate-limiting to the Route: Limit 300, Time window 60 seconds and Rejected code 429, with Limit strategy left at its default, total tokens. The limit counts the tokens the model reports in its usage, not the requests.

The ai-rate-limiting Plugin on assistant-api as saved: Limit 300, Time window 60 s, Rejected code 429 A budget in tokens, not requests. A long answer costs more of it than a short one.

With the on-premises model answering again, every call cost 65 tokens and the response headers counted them down:

X-AI-RateLimit-Limit-onprem-llama: 300
X-AI-RateLimit-Remaining-onprem-llama: 40
X-AI-RateLimit-Reset-onprem-llama: 40

The next call after the budget reached zero was refused with 429, and the refusals sit in Traffic Explorer as records with gateway as their node.

One measurement to know about. While the primary was failing and the fallback answered, the headers still carried the primary’s name and Remaining stayed at 300: the tokens the fallback produced were not counted against the budget. The form offers an Instances table with a budget per instance name, which is where a separate cap for the cloud model belongs; this article did not measure it.

What to do next

  • Decide what counts as a failure. http_5xx covers an outage; add http_429 if the cloud provider’s own limits should also send traffic to the other instance.
  • Put the Consumer’s key behind the company’s identity provider instead: Keycloak and G8.
  • Read how provider routing, token limits and prompt rules fit together on AI Gateway.
Written by
Akif F. Güven
Engineer, G8
Next step

See G8 in a demo

We’ll show you the console with APIs and AI traffic like yours, and answer the questions these articles leave open.

  1. 01Demo
  2. 02Proof of conceptOn Docker for a quick start, or on the infrastructure you plan to run G8 on.
  3. 03LicenseWe prepare a quote for your edition and nodes, and you receive a signed license file.