Skip to main content
Browse docs
By Audience
Getting Started
Configuration
Use Cases
IDE Integration
Third-Party Integrations
Engineering Cache
Console
API Reference
Gateway
Workflow Guides
Templates
Providers and SDKs
Industry Guides
Advanced Guides
Browse by Role
Deployment Guides
In-Depth Guides
Tutorials
FAQ

Google AI Studio (Gemini)

Keeptrusts gateways Google AI Studio's Gemini API with full policy enforcement, audit logging, and automatic format translation. Clients can send requests in standard OpenAI format — Keeptrusts translates them to Google's native Gemini wire format on the fly and translates responses back. Direct Gemini-format requests are also supported natively. Both chat completions and embedding endpoints are proxied.

Use this page when

  • You need the exact command, config, API, or integration details for Google AI Studio (Gemini).
  • You are wiring automation or AI retrieval and need canonical names, examples, and constraints.
  • If you want a guided rollout instead of a reference page, use the linked workflow pages in Next steps.

Primary audience

  • Primary: AI Agents, Technical Engineers
  • Secondary: Technical Leaders

Prerequisites

  1. Google AI Studio API key — obtain one from Google AI Studio.
  2. Keeptrusts CLI — install kt (quickstart guide).
  3. Export your API key:
export GOOGLE_API_KEY="AIzaSy..."

Keeptrusts auto-detects GOOGLE_API_KEY when provider is set to "google-ai-studio". The correct query-parameter auth and Gemini base URL are applied automatically.

Configuration

Create a policy-config.yaml with your provider targets:

pack:
name: gemini-gateway
version: 1.0.0
enabled: true
policies:
chain:
- prompt-injection
- pii-detector
- safety-filter
- audit-logger
policy:
prompt-injection:
threshold: 0.8
action: block
pii-detector:
action: redact
safety-filter:
mode: strict
action: block
audit-logger:
retention_days: 365
providers:
strategy: single
targets:
- id: gemini-25-pro
provider: google-ai-studio
model: gemini-2.5-pro
base_url: https://generativelanguage.googleapis.com
secret_key_ref:
env: GOOGLE_API_KEY

Start the gateway:

kt gateway run \
--listen 0.0.0.0:41002 \
--policy-config policy-config.yaml

Provider Fields

All fields available on a providers.targets[] entry for Google AI Studio:

FieldTypeDefaultDescription
idstringrequiredUnique identifier for this target
providerstringrequiredProvider ID: "google-ai-studio" or "google-ai-studio:chat:gemini-2.5-pro"
modelstringrequiredModel name, e.g. "gemini-2.5-pro", "gemini-2.5-flash"
base_urlstringhttps://generativelanguage.googleapis.comAPI base URL (auto-detected for google-ai-studio)
secret_key_refobjectGOOGLE_API_KEYObject reference to the environment variable holding the API key
timeout_secondsinteger60Maximum time for non-streaming requests
stream_timeout_secondsintegernoneMaximum time for streaming requests; falls back to timeout_seconds
max_context_tokensintegernoneMaximum tokens in the context window (used for context compression)
headersmap{}Additional HTTP headers sent with each request
formatstring"google-gemini"Wire format: "google-gemini" (auto-translates to/from OpenAI)
provider_typestring"google-ai-studio"Explicit provider type; overrides URL heuristic detection
descriptionstringnoneHuman-readable description for dashboards and logs
weightfloat1.0Routing weight for weighted_round_robin strategy
data_policyobjectnoneData handling policy (zero_data_retention, training_opt_out, retention_days)
pricingobjectnoneToken pricing in USD per 1M tokens (prompt, completion)
health_probeobjectnoneActive health probe configuration

Authentication

Google AI Studio uses API-key authentication passed as a query parameter. Keeptrusts auto-detects this when provider is "google-ai-studio":

# These are the defaults — you only need to set secret_key_ref
secret_key_ref:
env: "GOOGLE_API_KEY"
Google AI Studio does not use Authorization: Bearer headers. The key is appended as ?key=<value> in the request URL. Keeptrusts handles this automatically — you never need to construct the query parameter yourself.

For Google Cloud Vertex AI (which uses OAuth2/service account authentication instead of API keys), see the Google Vertex AI integration guide.

Supported Models

ModelContext WindowNotes
gemini-2.5-pro1MMost capable reasoning model, hybrid thinking
gemini-2.5-flash1MFast, cost-effective with thinking capabilities
gemini-2.0-flash1MPrevious-gen fast model
gemini-2.0-flash-lite1MLightweight, lowest cost
gemini-1.5-pro2MLegacy, largest context window
gemini-1.5-flash1MLegacy fast model
gemini-1.5-flash-8b1MLegacy, smallest model

Any model available on the Google AI Studio API can be used — set the model field to the model ID string. Keeptrusts passes the model identifier through to the upstream without validation.

Client Examples

Once the gateway is running, point your client to http://localhost:8080 instead of https://generativelanguage.googleapis.com. Clients send requests in OpenAI format — Keeptrusts translates to Gemini wire format automatically.

from openai import OpenAI

# Use the OpenAI SDK — Keeptrusts translates to Gemini format automatically
client = OpenAI(
base_url="http://localhost:8080/v1",
api_key="unused", # auth is handled by Keeptrusts via GOOGLE_API_KEY
)

response = client.chat.completions.create(
model="gemini-2.5-pro",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Explain the theory of relativity simply."},
],
temperature=0.7,
max_tokens=512,
)

print(response.choices[0].message.content)

Streaming

Keeptrusts fully supports Gemini's streaming mode. Set stream: true in your request — the gateway applies policies to each chunk in real time and translates streaming events between Gemini SSE and OpenAI SSE formats.

Configure a separate streaming timeout to accommodate long-running Gemini generations (especially with thinking models):

pack:
name: google-ai-studio-providers-3
version: 1.0.0
enabled: true
providers:
targets:
- id: gemini-streaming
provider: google-ai-studio
model: gemini-2.5-pro
policies:
chain:
- audit-logger
policy:
audit-logger:
immutable: true
retention_days: 365
log_all_access: true
from openai import OpenAI

client = OpenAI(base_url="http://localhost:8080/v1", api_key="unused")

stream = client.chat.completions.create(
model="gemini-2.5-flash",
messages=[{"role": "user", "content": "Write a short story about AI."}],
stream=True,
)

for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)

Advanced Configuration

Multi-Model Fallback

Automatically fail over from Gemini 2.5 Pro to Flash when the primary is unavailable:

pack:
name: google-ai-studio-providers-4
version: 1.0.0
enabled: true
providers:
targets:
- id: gemini-pro-primary
provider: google-ai-studio
model: gemini-2.5-pro
secret_key_ref:
env: GOOGLE_API_KEY
- id: gemini-flash-fallback
provider: google-ai-studio
model: gemini-2.5-flash
secret_key_ref:
env: GOOGLE_API_KEY
policies:
chain:
- audit-logger
policy:
audit-logger:
immutable: true
retention_days: 365
log_all_access: true

Cross-Provider Fallback

Use Gemini as primary with OpenAI as fallback — format translation is handled automatically for both:

pack:
name: google-ai-studio-providers-5
version: 1.0.0
enabled: true
providers:
targets:
- id: gemini-primary
provider: google-ai-studio
model: gemini-2.5-pro
secret_key_ref:
env: GOOGLE_API_KEY
- id: openai-fallback
provider: openai
model: gpt-4o
secret_key_ref:
env: OPENAI_API_KEY
policies:
chain:
- audit-logger
policy:
audit-logger:
immutable: true
retention_days: 365
log_all_access: true

Format Translation

Keeptrusts automatically translates between OpenAI and Gemini wire formats. Set format: "google-gemini" on the target — clients send standard OpenAI /v1/chat/completions requests and receive OpenAI-shaped responses:

pack:
name: google-ai-studio-providers-6
version: 1.0.0
enabled: true
providers:
targets:
- id: gemini-translated
provider: google-ai-studio
model: gemini-2.5-pro
secret_key_ref:
env: GOOGLE_API_KEY
policies:
chain:
- audit-logger
policy:
audit-logger:
immutable: true
retention_days: 365
log_all_access: true

This means you can swap between OpenAI, Anthropic, and Gemini providers without changing your client code — only the config target changes.

OpenAI ConceptGemini Equivalent
messagescontents with parts
system messagesystemInstruction
toolstools with functionDeclarations
max_tokensmaxOutputTokens
temperaturetemperature
choices[0].messagecandidates[0].content

Latency-Based Routing

Route each request to the provider target with the lowest observed latency:

pack:
name: google-ai-studio-providers-7
version: 1.0.0
enabled: true
providers:
targets:
- id: gemini-pro
provider: google-ai-studio
model: gemini-2.5-pro
secret_key_ref:
env: GOOGLE_API_KEY
- id: gemini-flash
provider: google-ai-studio
model: gemini-2.5-flash
secret_key_ref:
env: GOOGLE_API_KEY
policies:
chain:
- audit-logger
policy:
audit-logger:
immutable: true
retention_days: 365
log_all_access: true

Circuit Breaker

Temporarily remove unhealthy targets from the rotation:

pack:
name: google-ai-studio-providers-8
version: 1.0.0
enabled: true
providers:
targets:
- id: gemini-main
provider: google-ai-studio
model: gemini-2.5-pro
secret_key_ref:
env: GOOGLE_API_KEY
policies:
chain:
- audit-logger
policy:
audit-logger:
immutable: true
retention_days: 365
log_all_access: true

Retry Policy

Retry transient failures automatically:

pack:
name: google-ai-studio-providers-9
version: 1.0.0
enabled: true
providers:
targets:
- id: gemini-pro
provider: google-ai-studio
model: gemini-2.5-pro
secret_key_ref:
env: GOOGLE_API_KEY
policies:
chain:
- audit-logger
policy:
audit-logger:
immutable: true
retention_days: 365
log_all_access: true

Context Compression

Automatically truncate conversation history to fit within the model's context window:

pack:
name: google-ai-studio-providers-10
version: 1.0.0
enabled: true
providers:
targets:
- id: gemini-pro
provider: google-ai-studio
model: gemini-2.5-pro
secret_key_ref:
env: GOOGLE_API_KEY
policies:
chain:
- audit-logger
policy:
audit-logger:
immutable: true
retention_days: 365
log_all_access: true

Zero Data Retention

Enforce that no prompt or completion data is stored by the provider:

pack:
name: google-ai-studio-providers-11
version: 1.0.0
enabled: true
providers:
targets:
- id: gemini-zdr
provider: google-ai-studio
model: gemini-2.5-pro
secret_key_ref:
env: GOOGLE_API_KEY
policies:
chain:
- audit-logger
policy:
audit-logger:
immutable: true
retention_days: 365
log_all_access: true

A/B Testing Between Models

Split traffic across models with weighted routing:

pack:
name: google-ai-studio-providers-12
version: 1.0.0
enabled: true
providers:
targets:
- id: variant-pro
provider: google-ai-studio
model: gemini-2.5-pro
secret_key_ref:
env: GOOGLE_API_KEY
- id: variant-flash
provider: google-ai-studio
model: gemini-2.5-flash
secret_key_ref:
env: GOOGLE_API_KEY
policies:
chain:
- audit-logger
policy:
audit-logger:
immutable: true
retention_days: 365
log_all_access: true

Rate Limiting

Enforce per-provider request rate limits:

pack:
name: google-ai-studio-providers-13
version: 1.0.0
enabled: true
providers:
targets:
- id: gemini-pro
provider: google-ai-studio
model: gemini-2.5-pro
secret_key_ref:
env: GOOGLE_API_KEY
policies:
chain:
- audit-logger
policy:
audit-logger:
immutable: true
retention_days: 365
log_all_access: true

Token Cost Tracking

Declare pricing for cost dashboards and budget alerts:

pack:
name: google-ai-studio-providers-14
version: 1.0.0
enabled: true
providers:
targets:
- id: gemini-pro
provider: google-ai-studio
model: gemini-2.5-pro
secret_key_ref:
env: GOOGLE_API_KEY
policies:
chain:
- audit-logger
policy:
audit-logger:
immutable: true
retention_days: 365
log_all_access: true

Google AI Studio vs Vertex AI

FeatureAI StudioVertex AI
AuthAPI keyOAuth2 / Service Account
Data residencyGoogle-managedGCP project region
Enterprise featuresLimitedFull (VPC-SC, CMEK, etc.)
PricingFree tier availablePay-per-use
Rate limitsLowerHigher (adjustable)

Use Google AI Studio for development and prototyping. Use Vertex AI for production deployments that require data residency, VPC controls, or higher rate limits.

Best Practices

  • Format translation is automatic — use OpenAI SDKs against Gemini endpoints without code changes; only the config target changes.
  • Use stream_timeout_seconds for streaming — Gemini thinking models (2.5 Pro, 2.5 Flash) can take significantly longer than non-thinking models.
  • Set max_context_tokens below the actual model limit to leave headroom for the response. Gemini 2.5 Pro supports 1M tokens but you should set max_context_tokens to ~900000.
  • Enable health probes on production targets so routing strategies can react to Google API outages.
  • Use data_policy to document and enforce your organization's data handling requirements — especially important for enterprise Gemini usage.
  • Prefer fallback strategy for critical workloads; use latency or weighted_round_robin for cost/performance optimization.
  • Separate API keys per environment — use distinct secret_key_ref values for dev, staging, and production.
  • Declare pricing even if approximate — it enables cost dashboards and per-request budget enforcement.
  • Consider Vertex AI for enterprise deployments that require OAuth2/service account auth, VPC-SC, or CMEK encryption — see the Google Vertex AI guide.

For AI systems

  • Canonical terms: Keeptrusts gateway, Google AI Studio, Gemini, Gemini API, provider target, policy-config.yaml, provider: "google-ai-studio", GOOGLE_AI_API_KEY.
  • Config field names: provider, model, base_url, secret_key_ref.env: "GOOGLE_AI_API_KEY", format, provider_type: "google-ai-studio", pricing.
  • Provider shorthand: google-ai-studio:chat:<model> (e.g., google-ai-studio:chat:gemini-2.0-flash).
  • Key behavior: Keeptrusts translates between OpenAI format and Google's Gemini API, handling API key auth.
  • Best next pages: Google Vertex AI integration (enterprise tier), OpenAI integration, Policy configuration.

For engineers

  • Prerequisites: Google AI Studio API key (GOOGLE_AI_API_KEY from aistudio.google.com), kt CLI installed.
  • Start command: kt gateway run --listen 0.0.0.0:41002 --policy-config policy-config.yaml.
  • Validate: curl http://localhost:8080/v1/chat/completions -H 'Content-Type: application/json' -d '{"model":"gemini-2.0-flash","messages":[{"role":"user","content":"hello"}]}'.
  • Google AI Studio uses API key auth (simpler than Vertex AI's OAuth2/service account flow).
  • For enterprise features (VPC-SC, CMEK, service accounts), use Google Vertex AI instead.
  • Declare pricing fields for cost dashboard accuracy even if approximate.

For leaders

  • Google AI Studio is the consumer/developer tier — faster to set up but lacks enterprise controls (VPC-SC, CMEK, IAM) available in Vertex AI.
  • Suitable for prototyping, development, and non-regulated workloads where API key auth is acceptable.
  • Gemini models offer competitive pricing and multimodal capabilities (text, image, video, audio).
  • For regulated or production workloads, evaluate Google Vertex AI for its enterprise security controls.

Next steps