Skip to content

Routing Rules

Configure intelligent routing rules for model selection and fallbacks.

List Routing Rules

Endpoint

GET /v1/management/routing-rules

Example

bash
curl https://api.gateflow.ai/v1/management/routing-rules \
  -H "Authorization: Bearer gw_admin_..."

Response

json
{
  "rules": [
    {
      "id": "rule_abc123",
      "name": "cost-optimized-routing",
      "priority": 1,
      "enabled": true,
      "conditions": {
        "models": ["auto", "gpt-5.2"]
      },
      "actions": {
        "route_to": "gpt-5-mini",
        "fallbacks": ["claude-haiku-4-5-20251015"]
      },
      "created_at": "2026-02-01T10:00:00Z"
    },
    {
      "id": "rule_def456",
      "name": "premium-routing",
      "priority": 2,
      "enabled": true,
      "conditions": {
        "api_keys": ["key_premium_*"],
        "models": ["auto"]
      },
      "actions": {
        "route_to": "gpt-5.2",
        "fallbacks": ["claude-sonnet-4-5-20250929", "gemini-3-pro"]
      },
      "created_at": "2026-02-05T10:00:00Z"
    }
  ]
}

Create Routing Rule

Endpoint

POST /v1/management/routing-rules

Request Body

ParameterTypeRequiredDescription
namestringYesRule name
priorityintegerNoEvaluation order (lower = first)
enabledbooleanNoWhether rule is active
conditionsobjectYesWhen to apply rule
actionsobjectYesWhat to do

Condition Fields

FieldTypeDescription
modelsarrayMatch these model requests
api_keysarrayMatch these API key patterns
headersobjectMatch request headers
metadataobjectMatch request metadata
time_rangeobjectMatch time of day
token_estimateobjectMatch estimated tokens

Action Fields

FieldTypeDescription
route_tostringPrimary model to use
fallbacksarrayFallback model chain
retryobjectRetry configuration
cacheobjectCache configuration
transformobjectRequest transformation

Examples

Cost-Optimized Routing

bash
curl -X POST https://api.gateflow.ai/v1/management/routing-rules \
  -H "Authorization: Bearer gw_admin_..." \
  -H "Content-Type: application/json" \
  -d '{
    "name": "cost-optimized",
    "priority": 1,
    "conditions": {
      "models": ["auto"],
      "metadata": {
        "prefer": "cost"
      }
    },
    "actions": {
      "route_to": "gpt-5-mini",
      "fallbacks": ["claude-haiku-4-5-20251015", "gemini-3-flash"]
    }
  }'

Quality-First Routing

bash
curl -X POST https://api.gateflow.ai/v1/management/routing-rules \
  -H "Authorization: Bearer gw_admin_..." \
  -H "Content-Type: application/json" \
  -d '{
    "name": "quality-first",
    "priority": 2,
    "conditions": {
      "models": ["auto"],
      "metadata": {
        "prefer": "quality"
      }
    },
    "actions": {
      "route_to": "gpt-5.2",
      "fallbacks": ["claude-sonnet-4-5-20250929", "gemini-3-pro"]
    }
  }'

Time-Based Routing

bash
curl -X POST https://api.gateflow.ai/v1/management/routing-rules \
  -H "Authorization: Bearer gw_admin_..." \
  -H "Content-Type: application/json" \
  -d '{
    "name": "off-peak-routing",
    "priority": 3,
    "conditions": {
      "time_range": {
        "start": "22:00",
        "end": "06:00",
        "timezone": "America/New_York"
      }
    },
    "actions": {
      "route_to": "gpt-5-mini",
      "cache": {
        "enabled": true,
        "ttl_seconds": 3600
      }
    }
  }'

Large Request Routing

bash
curl -X POST https://api.gateflow.ai/v1/management/routing-rules \
  -H "Authorization: Bearer gw_admin_..." \
  -H "Content-Type: application/json" \
  -d '{
    "name": "large-context-routing",
    "priority": 4,
    "conditions": {
      "token_estimate": {
        "min": 50000
      }
    },
    "actions": {
      "route_to": "gemini-3-pro",
      "fallbacks": ["claude-sonnet-4-5-20250929"]
    }
  }'

Header-Based Routing

bash
curl -X POST https://api.gateflow.ai/v1/management/routing-rules \
  -H "Authorization: Bearer gw_admin_..." \
  -H "Content-Type: application/json" \
  -d '{
    "name": "enterprise-routing",
    "priority": 5,
    "conditions": {
      "headers": {
        "X-Customer-Tier": "enterprise"
      }
    },
    "actions": {
      "route_to": "gpt-5.2",
      "retry": {
        "max_attempts": 5,
        "initial_delay_ms": 500
      }
    }
  }'

Response

json
{
  "id": "rule_xyz789",
  "name": "cost-optimized",
  "priority": 1,
  "enabled": true,
  "conditions": {
    "models": ["auto"],
    "metadata": {
      "prefer": "cost"
    }
  },
  "actions": {
    "route_to": "gpt-5-mini",
    "fallbacks": ["claude-haiku-4-5-20251015", "gemini-3-flash"]
  },
  "created_at": "2026-02-16T10:00:00Z"
}

Get Routing Rule

Endpoint

GET /v1/management/routing-rules/{rule_id}

Example

bash
curl https://api.gateflow.ai/v1/management/routing-rules/rule_abc123 \
  -H "Authorization: Bearer gw_admin_..."

Update Routing Rule

Endpoint

PATCH /v1/management/routing-rules/{rule_id}

Example

bash
curl -X PATCH https://api.gateflow.ai/v1/management/routing-rules/rule_abc123 \
  -H "Authorization: Bearer gw_admin_..." \
  -H "Content-Type: application/json" \
  -d '{
    "priority": 2,
    "actions": {
      "fallbacks": ["claude-sonnet-4-5-20250929", "gemini-3-pro", "mistral-large-3"]
    }
  }'

Enable/Disable Rule

Endpoint

POST /v1/management/routing-rules/{rule_id}/enable
POST /v1/management/routing-rules/{rule_id}/disable

Example

bash
curl -X POST https://api.gateflow.ai/v1/management/routing-rules/rule_abc123/disable \
  -H "Authorization: Bearer gw_admin_..."

Delete Routing Rule

Endpoint

DELETE /v1/management/routing-rules/{rule_id}

Example

bash
curl -X DELETE https://api.gateflow.ai/v1/management/routing-rules/rule_abc123 \
  -H "Authorization: Bearer gw_admin_..."

Test Routing Rule

Test how a request would be routed.

Endpoint

POST /v1/management/routing-rules/test

Example

bash
curl -X POST https://api.gateflow.ai/v1/management/routing-rules/test \
  -H "Authorization: Bearer gw_admin_..." \
  -H "Content-Type: application/json" \
  -d '{
    "model": "auto",
    "messages": [{"role": "user", "content": "Hello"}],
    "metadata": {
      "prefer": "cost"
    }
  }'

Response

json
{
  "matched_rule": {
    "id": "rule_abc123",
    "name": "cost-optimized"
  },
  "resolved_model": "gpt-5-mini",
  "fallback_chain": ["claude-haiku-4-5-20251015", "gemini-3-flash"],
  "estimated_cost": 0.0001
}

Errors

CodeDescription
400Invalid rule configuration
401Invalid admin key
404Rule not found
409Conflicting rule priority
422Invalid model in route/fallbacks

See Also

Built with reliability in mind.