Create an AI Model Provider, and AI Model, and an AI Policy with all of your configuration
- The AI Model Provider entity defines your connection and stores your authentication credentials.
- The AI Request Transformer Policy removes extra fields that Gemini’s API does not support.
- The AI Model entity declares which upstream models are available, configures how client requests are routed, and specifies which AI Model Provider to use.
kongctl apply -f - --auto-approve --pat "$KONNECT_TOKEN" << 'EOF'
ai_gateway_model_providers:
- ref: my-gemini-account
ai_gateway: !lookup {id: !env AI_GATEWAY_ID}
name: my-gemini-account
display_name: "my-gemini-account"
type: gemini
config:
auth:
type: basic
headers:
- name: x-goog-api-key
value: !secret {source: !env GEMINI_API_KEY}
ai_gateway_policies:
- ref: strip-claude-beta-info
ai_gateway: !lookup {id: !env AI_GATEWAY_ID}
name: strip-claude-beta-info
display_name: "strip-claude-beta-info"
type: request-transformer-advanced
config:
remove:
headers:
- anthropic-beta
- authorization
- x-api-key
querystring:
- beta
body:
- output_config
- context_management
- mcp_servers
- container
- service_tier
- reasoning_effort
ai_gateway_models:
- ref: my-claude-gemini
ai_gateway: !lookup {id: !env AI_GATEWAY_ID}
name: my-claude-gemini
display_name: "my-claude-gemini"
type: model
formats:
- type: anthropic
config:
route:
paths:
- /
model:
body_param: model
values:
- my-claude-gemini
targets:
- name: gemini-2.5-flash
provider: !ref my-gemini-account#name
config:
type: gemini
policies:
- !ref strip-claude-beta-info#name
capabilities:
- generate
EOF
The AI Model Provider uses the following settings:
type: gemini: Specifies that this provider connects to the Gemini service using Gemini’s standard API format.
name: my-gemini-account: A unique identifier that AI Models will reference to route requests through this provider.
config.auth: Stores your Gemini API key. value: !secret {source: !env GEMINI_API_KEY} loads the value from your environment at apply time instead of embedding it in the YAML, and kongctl redacts it in plan and diff output. AI Gateway securely manages this credential and injects it into upstream requests automatically, eliminating the need for clients to pass API keys.
The AI Policy uses the following settings:
type: request-transformer-advanced: Modifies requests before AI Gateway forwards them upstream.
config.remove.headers: Removes the anthropic-beta, authorization, and x-api-key headers.
config.remove.querystring: Removes the beta query string parameter.
config.remove.body: Removes the output_config, context_management, mcp_servers, container, service_tier, and reasoning_effort body fields.
- Don’t strip
model: AI Gateway uses that field to select the target, and removing it breaks routing.
- Claude Code beta features vary by version and may add other incompatible fields over time. If you still see an error mentioning an unexpected field after applying this Policy, add that field to the appropriate
remove list and re-apply.
The AI Model uses the following settings:
type: model: Specifies this is a synchronous model for request/response workloads.
name: my-claude-gemini: A unique identifier for this model.
formats: [type: anthropic]: Declares that this model accepts requests in Anthropic-compatible format, matching what Claude Code sends natively, even though the upstream model is Gemini.
config.route.paths: [/]: Configures the custom base path where this model’s routes will be accessible. Setting this to a unique value avoids clashes when you have multiple AI Models.
capabilities: [generate]: Enables the text generation capability. For a model using the anthropic format, the generate capability creates a /messages endpoint matching Anthropic’s native Messages API, so combined with your base path, clients send requests to /v1/messages.
targets: Specifies which upstream AI Model Provider model to route requests to. Here, provider: !ref my-gemini-account#name references the AI Model Provider we created earlier, and name: gemini-2.5-flash specifies which Gemini model to call upstream.
policies: [!ref strip-claude-beta-info#name]: Attaches the AI Policy created earlier so it applies to every request to this AI Model.