Claude Code with Gemini: A Setup Guide
How to configure Claude Code to use Google's Gemini models through Claude Code Router, avoiding the need for an Anthropic API subscription.
Claude Code is Anthropic's CLI coding assistant—a powerful tool for developers. But what if you want to use it with Google's Gemini models instead of Anthropic's API?
This guide shows you how to set up Claude Code with Gemini using Claude Code Router, an open-source tool that redirects API requests to alternative LLM providers.
Prerequisites
Before starting, ensure you have:
- Node.js 18+ (verify with
node --version) - npm (comes with Node.js)
- Gemini API Key from Google AI Studio
Step 1: Install Claude Code
Install Claude Code globally via npm:
npm install -g @anthropic-ai/claude-code
Verify the installation:
claude --version
Step 2: Install Claude Code Router
Claude Code Router intercepts Claude Code requests and routes them to alternative LLM providers (Gemini, OpenRouter, Ollama, etc.):
npm install -g @musistudio/claude-code-router
Verify:
ccr -v
Step 3: Create Configuration File
Create the config directory and configuration file:
mkdir -p ~/.claude-code-router
Create the config file at ~/.claude-code-router/config.json:
{
"LOG": true,
"Providers": [
{
"name": "gemini",
"api_base_url": "https://generativelanguage.googleapis.com/v1beta/models/",
"api_key": "YOUR_GEMINI_API_KEY_HERE",
"models": [
"gemini-2.0-flash",
"gemini-2.5-flash",
"gemini-2.5-pro"
],
"transformer": {
"use": ["gemini"]
}
}
],
"Router": {
"default": "gemini,gemini-2.0-flash",
"background": "gemini,gemini-2.0-flash",
"think": "gemini,gemini-2.0-flash",
"longContext": "gemini,gemini-2.0-flash"
}
}
Replace YOUR_GEMINI_API_KEY_HERE with your actual Gemini API key.
Step 4: Configure Shell Environment
Add the router activation to your shell profile:
For Zsh (~/.zshrc):
echo '# Claude Code Router' >> ~/.zshrc
echo 'eval "$(ccr activate)"' >> ~/.zshrc
source ~/.zshrc
For Bash (~/.bashrc):
echo '# Claude Code Router' >> ~/.bashrc
echo 'eval "$(ccr activate)"' >> ~/.bashrc
source ~/.bashrc
This sets the following environment variables:
- ANTHROPIC_AUTH_TOKEN: A dummy token for the router
- ANTHROPIC_BASE_URL: Routes requests to local proxy at port 3456
- DISABLE_TELEMETRY: Disables telemetry
Step 5: Start the Router
Start the Claude Code Router server:
ccr start
Verify it's running:
ccr status
You should see output indicating the status is "Running" with the API endpoint at port 3456.
Step 6: Run Claude Code
Launch Claude Code with Gemini:
ccr code
When prompted about the custom API key, select "Yes" to use the router's dummy key.
Gemini Free Tier Limits
Here are the rate limits for Gemini's free tier:
gemini-2.0-flash: 1500 requests per day, 15 per minute (recommended for highest limits)
gemini-2.5-flash: 500 requests per day, 10 per minute
gemini-2.5-pro: 25 requests per day, 2 per minute
Recommendation: Use gemini-2.0-flash as default for the highest free tier limits.
Useful Commands
Here are the key commands you'll use:
ccr start— Start the router serverccr stop— Stop the router serverccr restart— Restart the routerccr status— Check server statusccr code— Launch Claude Code with routerccr model— Interactive model selection/model gemini,gemini-2.5-pro— Switch models within Claude Code
Troubleshooting
"Rate limit exceeded" (429 error)
- Switch to a model with higher limits (e.g., gemini-2.0-flash)
- Wait for rate limit to reset (usually a few minutes)
- Generate a new API key from Google AI Studio
Claude Code shows login screen
- Ensure router is running:
ccr status - Ensure environment is set:
eval "$(ccr activate)" - Restart terminal and try again
Router not starting
Check if port 3456 is already in use:
lsof -i :3456
Kill any existing process and restart:
ccr stop
ccr start
Using Multiple Providers
You can configure multiple providers for fallback or specific use cases. For example, you can use Gemini for default tasks and Ollama for background tasks by adding both to your Providers array and configuring the Router section accordingly.
Router Scenarios
The router supports different scenarios:
- default: General coding tasks
- background: Background/automated tasks
- think: Reasoning-heavy tasks (Plan Mode)
- longContext: Large file/context handling
Resources
- Claude Code Documentation: docs.claude.com
- Claude Code Router GitHub: github.com/musistudio/claude-code-router
- Google AI Studio for API Keys: aistudio.google.com
- Gemini API Rate Limits: ai.google.dev/gemini-api/docs/rate-limits