Request flow
When you send a request to your AI Gateway endpoint:- Your app sends a request with your AI Gateway API Key to your ngrok endpoint
- The gateway validates your key and injects ngrok’s managed provider keys
- The gateway selects which models to try based on your configuration and the request
- Unsupported parameters are stripped if needed from the request body based on the selected provider/model
- The request is forwarded to the provider with the appropriate provider API key
- If it fails, the gateway retries with the next model or key in the list
- The response is returned to your app
Authentication
The gateway supports two authentication paths:AI Gateway API Keys (recommended)
When you use an AI Gateway API Key, ngrok handles provider authentication automatically:- Your app sends a request with the AI Gateway API Key as
Authorization: Bearer ng-xxxxx-g1-xxxxx - The gateway validates the key
- ngrok’s managed provider keys are injected for supported providers (currently OpenAI and Anthropic)
- The request is forwarded to the provider
Bring Your Own Keys (BYOK)
When you bring your own provider keys, you configure them in your Traffic Policy:Model selection
The gateway needs to determine which model and provider to use for each request. This happens in two stages: resolving what the client asked for, then selecting from available options.Resolving the client’s request
The model name in your request determines the starting point:| Model in Request | What Happens |
|---|---|
gpt-4o | Known OpenAI model in the model catalog |
claude-3-5-sonnet-latest | Known Anthropic model in the model catalog |
openai:gpt-4o | Provider and model match known OpenAI model |
openai:gpt-5-preview | Known provider but unknown model, so passed through to OpenAI as-is |
my-provider:my-model | Uses your configured custom provider |
ngrok/auto | Let the gateway choose based on your selection strategy |
Unknown models (not in the catalog) are automatically passed through if you include a known provider prefix. This lets you use new models immediately without waiting for catalog updates.
Building the set of available options
The gateway builds a set of available options based on your configuration and the request path. Starting from the model catalog, the gateway adds any custom models from your configuration to the set of available options. Then the gateway uses the request path to determine the API format and filters out providers that don’t support it. For example, if the request path is/v1/chat/completions, only providers that support the OpenAI API format are included; likewise for /v1/messages and providers that support the Anthropic Claude API format.
Custom selection strategies
You can completely customize how models are selected using CEL expressions. Define amodel_selection strategy to control the order models are tried:
The example below uses BYOK provider keys. When using AI Gateway API Keys, provider keys are handled automatically.
traffic-policy.yaml
- Cost optimization - Route to cheapest models first
- Provider preference - Prefer certain providers over others
- Load balancing - Randomize across equivalent models
- Capability filtering - Select models with specific features
Parameter compatibility
Different AI providers and models support different sets of request parameters. The gateway can remove parameters that a provider or model doesn’t support before forwarding the request, preventing errors caused by unsupported fields.How it works
Parameter removal happens in two independent passes before each request is forwarded. Each pass only runs if the relevant configuration is present. If neither is configured, no filtering occurs.- Provider-level (allowlist): If the matched surface entry has a non-empty
supported_paramslist, any top-level request body parameters not on that list are stripped. - Model-level (denylist): If the model has
unsupported_paramsentries, those specific parameters are stripped regardless of what the provider surface allows.
Parameter removal only applies to top-level request body fields. Nested structures are not inspected.
Official providers
For official (built-in) providers and models in the ngrok model catalog, parameter compatibility information is maintained automatically. No configuration is required.Custom providers and models
Custom providers and models have no parameter compatibility information by default, so no filtering occurs unless you configure it. You can opt in using either or both mechanisms:traffic-policy.yaml
Failover
When a request fails, the gateway automatically tries the next candidate. Your app receives a successful response, or a final error if all candidates are exhausted.What triggers failover?
- Timeouts - Provider took too long to respond
- HTTP errors - Any non-2xx/3xx response (4xx, 5xx)
- Connection failures - Network errors, DNS issues, etc.
Failover order
The gateway works through your configured options: For example, if you configure OpenAI with 2 keys and Anthropic as backup:- OpenAI with key #1
- OpenAI with key #2
- Anthropic
Timeouts
Two settings control how long the gateway waits:| Setting | Default | Description |
|---|---|---|
per_request_timeout | 3m | Max time for a single attempt |
total_timeout | 6m | Max time including all failover attempts |
traffic-policy.yaml
per_request_timeout, the gateway moves to the next option. If total time exceeds total_timeout, the gateway returns an error to your app.
Token counting
The gateway counts tokens for each request, enabling:- Usage tracking - See token usage per provider and model
- Input limits - Reject oversized requests before they’re sent to providers
traffic-policy.yaml
Content modification
You can modify requests and responses using Traffic Policy’s find and replace actions (request-body-find-replace, response-body-find-replace, sse-find-replace). This enables use cases like:
- PII redaction - Remove sensitive data before it reaches AI providers
- Response sanitization - Filter inappropriate content from responses
- Prompt injection - Add system instructions to user prompts
traffic-policy.yaml
Modifying Requests
Redact PII, inject prompts, add headers
Modifying Responses
Sanitize responses and streaming content
Next steps
AI Gateway API Keys
Create keys to authenticate requests
Configuring Providers (BYOK)
Set up providers and provider API keys
Model Selection Strategies
Define custom routing logic