# Cost, occupancy, tags and semantic fallbacks

**Last updated:** 2026-11-24T13:12:28-03:00 (UTC-3)

Four ways to decide which target serves a request, beyond the declared order and [weights](/docs/features/routing-and-admission/):

| Feature | What it does | Flag |
| --- | --- | --- |
| `cost` strategy | Tries the cheapest target first | `routing_strategies` |
| `least_busy` strategy | Tries the target with the fewest in-progress requests first | `routing_strategies` |
| Tag routing | Reserves targets for requests with certain tags (for example, enterprise customers) | `tag_routing` |
| Semantic fallbacks | Sends to another alias when the request exceeds the context or hits the content policy | `semantic_fallbacks` |

## By cost or by occupancy

```yaml
features:
  routing_strategies: true
models:
  - id: chat
    routing_strategy: cost        # or least_busy; ordered is the default
    targets:
      - {provider: openai, model: gpt-4.1-mini}
      - {provider: groq, model: llama-3.3-70b-versatile}
```

- **`cost`:** the lowest-priced target goes first. The price is input + output per million tokens, from the price book or [automatic pricing](/docs/features/automatic-pricing/). A target without a price goes last.
- **`least_busy`:** the target with the fewest in-progress requests on this replica goes first. It is the default of the alias a [local inference](/docs/features/local-inference/) pool generates, where a replica that is not ready goes last.
- The other targets remain as fallbacks, in the strategy's order.

In the console, the model editor shows the **Routing strategy** section when the alias has two or more targets and no weights.

Limits:
- requires two or more targets;
- does not combine with weights, [latency](/docs/features/latency-routing/) or [automatic routing](/docs/features/auto-routing/);
- the Responses route keeps the declared order.

## By tags

Useful for giving a group of customers a dedicated deployment without creating another alias:

```yaml
features:
  tag_routing: true
models:
  - id: chat
    targets:
      - {provider: dedicated, model: gpt-4.1, tags: {tier: enterprise}}
      - {provider: shared, model: gpt-4.1}
usage_tags:
  allowed_values: {tier: [enterprise, free]}
  defaults_by_team: {acme: {tier: enterprise}}
```

`targets[].tags` reserves a target for requests whose usage tags carry every listed name and value. Tags come from the `X-Gateway-Tags` header and from the team defaults (see [usage tags](/docs/features/usage-tags/)).
- Targets whose tags match the request's come first.
- Targets without tags form the general pool.
- With no eligible target, the response is `403 no_target_for_tags`.

In the console, each target has the **Reserved for tags** field.

## Semantic fallbacks

A request too long for the model, or refused by the provider's content policy, can go on to another alias instead of coming back as an error:

```yaml
features:
  semantic_fallbacks: true
models:
  - id: chat
    context_window_fallback: chat-long    # context overflow
    content_policy_fallback: chat-safe    # refusal by content policy
    targets: [{provider: openai, model: gpt-4.1-mini}]
```

- `context_window_fallback` names the alias that serves a request too long for the model's context window, and `content_policy_fallback` the alias that serves a request the provider refused for its content policy; both need `semantic_fallbacks`.
- The second attempt happens only once. Its answer carries `X-Gateway-Fallback: <kind>:<alias>` (`context_window:chat-long`).
- Other 400 errors come back as they were.
- With `targets[].max_input_tokens` declared, a request clearly too long for a target is caught **before** it is sent: the target is skipped without a provider call, the next target is tried, and when none is left the request goes to `context_window_fallback` the same way. See [context window check before dispatch](/docs/features/context-window-check/).

The fallback alias is admitted exactly like a direct request of the same key, so a fallback can never reach a model, budget or plan the key could not reach on its own:

| Check on the fallback alias | Applies when |
| --- | --- |
| The key's `allowed_models` (and access groups) | Always |
| The team policy | `team_policy_enforcement` is on |
| The organization ceiling | `organizations` is on |
| Plan entitlements for the alias's features (routing strategy, retries, semantic cache and so on) | `feature_plan_entitlements` is on |
| Rate limits, including per end user | `rate_limits` / `end_user_rate_limits` are on |
| Budgets and TPM/TPD, including model, team and end-user policies, with an approved rate for the fallback's target | `budget_limits` / `tpm_limits` are on |
| Input guardrails of the fallback alias | `guardrails` is on |

The second attempt carries the client's `X-Gateway-End-User`, `X-Gateway-Session-Id`, `X-Gateway-Tags`, `X-Gateway-Cache` and `X-Gateway-Timeout-Ms` headers, so end-user and tag budgets, tag-reserved targets, cache directives and the client's timeout apply to it too. It is a separate request in usage and budgets, with the request ID `<request id>.fallback`. The refused first attempt keeps its own record and reservation under the usual rules.

When the fallback alias gets no provider answer, there is no fallback. That covers a refusal by the gateway before dispatch (access, plan, rate limit, budget, guardrail, quota or policy storage unavailable) and a provider that could not be reached or timed out. The client then gets the original provider error unchanged, which names the real cause (the prompt is too long, the content was refused), and the header `X-Gateway-Fallback-Refused: <kind>:<code>` with the gateway code of the refusal:

```http
HTTP/1.1 400 Bad Request
X-Gateway-Fallback-Refused: context_window:model_forbidden

{"error":{"code":"context_length_exceeded","message":"This model's maximum context length is 8192 tokens"}}
```

Common codes are `model_forbidden` (key, team policy or organization), `entitlement_forbidden`, `rate_limit_exceeded`, `quota_exceeded` (see [budgets and TPM](/docs/features/budgets-and-tpm/)), `guardrail_blocked`, `quota_unavailable`, `upstream_error` and `request_timeout`. The header never names the fallback alias, which the key may not be allowed to know. The refusal is also logged as `semantic_fallback_refused` with the kind and code. If the fallback's provider answers with an error, that answer is returned as it is, with `X-Gateway-Fallback`.

## See also

- [Weights and per-target capacity](/docs/features/routing-and-admission/).
- [Automatic routing](/docs/features/auto-routing/): choosing the alias by the request's content.
- [Circuit breaker](/docs/features/resilience/): taking a failing target out of rotation.
