---
title: "Google GenAI SDK with LiteLLM"
url: "/docs/tutorials/google_genai_sdk"
canonical_url: "https://docs.litellm.ai/docs/tutorials/google_genai_sdk"
type: "docs"
last_updated: "2026-10-03"
summary: "Use Google's official GenAI SDK (JavaScript/TypeScript and Python) with any LLM provider through LiteLLM Proxy."
related:
  - "/docs/tutorials/google_adk"
  - "/docs/tutorials/livekit_xai_realtime"
---
# Google GenAI SDK with LiteLLM

> Index of all LiteLLM docs: https://docs.litellm.ai/llms.txt


Use Google's official GenAI SDK (JavaScript/TypeScript and Python) with any LLM provider through LiteLLM Proxy.

The Google GenAI SDK (`@google/genai` for JS, `google-genai` for Python) provides a native interface for calling Gemini models. By pointing it to LiteLLM, you can use the same SDK with OpenAI, Anthropic, Bedrock, Azure, Vertex AI, or any other provider while keeping the native Gemini request/response format.

## Why Use LiteLLM with Google GenAI SDK?

**Developer Benefits:**
- **Universal Model Access**: Use any LiteLLM-supported model (Anthropic, OpenAI, Vertex AI, Bedrock, etc.) through the Google GenAI SDK interface
- **Higher Rate Limits & Reliability**: Load balance across multiple models and providers to avoid hitting individual provider limits, with fallbacks to ensure you get responses even if one provider fails

**Proxy Admin Benefits:**
- **Centralized Management**: Control access to all models through a single LiteLLM proxy instance without giving developers API keys to each provider
- **Budget Controls**: Set spending limits and track costs across all SDK usage
- **Logging & Observability**: Track all requests with cost tracking, logging, and analytics

| Feature | Supported | Notes |
|---------|-----------|-------|
| Cost Tracking | ✅ | All models on `/generateContent` endpoint |
| Logging | ✅ | Works across all integrations |
| Streaming | ✅ | `streamGenerateContent` supported |
| Virtual Keys | ✅ | Use LiteLLM keys instead of Google keys |
| Load Balancing | ✅ | Via native router endpoints |
| Fallbacks | ✅ | Via native router endpoints |

## Quick Start

### 1. Install the SDK

**JavaScript/TypeScript**

```bash
npm install @google/genai
```

**Python**

```bash
uv add google-genai
```

### 2. Start LiteLLM Proxy

```yaml title="config.yaml" showLineNumbers
model_list:
  - model_name: gemini-3.8-flash
    litellm_params:
      model: gemini/gemini-3.8-flash
      api_key: os.environ/GEMINI_API_KEY
```

```bash
litellm --config config.yaml
```

### 3. Call the SDK through LiteLLM

**JavaScript/TypeScript**

```javascript title="index.js" showLineNumbers
const { GoogleGenAI } = require("@google/genai");

const ai = new GoogleGenAI({
  apiKey: "sk-<your-api-key>",  // LiteLLM virtual key (not a Google key)
  httpOptions: {
    baseUrl: "http://localhost:4000/gemini",  // LiteLLM proxy URL
  },
});

async function main() {
  const response = await ai.models.generateContent({
    model: "gemini-3.8-flash",
    contents: "Explain how AI works",
  });
  console.log(response.text);
}

main();
```

**Python**

```python title="main.py" showLineNumbers
from google import genai

client = genai.Client(
    api_key="sk-<your-litellm-api-key>",  # LiteLLM virtual key (not a Google key)
    http_options={"base_url": "http://localhost:4000/gemini"},  # LiteLLM proxy URL
)

response = client.models.generate_content(
    model="gemini-3.8-flash",
    contents="Explain how AI works",
)
print(response.text)
```

**curl**

```bash
curl "http://localhost:4000/gemini/v1beta/models/gemini-3.8-flash:generateContent?key=sk-<your-litellm-api-key>" \
  -H 'Content-Type: application/json' \
  -X POST \
  -d '{
    "contents": [{
      "parts": [{"text": "Explain how AI works"}]
    }]
  }'
```

## Streaming

**JavaScript/TypeScript**

```javascript title="streaming.js" showLineNumbers
const { GoogleGenAI } = require("@google/genai");

const ai = new GoogleGenAI({
  apiKey: "sk-<your-api-key>",
  httpOptions: {
    baseUrl: "http://localhost:4000/gemini",
  },
});

async function main() {
  const response = await ai.models.generateContentStream({
    model: "gemini-3.8-flash",
    contents: "Write a short poem about the ocean",
  });

  for await (const chunk of response) {
    process.stdout.write(chunk.text);
  }
}

main();
```

**Python**

```python title="streaming.py" showLineNumbers
from google import genai

client = genai.Client(
    api_key="sk-<your-litellm-api-key>",
    http_options={"base_url": "http://localhost:4000/gemini"},
)

response = client.models.generate_content_stream(
    model="gemini-3.8-flash",
    contents="Write a short poem about the ocean",
)

for chunk in response:
    print(chunk.text, end="")
```

## Multi-turn Chat

**JavaScript/TypeScript**

```javascript title="chat.js" showLineNumbers
const { GoogleGenAI } = require("@google/genai");

const ai = new GoogleGenAI({
  apiKey: "sk-<your-api-key>",
  httpOptions: {
    baseUrl: "http://localhost:4000/gemini",
  },
});

async function main() {
  const chat = ai.chats.create({
    model: "gemini-3.8-flash",
  });

  const response1 = await chat.sendMessage({ message: "I have 2 dogs and 3 cats." });
  console.log(response1.text);

  const response2 = await chat.sendMessage({ message: "How many pets is that in total?" });
  console.log(response2.text);
}

main();
```

**Python**

```python title="chat.py" showLineNumbers
from google import genai

client = genai.Client(
    api_key="sk-<your-litellm-api-key>",
    http_options={"base_url": "http://localhost:4000/gemini"},
)

chat = client.chats.create(model="gemini-3.8-flash")

response1 = chat.send_message("I have 2 dogs and 3 cats.")
print(response1.text)

response2 = chat.send_message("How many pets is that in total?")
print(response2.text)
```

## Advanced: Use Any Model with the GenAI SDK

By default, the GenAI SDK talks to Gemini models. But with LiteLLM's router, you can route GenAI SDK requests to **any provider**: Anthropic, OpenAI, Bedrock, etc.

This works by using `model_group_alias` to map Gemini model names to your desired provider models. LiteLLM handles the format translation internally.

:::info

For this to work, point the SDK `baseUrl` to `http://localhost:4000` (without `/gemini`). This routes requests through LiteLLM's native Google endpoints, which go through the router and support model aliasing.

:::

**Anthropic**

Route `gemini-3.8-flash` requests to Claude Sonnet:

```yaml title="config.yaml" showLineNumbers
model_list:
  - model_name: claude-sonnet
    litellm_params:
      model: anthropic/claude-sonnet-5
      api_key: os.environ/ANTHROPIC_API_KEY

router_settings:
  model_group_alias: {"gemini-3.8-flash": "claude-sonnet"}
```

**OpenAI**

Route `gemini-3.8-flash` requests to `gpt-5.6-terra`:

```yaml title="config.yaml" showLineNumbers
model_list:
  - model_name: openai-gpt
    litellm_params:
      model: gpt-5.6-terra
      api_key: os.environ/OPENAI_API_KEY

router_settings:
  model_group_alias: {"gemini-3.8-flash": "openai-gpt"}
```

**Bedrock**

Route `gemini-3.8-flash` requests to Claude on Bedrock:

```yaml title="config.yaml" showLineNumbers
model_list:
  - model_name: bedrock-claude
    litellm_params:
      model: bedrock/us.anthropic.claude-sonnet-5
      aws_access_key_id: os.environ/AWS_ACCESS_KEY_ID
      aws_secret_access_key: os.environ/AWS_SECRET_ACCESS_KEY
      aws_region_name: us-east-1

router_settings:
  model_group_alias: {"gemini-3.8-flash": "bedrock-claude"}
```

**Multi-Provider Load Balancing**

Load balance across Anthropic and OpenAI:

```yaml title="config.yaml" showLineNumbers
model_list:
  - model_name: my-model
    litellm_params:
      model: anthropic/claude-sonnet-5
      api_key: os.environ/ANTHROPIC_API_KEY
  - model_name: my-model
    litellm_params:
      model: gpt-5.6-terra
      api_key: os.environ/OPENAI_API_KEY

router_settings:
  model_group_alias: {"gemini-3.8-flash": "my-model"}
```

Then use the SDK with `baseUrl` pointing to LiteLLM (without `/gemini`):

**JavaScript/TypeScript**

```javascript title="any_model.js" showLineNumbers
const { GoogleGenAI } = require("@google/genai");

const ai = new GoogleGenAI({
  apiKey: "sk-<your-api-key>",
  httpOptions: {
    baseUrl: "http://localhost:4000",  // No /gemini — goes through the router
  },
});

async function main() {
  // This calls Claude/gpt-5.6-terra/Bedrock under the hood via model_group_alias
  const response = await ai.models.generateContent({
    model: "gemini-3.8-flash",
    contents: "Hello from any model!",
  });
  console.log(response.text);
}

main();
```

**Python**

```python title="any_model.py" showLineNumbers
from google import genai

client = genai.Client(
    api_key="sk-<your-litellm-api-key>",
    http_options={"base_url": "http://localhost:4000"},  # No /gemini
)

# This calls Claude/gpt-5.6-terra/Bedrock under the hood via model_group_alias
response = client.models.generate_content(
    model="gemini-3.8-flash",
    contents="Hello from any model!",
)
print(response.text)
```

## Pass-through vs Native Router Endpoints

LiteLLM offers two ways to handle GenAI SDK requests:

| | Pass-through (`/gemini`) | Native Router (`/`) |
|---|---|---|
| **baseUrl** | `http://localhost:4000/gemini` | `http://localhost:4000` |
| **Models** | Gemini only | Any provider via `model_group_alias` |
| **Translation** | None — proxies directly to Google | Translates internally |
| **Cost Tracking** | ✅ | ✅ |
| **Virtual Keys** | ✅ | ✅ |
| **Load Balancing** | ❌ | ✅ |
| **Fallbacks** | ❌ | ✅ |
| **Best for** | Simple Gemini proxy | Multi-provider routing |

## Environment Variable Configuration

You can also configure the SDK via environment variables instead of code:

```bash
# For JavaScript SDK (@google/genai)
export GOOGLE_GEMINI_BASE_URL="http://localhost:4000/gemini"
export GEMINI_API_KEY="sk-<your-litellm-api-key>"

# For Python SDK (google-genai)
# Note: The Python SDK does not support a base URL env var.
# Configure it in code with http_options={"base_url": "..."} instead.
export GEMINI_API_KEY="sk-<your-litellm-api-key>"
```

This is especially useful for tools built on top of the GenAI SDK (like [Gemini CLI](./litellm_gemini_cli.md)).

## Related Resources

- [Gemini CLI with LiteLLM](./litellm_gemini_cli.md)
- [Google AI Studio Pass-Through](../pass_through/google_ai_studio)
- [Google ADK with LiteLLM](./google_adk.md)
- [LiteLLM Proxy Quick Start](../proxy/quick_start)
- [`@google/genai` npm package](https://www.npmjs.com/package/@google/genai)
- [`google-genai` PyPI package](https://pypi.org/project/google-genai/)

## Related pages

- [Google ADK with LiteLLM](https://docs.litellm.ai/docs/tutorials/google_adk.md)
- [LiveKit xAI Realtime Voice Agent](https://docs.litellm.ai/docs/tutorials/livekit_xai_realtime.md)
