For the complete documentation index, see llms.txt. Markdown versions of all docs pages are available by appending .md to any docs URL.
Gemini
Send requests through agentgateway in the native Gemini wire format, including streaming and token counting.
Serve clients that send the native Gemini wire format, such as the Gemini and Vertex AI SDKs, through agentgateway.
About
The Gemini API addresses a model through the request path rather than the request body, as models/{model}:generateContent. A client that is built on the Gemini or Vertex AI SDK sends this format directly, so pointing that client at agentgateway needs no OpenAI-compatible shim.
Two route types carry the format:
| Route type | Endpoints |
|---|---|
GenerateContent | models/{model}:generateContent and models/{model}:streamGenerateContent |
GeminiCountTokens | models/{model}:countTokens |
Because the model name comes from the path, any Gemini model works without a per-model entry in your configuration. The tunedModels/{model} form is preserved as well.
Native Gemini requests reach Gemini-family backends only:
- The Gemini API
- Vertex AI with a Gemini model
A native Gemini request that is routed to any other provider is rejected with an unsupported-conversion error rather than translated.
Note
Guardrails apply to GenerateContent. They are skipped for GeminiCountTokens, which counts tokens and never reaches a model. Thinking configuration in generationConfig.thinkingConfig, returned thought parts, and thoughtsTokenCount all pass through unchanged.
Before you begin
- Install and set up an agentgateway proxy.
- Set up access to the Gemini or Vertex AI LLM provider.
Step 1: Add the Gemini route types
Create an AgentgatewayBackend that maps the Gemini method suffixes to their route types. The default behavior routes all traffic as Completions, so the native Gemini paths must be mapped explicitly.
If you already set up multiple endpoints, add these paths to your existing AgentgatewayBackend.
kubectl apply -f- <<EOF
apiVersion: agentgateway.dev/v1alpha1
kind: AgentgatewayBackend
metadata:
name: google-native
namespace: agentgateway-system
spec:
ai:
provider:
gemini: {}
policies:
auth:
secretRef:
name: google-secret
ai:
routes:
":generateContent": "GenerateContent"
":streamGenerateContent": "GenerateContent"
":countTokens": "GeminiCountTokens"
EOFReview the following table to understand this configuration.
| Setting | Description |
|---|---|
ai.provider.gemini | Define the Gemini provider. Leave model unset so that the model from the request path is used. |
policies.auth | The authentication token to use to authenticate to the LLM provider. The example refers to the google-secret secret from the Gemini provider setup. |
policies.ai.routes | Map each Gemini method suffix to its route type. The keys are matched as path suffixes, so the method suffix on its own matches whatever version prefix the client sends. |
Step 2: Create an HTTPRoute
Create an HTTPRoute that routes the native Gemini paths to the AgentgatewayBackend.
kubectl apply -f- <<EOF
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: google-native
namespace: agentgateway-system
spec:
parentRefs:
- name: agentgateway-proxy
namespace: agentgateway-system
rules:
- matches:
- path:
type: PathPrefix
value: /v1beta/models
backendRefs:
- name: google-native
namespace: agentgateway-system
group: agentgateway.dev
kind: AgentgatewayBackend
EOFStep 3: Send a native Gemini request
Send a request to models/{model} with the Gemini method suffix. Agentgateway forwards the body unchanged and returns the Gemini response shape to the client.
curl "$INGRESS_GW_ADDRESS/v1beta/models/gemini-2.5-flash:generateContent" \
-H content-type:application/json \
-d '{
"contents": [{"role": "user", "parts": [{"text": "Say hello"}]}]
}' | jqTo estimate the size of a request before you send it, use the :countTokens suffix with the same body.
Step 4: Stream a response
Streaming uses the :streamGenerateContent suffix, and requires the alt=sse query parameter.
curl -N "$INGRESS_GW_ADDRESS/v1beta/models/gemini-2.5-flash:streamGenerateContent?alt=sse" \
-H content-type:application/json \
-d '{
"contents": [{"role": "user", "parts": [{"text": "Count to five"}]}]
}'Without alt=sse, the Gemini API streams a JSON array instead of server-sent events, which agentgateway cannot parse incrementally. Rather than fail partway through a response, the request is rejected before it reaches the provider.
{
"error": {
"code": 400,
"message": "streamGenerateContent requires alt=sse; the JSON-array streaming variant is not supported",
"status": "INVALID_ARGUMENT"
}
}Cleanup
You can remove the resources that you created in this guide.kubectl delete httproute google-native -n agentgateway-system
kubectl delete AgentgatewayBackend google-native -n agentgateway-system