Skip to main content
Version: Dev

Multi-Target Routing and Failover

A multi-target model exposes one caller-facing alias for several direct models. AISIX selects a target for each request and can retry or fail over without requiring the application to change model names.

Each target keeps its own provider key and upstream model name. Callers see only the routing alias, including in GET /v1/models. Grant a caller access to the alias alone when the targets should remain an implementation detail.

The routing model selects a direct target, and that target uses its provider key to call the upstream model:

Choose a Strategy​

Choose a strategy based on how the caller-facing alias should distribute traffic:

GoalStrategySelection Behavior
Keep a primary target with ordered backups.failoverAttempts targets in declaration order.
Rotate traffic across similar targets, optionally by share.round_robinSmooth weighted round-robin over target weights. Equal or absent weights rotate in declaration order.
Pin each session to one target.consistent_hashHashes a per-request key over a weighted ring. See canary traffic and session-affine pools.
Prefer the lowest estimated price.least_costRanks eligible targets by configured prices or control-plane catalog pricing.
Prefer the fastest recently observed target.least_latencyRanks eligible targets by recent upstream latency.
Prefer the target with the lowest weighted concurrency.least_busyRanks eligible targets by (1 + current in-flight requests) / max(weight, 1), lowest first.

Failover is the default strategy. Two target settings refine any strategy:

SettingEffect
weightControls the rotation share for round_robin, the hash-ring share for consistent_hash, and the weighted concurrency score for least_busy. The default is 1. Other strategies ignore it.
prioritySplits targets into tiers. Higher values are preferred; use -1 for backups. The default is 0. See Pin Sessions with Pool Failover.

AISIX applies routing tags and allowed_cidrs before it evaluates priority. A lower-priority target can serve a request when those filters exclude every higher-priority target.

Configure and Test a Failover Model​

The following example builds and tests a two-target model that keeps gpt-4o-primary as its preferred target and uses gpt-4o-secondary after an eligible failure. Instructions are provided for AISIX Enterprise, AISIX Cloud, and AISIX OSS.

Prerequisites​

Before starting, prepare the following:

  • Two or more direct models to use as routing targets. To run the failure test, the primary and secondary models must use separate provider-key resources, although both resources can contain the same upstream credential.
  • A caller API key that can call the multi-target model, or permission to create one.
  • cURL for verification requests. If you use the Admin API to configure AISIX Enterprise or AISIX Cloud, or run the optional status queries, also install jq.
  • For AISIX Enterprise or AISIX Cloud, an environment with an attached gateway. Dashboard users need permission to manage models and caller API keys, plus provider keys for the failure simulation. AISIX Cloud Admin API users need a write-scoped admin token.
  • For the open-source AISIX gateway, access to the declarative resources file and the gateway process.

Create the Model​

Create the direct target models before the multi-target model. Every management path below configures the same behavior: AISIX starts with the primary target and moves to the secondary target after a retryable failure.

Create the model group and allow the caller in the same environment.

Open the environment's Models page, select Create model, then choose Model group.

  1. Set Display name to chat-prod and Strategy to Failover.
  2. Under Targets, add gpt-4o-primary first and gpt-4o-secondary second. Drag the rows to correct the order if needed.
Failover model group with primary and secondary targets in order
  1. Expand Reliability — retries and fallback budget (optional). Set Retries to 1 and Max fallbacks to 1. Select Include upstream 429 in retries + failover.
Expanded retry and failover settings with one retry, one fallback, and upstream 429 enabled
  1. Select Create model group and confirm that the saved row shows the intended strategy and target order.
Saved chat-prod failover group showing its ordered targets

An existing caller key with all-model access already grants the new alias. Otherwise:

  1. Open API keys in the same environment.
  2. Select Edit for an existing caller whose plaintext you have saved, or select New API key and enter a display name.
  3. Keep Selected models and select chat-prod under Allowed models, retaining any other required models.
Caller model access with chat-prod selected

Finish the key according to the path you chose:

  • For an existing key, select Save changes.
  • For a new key, select Create API key, copy its plaintext from One-time view, then select I've saved it.

Export the caller credential for verification:

export AISIX_API_KEY="YOUR_CALLER_API_KEY"

To adjust the group later, select Edit on its row. Saving strategy, target, order, or reliability changes keeps the resource and its caller grant. To retire the alias, select Delete and confirm the action.

Verify Primary Routing​

Export the gateway URL for the deployment you configured:

# Use the gateway origin without a trailing slash or endpoint path.
# The local quickstarts use http://127.0.0.1:3000
export AISIX_PROXY="YOUR_AISIX_GATEWAY_URL"

For AISIX Enterprise or AISIX Cloud, continue using the AISIX_API_KEY exported or created earlier. For the open-source AISIX gateway, export the caller API key:

export AISIX_API_KEY="YOUR_CALLER_API_KEY"

Send a request to the multi-target alias:

curl -sSi -X POST "$AISIX_PROXY/v1/chat/completions" \
-H "Authorization: Bearer ${AISIX_API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"model": "chat-prod",
"messages": [
{"role": "user", "content": "Hello from AISIX routing."}
]
}'

A successful request starts with HTTP/1.1 200 OK. The response body retains chat-prod as the caller-facing model name, so by default the response does not tell the caller which target served it. To see the target, copy the x-aisix-request-id response header value and find the request's line in the gateway's access log. Its served_by_model field reports gpt-4o-primary. With the open-source quickstart container:

docker logs aisix-quickstart 2>&1 | grep "YOUR_REQUEST_ID"

On a test gateway, you can instead set proxy.expose_debug_headers to true in its startup configuration and restart it. The response then also includes x-aisix-served-by: gpt-4o-primary. The header identifies the direct model that served the request and is not present on cache hits, single-target model responses, error responses, or endpoints other than /v1/chat/completions.

Streaming requests can resolve multi-target aliases, but they do not fail over after a stream has started.

Simulate a Primary Failure​

After the normal request reaches gpt-4o-primary, make that target unreachable and repeat the request. For this test, the primary and secondary direct models must use different provider keys so changing the primary endpoint does not also affect the fallback.

caution

Use a dedicated non-production provider key for the primary target. In AISIX Enterprise and AISIX Cloud, a provider key is organization-scoped, and changing its endpoint affects every model that references it.

Change only the dedicated primary provider key so the secondary target remains available.

  1. Open Provider keys and select Edit on the test key used only by gpt-4o-primary.
  2. Record the existing API base, then replace it with https://api.openai.invalid/v1.
Primary test provider key pointed to a reserved unreachable endpoint
  1. Select Save. Confirm that the gateway has applied the latest configuration revision before repeating the request. See Resource Projection.

Verify Failover​

Repeat the request from Verify Primary Routing. It should still return HTTP/1.1 200 OK, but the access log should now report served_by_model as gpt-4o-secondary. With debug headers enabled, the response also includes:

x-aisix-served-by: gpt-4o-secondary

The response body still identifies the caller-facing routing alias.

Restore Primary Routing​

Edit the primary test provider key and restore its recorded API base. Clear the field if it was originally empty, then select Save.

Primary provider endpoint restored after the failure test

Repeat the request and confirm that served_by_model, or x-aisix-served-by when it is enabled, identifies gpt-4o-primary again. If you enabled cooldown on the primary target, it stays out of rotation until its current cooldown period expires, so wait for it to report healthy again first.

Next Steps​

Use Multi-Target Routing Patterns for cost-aware selection, canary traffic, session-affine pools, and request tags. See Multi-Target Runtime Behavior for endpoint support and retry rules. Continue with Semantic Routing when target selection should depend on request meaning.