Configure model routing
This page describes how to configure, deploy, and test model routing in API Gateway using OpenAPI 3.x specifications.
Before you begin
Before configuring model routing, check that your environment meets the following prerequisites:
- Check IAM permissions: Check that you have access to the API Gateway Management Plane and Gemini Enterprise Agent Platform Model Garden. You must have the API Gateway Admin (
roles/apigateway.admin) role to create API configs and gateways. In addition, the service account used by your API gateway—either the default Compute Engine service account or a user-managed service account specified when creating the API config—must be granted the Agent Platform User (roles/aiplatform.user) role to access target models. - Check model availability and endpoint access: Check that your routable models are pre-deployed open models for Model as a Service (MaaS) in Agent Platform Model Garden. All models referenced by a single router must share the exact same hostname. Pick either the global endpoint (
aiplatform.googleapis.com) or a single regional endpoint (for example,us-central1-aiplatform.googleapis.com) for every model referenced within that router. - Check gateway deployment eligibility: You cannot update an existing gateway deployed without model routing to enable model routing, nor can you update a gateway deployed with model routing to disable or remove model routing. To switch routing modes, you must create and deploy a new API config and gateway instance.
- Check VPC Service Controls and endpoint compatibility: Model routing gateways do not support VPC Service Controls or Private Service Connect (PSC) endpoint configurations. Check that your target project and API Gateway instances are not restricted by VPC Service Controls perimeters, and that your models use public regional or global endpoints.
Configuration validation
When you deploy an API config, the API Gateway management plane validates your OpenAPI specification. The management plane rejects invalid configurations during deployment with an informational validation error. The validation process enforces the following rules:
Structural and location checks
- The
x-google-api-managementextension and its associated blocks (backends,ai.models.routing.routers, individual routers, andrules) must be well-formed. Keys must match their expected data types (map, list, or string). The management plane rejects type mismatches with anexpected map/list/stringerror. - The
x-google-api-managementextension must contain a validbackendsblock when model routing is enabled. - The
x-google-model-routerextension is supported only in OpenAPI 3.x specifications (it isn't supported in OpenAPI 2.0 / Swagger). - The
x-google-model-routerextension can only be specified at the operation level. The management plane explicitly rejectsx-google-model-routerdefinitions placed at the path level or root (top) level. - The
ai.models.routing.routersblock must be defined insidex-google-api-managementwhenever any operation referencesx-google-model-router. - You cannot specify both
x-google-model-routerandx-google-backendon the same API operation. - An OpenAPI specification cannot contain a mix of model routing and non-model routing operations. You cannot specify standard routing extensions (such as
x-google-backend) on some operations while usingx-google-model-routeron other operations within the same API specification.
HTTP method check
- The
x-google-model-routerextension can only be applied to operations using thePOSTHTTP method. The management plane rejects model routing on any other HTTP method (such asGET,PUT, orDELETE).
Backend validity
- Every backend defined under
x-google-api-management.backendsmust include a non-emptyaddressfield. - The backend
addressmust be a valid URL using thehttporhttpsscheme. To protect prompt payloads and authentication credentials in transit across public or remote endpoints, always specify thehttpsscheme when defining theaddressfield. - Every backend defined under
x-google-api-management.backendsand referenced by a model router must usepathTranslation: CONSTANT_ADDRESS. The management plane rejects configurations usingpathTranslation: APPEND_PATH_TO_ADDRESSfor model routing backends because path translation is ignored in the model router's runtime path. - Model routing backends don't support VPC Service Controls or Private Service Connect (PSC) endpoint configurations. All backend
addressfields must point to public regional or global MaaS open model endpoints.
Router reference resolution
- The router name referenced by an operation's
x-google-model-routermust match a valid router key defined underai.models.routing.routers. - The
backendreferenced by a router'sdefaultModelmust match a valid backend defined underx-google-api-management.backends. - The
backendreferenced by each rule in a router must match a valid backend defined underx-google-api-management.backends.
Router contents
- Each router must define a
defaultModel. - The
defaultModelmust include a validbackendfield. - The
defaultModelmust include a non-emptytargetModelfield. - Each entry under
rulesmust include a non-emptymodelfield. The string valuedefaultis reserved and cannot be used as a rule'smodelvalue. - Each entry under
rulesmust include a non-emptytargetModelfield. - The
modelvalues defined across all rules within a single router must be unique. The management plane rejects duplicatemodelvalues within the same router.
Backend host and scheme consistency
- All backends referenced by a single router (including
defaultModel.backendand every rule'sbackend) must share the identical hostname and URL scheme. The management plane rejects configurations with differing hostnames or inconsistent schemes (httpversushttps) within the same router, ensuring the router dispatches all requests to a consistent upstream service endpoint.
Target model validation
- The
<provider>portion of thetargetModelstring (google,openai, oranthropic) and the<provider>/<model>identifier format are both validated at config-create (deploy) time. The management plane rejects atargetModelthat isn't formatted as<provider>/<model>or whose provider isn'tgoogle,openai, oranthropicwith anInvalidArgument: unsupported publishererror during deployment.
Step 1: Identify target models
Identify the target foundation models and their corresponding Agent Platform endpoint URLs. All routable models within a router must share a single hostname (for MaaS open models, this hostname is aiplatform.googleapis.com).
Endpoint URL paths vary based on the model provider:
- Google Gemini: Uses the
:generateContentmethod. - Anthropic Claude: Uses the
:rawPredictmethod. - OpenAI: Uses the
/endpoints/openapi/chat/completionsendpoint path.
The following table lists the MaaS endpoints used in the OpenAPI specification example later in this section:
| Model | Endpoint URL |
|---|---|
google/gemini-3.5-flash-lite |
https://aiplatform.googleapis.com/v1/projects/YOUR_PROJECT_ID/locations/global/publishers/google/models/gemini-3.5-flash-lite:generateContent |
anthropic/claude-opus-4-7 |
https://aiplatform.googleapis.com/v1/projects/YOUR_PROJECT_ID/locations/global/publishers/anthropic/models/claude-opus-4-7:rawPredict |
openai/gpt-oss-120b-maas |
https://aiplatform.googleapis.com/v1/projects/YOUR_PROJECT_ID/locations/global/endpoints/openapi/chat/completions |
Replace YOUR_PROJECT_ID with your Google Cloud project ID.
Step 2: Configure the OpenAPI 3.x specification
Create or update your OpenAPI 3.x specification to define your backend endpoints and model routing configurations.
The following example demonstrates an OpenAPI 3.0.3 specification defining two distinct model routers. To prevent horizontal scrolling, long backend address URLs use YAML double-quoted multi-line string continuation (``):
openapi: 3.0.3
info:
title: OpenAPI 3.x spec using Model Routing
description: Using Model Routing in an OAS 3.x spec
version: 1.0.0
x-google-api-management:
backends:
gemini-35-flashlite:
address: "https://aiplatform.googleapis.com/v1/projects/\
YOUR_PROJECT_ID/locations/global/publishers/google/\
models/gemini-3.5-flash-lite:generateContent"
deadline: 60.0
pathTranslation: CONSTANT_ADDRESS
anthropic-claude-opus-47:
address: "https://aiplatform.googleapis.com/v1/projects/\
YOUR_PROJECT_ID/locations/global/publishers/anthropic/\
models/claude-opus-4-7:rawPredict"
deadline: 60.0
pathTranslation: CONSTANT_ADDRESS
openai-gpt-oss-120b:
address: "https://aiplatform.googleapis.com/v1/projects/\
YOUR_PROJECT_ID/locations/global/endpoints/openapi/\
chat/completions"
deadline: 60.0
pathTranslation: CONSTANT_ADDRESS
ai:
models:
routing:
routers:
# Router 1: route between Gemini (default) and Claude.
gemini-claude-router:
defaultModel:
backend: gemini-35-flashlite
targetModel: google/gemini-3.5-flash-lite
rules:
- model: "claude-opus-4-7"
backend: anthropic-claude-opus-47
targetModel: anthropic/claude-opus-4-7
# Router 2: route between OpenAI GPT (default) and Gemini.
openai-gemini-router:
defaultModel:
backend: openai-gpt-oss-120b
targetModel: openai/gpt-oss-120b-maas
rules:
- model: "gemini-3.5-flash-lite"
backend: gemini-35-flashlite
targetModel: google/gemini-3.5-flash-lite
servers:
- url: "https://my-gateway-url.com"
paths:
/v1/chat/gemini-claude:
post:
summary: "Endpoint:defaults to Gemini & Claude as an option."
operationId: "chatGeminiClaude"
x-google-model-router: gemini-claude-router
responses:
'200':
description: "OK"
/v1/chat/openai-gemini:
post:
summary: "Endpoint:defaults to OpenAI & Gemini as an option."
operationId: "chatOpenAIGemini"
x-google-model-router: openai-gemini-router
responses:
'200':
description: "OK"
Configuration properties
backends: Thebackendsobject underx-google-api-managementdefines all routable model endpoints. Each backend name represents a symbolic model name (for example,gemini-35-flashlite) containing the destinationaddress. Thebackendsfield is an existing Google OpenAPI extension.ai.models.routing: The model routing configuration resides underx-google-api-managementasai.models.routing, containing a map of named routers. Each map entry defines one model router, where the key represents the router's name (for example,gemini-claude-router) and the value contains:defaultModel: The required fallback model destination used when an incoming request payload doesn't match any explicit rule. It shares the exact structure of a rule entry but omits themodelmatching field. For OpenAI-compatible routes, when a request falls back todefaultModel, the value oftargetModelis forwarded as the outgoingmodelattribute in the request body sent to Agent Platform.rules: An optional array where each element maps a client-payload model string to a destination backend and target model.
- Rule properties: Each entry within
rules(and thedefaultModel) defines the following properties:model(rules only): The string value matched against themodelattribute within the client's incoming JSON prompt payload. The router compares the incoming payload'smodelvalue to this string. If no rule matches, the router selects thedefaultModel. For OpenAI-compatible routes (where the destination backend is/openapi/chat/completions), this string is forwarded directly as the outgoingmodelattribute in the request body sent to Agent Platform. Therefore, for OpenAI-compatible routes, the