Skip to main content

A Route to language models: primary, fallback and the keys at the gateway

Two model providers behind one Route with ai-proxy-multi: priority, a fallback on 5xx, provider keys sealed at the gateway, the fallback measured and recorded.

By Akif F. Güven AI Gateway

What you will have at the end

A Route, assistant-api, that answers chat completions for an application which holds nothing but an API key for the gateway. Behind it, two model instances: an on-premises model server as the primary and a cloud model as the fallback. The provider keys are typed once into the Plugin and stored at the gateway, masked in the Plugin’s page and in the audit log. When the primary answers 5xx, the same request is sent to the fallback within the same call, and Traffic Explorer names which provider answered each request, with the retry marked. This is the form-first counterpart of Fallback between two LLM providers, which tells the same story with more context.

Prerequisites

  • A Route and a Consumer. assistant-api serves POST /ai/chat with key-auth; the Consumer support_assistant holds the key the application sends.
  • An on-premises model server with an OpenAI-compatible chat endpoint. The lab’s is llm-onprem:8080, serving llama-3.3-70b-instruct, and it can be switched into a restarting state in which it answers 503.
  • A cloud provider account and its key, here Anthropic with claude-haiku-5-5. The fallback calls below went to the real provider with the lab’s own key on a real account; the key went into the form once, is masked in every screenshot and in the audit log, and no part of it is printed here.
  • Traffic logging turned on under Traffic Explorer › Settings, and a role that may manage Routes. The screenshots below were taken as nora.becker.

The two instances

Open the Route, Plugins, Add plugin, ai-proxy-multi. Its first setting, Instances, is a list edited as JSON; Use example fills in a two-instance skeleton. Each instance carries a name, a provider, a priority, a weight, its auth header and its options:

  • onprem-llama: provider openai-compatible, priority 10, weight 100, the model llama-3.3-70b-instruct under options, an Authorization header under auth, and under override the endpoint of the on-premises server, because an OpenAI-compatible provider needs to be told where it lives.
  • anthropic-haiku: provider anthropic, priority 0, weight 100, the model and max_tokens under options, and the x-api-key header under auth. A known provider needs no endpoint.

The higher priority is tried first; weight shares the traffic among instances of the same priority. One thing to know about the editor: while you edit, the keys are in the clear in it, as they are in any editor, and only roles that manage Routes reach the form. Once saved, the Plugin’s page shows them as dots.

The ai-proxy-multi Plugin on assistant-api as saved: the Instances table with onprem-llama, provider openai-compatible, priority 10, and anthropic-haiku, provider anthropic, priority 0; Fallback strategy http_5xx; Summaries off; and the note that secrets are shown only to roles that manage routes Two instances, two priorities. Their keys are not in this view; the audit record below shows both as four dots. The application that calls the Route never holds either.

When to fall back

Under Behavior, Fallback strategy is a set of checkboxes: http_429, http_5xx, instance_health_and_rate_limiting and rate_limiting. Tick http_5xx: a 5xx from the instance that was picked sends the request to the next one. The note under it says which combinations the engine accepts, and Fallback status codes adds codes of your own beside the ones the strategy names. Further down, Logging › Payloads and Summaries add the bodies, or the model, the duration and the token counts, to the record a logger Plugin on this Route writes; Fallback retry limit caps how many more instances a failed request may try.

The Behavior section of the ai-proxy-multi form: Fallback strategy with http_5xx ticked and the three other choices clear, the note about which combinations the engine accepts, Fallback status codes empty, Logging with Payloads and Summaries off, and Fallback retry limit empty The fallback is a choice of status codes. 5xx covers a model server restarting; 429 would cover a cloud provider’s own limit.

Select Add plugin. The Plugins tab draws the request path: key-auth first, then ai-proxy-multi, then the token budget of the next article, and no Upstream at all, because the Plugin is the backend.

The Plugins tab of assistant-api: the Request path rail with the request POST /ai/chat, the two global rules, then key-auth at 2500, ai-proxy-multi at 1041 and ai-rate-limiting at 1030, and Upstream None No Upstream. The Plugin picks the provider for each request and sends it there.

What the application sees

The application sends a chat completion with its gateway key and nothing else:

curl -X POST http://localhost:9281/ai/chat -H 'apikey: …' -H 'content-type: application/json' \
  -d '{"messages":[{"role":"user","content":"Where is order ord-10421?"}]}'
### without a key
HTTP/1.1 401 Unauthorized
### with the key, primary up
HTTP/1.1 200 OK
model: llama-3.3-70b-instruct · "Order ord-10421 is open and will ship within two working days." · usage 48 + 17 tokens

The on-premises server was then switched into its restarting state, where it answers every completion with 503. The same call, unchanged:

### while the primary answers 503, the first of two calls
HTTP/1.1 200 OK · 2.6 s
model: claude-haiku-5-5 · "I can't look up order ord-10421. I don't have access to your order system…" · usage 18 + 256 tokens
### primary restored
HTTP/1.1 200 OK
model: llama-3.3-70b-instruct

The application saw three 200s and never learned that one of them came from another provider; the model name in the body is the only trace. The content differs, because the fallback model has no access to the lab’s order system, which is a reminder that a fallback answers with its own knowledge.

What Traffic Explorer shows

The Upstream node column is the evidence. The records of the primary name llm-onprem:8080 with latencies from 6 to 148 ms; the records of the fallback name api.anthropic.com:443 with 2.6 seconds and a +1, the retry mark: the request was handed to the primary first.

Traffic Explorer filtered to the path /ai/chat: 200 records for assistant-api · support_assistant, three answered by llm-onprem:8080 in 6 to 148 ms, two answered by api.anthropic.com:443 +1 in 2.61 and 2.63 s Which provider answered, per request. The +1 rows are the ones the primary refused.

One of the fallback records, opened: the Timing tab reads Gateway 1 ms, Upstream 2.60 s, and the Upstream node line 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 request record of a fallback call: POST /ai/chat 200 for assistant-api · support_assistant, Timing Total 2.61 s, Gateway 1 ms, Upstream 2.60 s, and Upstream node api.anthropic.com:443 · 2 attempts Two attempts, one record. The first attempt is why the fallback exists; the second is why the client saw a 200.

What the audit log keeps

Every save of the Plugin is a Route update with the whole block in the diff: the instances, their endpoints, models and priorities, and both auth headers as •••• on both sides. The record below is a later change, the Summaries switch turned on, and it shows the same masking on a one-line change.

The Change section of an audit record of the assistant-api Route update: before and after side by side, logging.summaries false to true as the one changed line, the two instances with Authorization and x-api-key as four dots on both sides, the fallback strategy http_5xx and the ai-rate-limiting block The keys are the two values the log never holds. Everything else about the two providers is there to read.

What to do next

Written by
Akif F. Güven
Engineer, G8
Was this helpful?
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.