Skip to content
agentgateway has joined the Agentic AI FoundationLearn more

For the complete documentation index, see llms.txt. Markdown versions of all docs pages are available by appending .md to any docs URL.

Gemini

Page as Markdown

Send requests through agentgateway in the native Gemini wire format, including streaming and token counting.

Verified Code examples on this page have been automatically tested and verified.

The native Gemini API (models/{model}:generateContent) is the wire format that the Gemini and Vertex AI SDKs send.

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 typeEndpoints
generateContentmodels/{model}:generateContent and models/{model}:streamGenerateContent
geminiCountTokensmodels/{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 custom provider that advertises the generateContent format

A native Gemini request that is routed to any other provider is rejected with an unsupported-conversion error rather than translated.

Note

Prompt guards apply to generateContent and streamGenerateContent. 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.

Route type configuration

In the simplified llm configuration, agentgateway maps the three Gemini path suffixes to their route types, so no explicit route configuration is required.

# yaml-language-server: $schema=https://agentgateway.dev/schema/config
llm:
  models:
  - name: "*"
    provider: gemini
    params:
      apiKey: "$GEMINI_API_KEY"

In the gateways and routes format, set the route types in the policies.ai.routes map. This map is required in this format: traffic that matches no entry is handled as chat completions, and a Gemini request then fails to parse.

# yaml-language-server: $schema=https://agentgateway.dev/schema/config
gateways:
  default:
    port: 4000
routes:
- backends:
  - ai:
      name: gemini
      provider:
        gemini: {}
  policies:
    ai:
      routes:
        ":generateContent": "generateContent"
        ":streamGenerateContent": "generateContent"
        ":countTokens": "geminiCountTokens"
    backendAuth:
      key: "$GEMINI_API_KEY"

The keys are matched as path suffixes, so a leading colon matches the Gemini method suffix whatever version prefix the client sends.

Note

For detailed information about model routing and configuration modes, see Model routing and aliases.

Using the API

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 -X POST 'http://localhost:4000/v1beta/models/gemini-2.5-flash:generateContent' \
  -H 'Content-Type: application/json' \
  -d '{
    "contents": [
      {
        "role": "user",
        "parts": [{"text": "Say hello"}]
      }
    ]
  }'

To estimate the size of a request before you send it, use the :countTokens suffix with the same body.

curl -X POST 'http://localhost:4000/v1beta/models/gemini-2.5-flash:countTokens' \
  -H 'Content-Type: application/json' \
  -d '{
    "contents": [
      {
        "role": "user",
        "parts": [{"text": "How many tokens are in this request?"}]
      }
    ]
  }'

Streaming

Streaming uses the :streamGenerateContent suffix, and requires the alt=sse query parameter.

curl -N 'http://localhost:4000/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, agentgateway rejects the request before it reaches the provider.

{
  "error": {
    "code": 400,
    "message": "streamGenerateContent requires alt=sse; the JSON-array streaming variant is not supported",
    "status": "INVALID_ARGUMENT"
  }
}

For Gemini-specific provider settings, see the Gemini and Vertex AI provider guides.

Was this page helpful?
Agentgateway assistant

Ask me anything about agentgateway configuration, features, or usage.

Note: AI-generated content might contain errors; please verify and test all returned information.

Tip: one topic per conversation gives the best results. Use the + button in the chat header to start a new conversation.

Switching topics? Starting a new conversation improves accuracy.
↑↓ navigate select esc dismiss

What could be improved?

Your feedback helps us improve assistant answers and identify docs gaps we should fix.

Need more help? Join us on Discord: https://discord.gg/y9efgEmppm

Want to use your own agent? Add the Solo MCP server to query our docs directly. Get started here: https://search.solo.io/.