> ## Documentation Index
> Fetch the complete documentation index at: https://docs.promptguard.co/llms.txt
> Use this file to discover all available pages before exploring further.

# Route requests to another model or provider

> Rewrite the upstream provider/model on the fly based on request shape, with optional failover and fallback models

Routing rules are priority-ordered rewrites that PromptGuard applies after the policy engine has allowed (or redacted) a request, before it resolves the provider key. Each rule has a **condition** (when it fires) and an **action** (what it changes). The first matching rule wins.

The three big use cases:

1. **Cost control**: downgrade simple prompts to a cheaper model.
2. **Vendor pinning**: force a specific tenant onto a specific provider, e.g. for compliance or data-residency reasons.
3. **Resilience**: name the models to fall back to when a model is rate limited or down, without changing your code.

**Prefer to watch?** This page as a silent, step-by-step video:

**Tutorial 09** ·  · a silent follow-along of this page. Pressing
play loads the video from YouTube.
Watch on YouTube

**Prefer to watch?** This page as a silent, step-by-step video:

**Tutorial 09** ·  · a silent follow-along of this page. Pressing
play loads the video from YouTube.
Watch on YouTube

## Anatomy

```json theme={"system"}
{
  "name": "downgrade short prompts",
  "description": "Anything under 500 chars goes to gpt-5-nano",
  "priority": 100,
  "condition": {
    "max_tokens": 500
  },
  "action": {
    "provider": "openai",
    "model": "gpt-5-nano"
  }
}
```

* **priority**: lower numbers fire first. Use 100, 200, 300, ... so you can wedge new rules between old ones without renumbering.
* **condition**: JSON object describing what to match. See the conditions reference below.
* **action**: JSON object describing the rewrite. Sets `provider`, `model` or `fallback_models` (at least one), and optionally `failover_provider`.

## Conditions reference

| Key | Meaning |
| - | - |
| `model` | Inbound model glob (`fnmatch`-style: `gpt-4*`, `claude-3-*`). |
| `header_match` | `{"x-tenant": "acme"}`: matches when each named request header contains the value, ignoring case. Useful for tenant pinning. |
| `end_user` | Match a specific `X-End-User`. |
| `max_tokens` | Match when the prompt's estimated token count is at most this. |
| `min_tokens` | Match when the prompt's estimated token count is at least this. |
| `country` | A 2-letter country code. The proxy does not resolve a caller's country today, so a rule with this condition never matches. |

Conditions are AND-ed together inside a rule. Use multiple rules with different priorities for OR semantics.

## Actions reference

| Key | Meaning |
| - | - |
| `provider` | Rewrite the upstream provider (`openai`, `anthropic`, `bedrock`, ...). |
| `model` | Rewrite the model name. |
| `failover_provider` | If the primary upstream returns a 5xx on a non-streaming request, retry the **same model** exactly once against this provider. |
| `fallback_models` | Up to 3 **other models** to try, in order, when the model fails with a rate limit, a timeout or a server error. Works for streamed requests too, before the first byte. See [Fallback models](#fallback-models). |

## Smart failover

When `failover_provider` is set on a matched rule and the primary upstream returns 5xx on a non-streaming request, PromptGuard:

1. Re-authenticates against the failover provider's stored Provider Key. With no stored key for that provider, there is no retry and the caller sees the primary's error.
2. Issues exactly one retry. No exponential backoff, no recursion, if the failover also 5xx's, the caller sees that error.

Streaming requests are not retried by `failover_provider`: the primary's 5xx reaches the caller, unless the rule also names `fallback_models`. Non-5xx responses (rate limits, validation errors, timeouts) are surfaced as-is. The gateway does not switch vendors for a 429, because a 429 from the primary usually means *your account* is being rate limited, and switching providers without telling the caller is a great way to silently drop traffic.

The retry shares the request's one time limit with every other call to a provider, so it never makes a request take longer than a single call could. See [One time limit for the whole request](https://docs.promptguard.co/gateway/fallback-models.md#one-time-limit-for-the-whole-request).

## Fallback models

A rule's `fallback_models` is the same chain a request can name for itself, applied to every request the rule matches. It works exactly as described in [Fallback Models](https://docs.promptguard.co/gateway/fallback-models.md): the models are tried in order after a failure worth retrying, each one only on a vendor you have consented to, and the response's `X-PromptGuard-Model-Used` header names the model that answered.

```json theme={"system"}
{
  "name": "keep GPT-4o answering",
  "priority": 100,
  "condition": { "model": "gpt-4o" },
  "action": { "fallback_models": ["gpt-4o-mini", "claude-haiku-4-5"] }
}
```

* **A chain in the request replaces the rule's.** A request that sends its own `fallback_models` field or `X-PromptGuard-Fallback-Models` header uses that chain and never the rule's. The rest of the rule (its `provider` and `model`) still applies.
* **The chain follows the rule's rewrite.** When the rule also sets `model`, the fallbacks are for that model.
* **With `failover_provider` too**, a non-streamed request's model gets its retry on the failover provider first, and the fallback models are tried after it.
* **A model on a vendor you have not consented to**: no stored provider key for it, is skipped for every request the rule matches, and the request goes ahead with its own model and the rule's other fallback models. So is a model on another vendor on `/api/v1/messages` or `/api/v1/responses`, whose bodies only one vendor can read. A rule's chain never stops a request that would otherwise have been answered. Saving the rule warns you about each model on a vendor with no stored key, and saves it anyway.
* **Saving the rule refuses** a chain of more than 3 models, one that names a model twice, or a model no provider serves.

In the dashboard, set the chain in the rule editor's **Fallback models** field, a comma-separated list, rather than in the action JSON.

## Match counters

Every rule that fires increments `match_count` and updates `last_matched_at`. The dashboard surfaces these so you can spot dead rules (created six months ago, never matched) and aggressive ones (matching 90% of traffic, probably too broad).

```sql theme={"system"}
-- Quick health check from the SQL editor
SELECT name, priority, match_count, last_matched_at
FROM routing_rules
WHERE user_id = $1
ORDER BY match_count DESC;
```

## Worked example

```json theme={"system"}
[
  {
    "name": "internal team always Sonnet",
    "priority": 50,
    "condition": { "header_match": { "x-tenant": "internal" } },
    "action": { "provider": "anthropic", "model": "claude-sonnet-4-5" }
  },
  {
    "name": "downgrade short prompts",
    "priority": 100,
    "condition": { "max_tokens": 500 },
    "action": { "provider": "openai", "model": "gpt-5-nano" }
  },
  {
    "name": "GPT keeps answering",
    "priority": 1000,
    "condition": { "model": "gpt-*" },
    "action": {
      "provider": "openai",
      "fallback_models": ["claude-haiku-4-5"]
    }
  }
]
```

A `gpt-4` request from an `internal` tenant gets rewritten to Claude Sonnet 4.5 (priority 50 fires first). A short prompt without that header goes to `gpt-5-nano`. Any other GPT request stays on OpenAI, and falls back to Claude 3.5 Haiku when OpenAI is rate limited or down, provided you have stored an Anthropic provider key.

## Next steps

> [card] [Fallback models](https://docs.promptguard.co/gateway/fallback-models.md)
>
> Models to try when the one you asked for is down

> [card] [Provider keys](https://docs.promptguard.co/gateway/provider-keys.md)
>
> Store your provider credentials once, forwarded server-side

> [card] [Caching](https://docs.promptguard.co/gateway/caching.md)
>
> Serve repeated requests from cache


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.