How to Point Claude Code at a Free Gemini API Key (and What You Lose)
24 December 2026 · Updated 28 November 2026 · 11 min read · by Yang Kai
Claude Code has no official Gemini mode — but a local router can translate its requests to a free Gemini API key. The exact setup from Anthropic's env-var docs and the router's repo, what breaks in translation, and when the trade is worth it.
Config inside: Router setup for Claude Code on a Gemini key: env vars, config shape, and the breakage list
Contents
- Why a translator is needed at all
- The official half: Claude Code's routing variables
- The router: Claude Code Router
- What you need before you start
- Step 1: Install the router and write the config
- Step 2: Start it and verify the route
- What you lose: the honest list
- When this is worth it, and when it is not
- Troubleshooting: the three failures everyone meets
- How we researched this
- Conclusion
Claude Code is the tool people want to learn, and its meter is the reason they hesitate. A free Gemini API key, meanwhile, sits in AI Studio costing nothing. The question shows up in every free-credit thread: can the first run on the second?
It can — with a translator in the middle, and with real losses you should know about before you build the habit. This page gives the working method: the environment variables from Anthropic’s own documentation, the open-source router that does the translation, the config shape, and then the honest half — what breaks, what silently degrades, and the two jobs this setup is genuinely good for. Every mechanism below is sourced from Anthropic’s Claude Code documentation and the router project’s own repo, checked November 2026.
The plain statement first: there is no official Gemini mode for Claude Code. Anthropic’s supported third-party routes run Claude models on Bedrock, Vertex AI and similar platforms. Pointing Claude Code at Gemini means a community router translating between two API formats, and Anthropic does not support non-Claude backends. Everything after this paragraph is a workaround — a good one, within its limits.
Why a translator is needed at all
Claude Code speaks one protocol: the Anthropic Messages API. It sends requests in that format and expects answers — including tool calls — in that format. Gemini’s API speaks Google’s own format (plus an OpenAI-compatible endpoint, which is still not Anthropic’s format). Point ANTHROPIC_BASE_URL straight at Google’s endpoint and the requests simply fail: the shapes do not match.
So the setup has three parts:
- Claude Code’s base-URL setting — the documented switch that sends its requests somewhere other than Anthropic.
- A local router — a small proxy on your own machine that receives Messages-format requests and re-issues them in Gemini’s format.
- A free Gemini API key — the router holds it and attaches it to the translated requests.
The official half: Claude Code’s routing variables
Anthropic documents the redirection mechanism itself, in the environment variables reference and the LLM gateway guide. The variables that matter:
| Variable | What it does |
|---|---|
ANTHROPIC_BASE_URL |
The endpoint Claude Code sends requests to (it appends /v1/messages itself) |
ANTHROPIC_AUTH_TOKEN |
Sent as an Authorization: Bearer header — the credential for whatever sits at that URL |
ANTHROPIC_API_KEY |
Sent as an x-api-key header; takes precedence once approved, so a leftover real key hijacks a router setup |
ANTHROPIC_MODEL |
Pins the model for the session |
ANTHROPIC_DEFAULT_HAIKU_MODEL / _SONNET_MODEL / _OPUS_MODEL |
The models behind Claude Code’s internal aliases — background and helper calls use these |
Variables can be exported in your shell or set under the env key in ~/.claude/settings.json, where they apply to every session. These variables are designed for gateways that still serve Claude models. The router in the next section is what makes a non-Claude backend answer them.
The router: Claude Code Router
The well-known open-source implementation is Claude Code Router (github.com/musistudio/claude-code-router, installed as @musistudio/claude-code-router). It runs as a local proxy — by default at 127.0.0.1:3456 — and its job is translation plus routing:
- Providers are configured with an API base URL, a key, a model list and a transformer — the component that converts Anthropic-format requests into the provider’s format and converts the response back. Gemini is one of its supported providers, alongside OpenRouter, DeepSeek, Ollama and others.
- The Router block decides which provider and model handles each request class:
default,background,think,longContextandwebSearch. That split matters for a free key, as the next sections show. - You launch Claude Code through it with
ccr code, which sets the base URL and token for the session, and you can switch models mid-session with/model provider,model.
What you need before you start
- Claude Code installed (
npm install -g @anthropic-ai/claude-code) and working on its default settings once, so you know what normal feels like. - Node.js 18 or later, which the router also needs.
- A free Gemini API key from Google AI Studio. The free tier covers the Flash and Flash-Lite models; your exact limits are per project and shown only inside AI Studio — the full picture, including the data term, is in the Gemini free tier limits guide.
- Ten minutes and a throwaway project to test in. Do not make a real codebase your first routed session.
Step 1: Install the router and write the config
Install both tools, then create the router’s config at ~/.claude-code-router/config.json. The shape, following the repo’s documented structure with the key kept in an environment variable rather than the file:
{
"Providers": [
{
"name": "gemini",
"api_base_url": "https://generativelanguage.googleapis.com/v1beta/models/",
"api_key": "$GEMINI_API_KEY",
"models": ["gemini-3.8-flash"],
"transformer": { "use": ["gemini"] }
}
],
"Router": {
"default": "gemini,gemini-3.8-flash",
"background": "gemini,gemini-3.8-flash",
"longContext": "gemini,gemini-3.8-flash"
}
}
Two details in that config carry the whole setup. The transformer line is the translation layer — without it, Gemini receives Anthropic-shaped JSON and rejects it. And the background route is not optional decoration: Claude Code makes constant small helper calls (file scanning, summarising, title generation) through its Haiku alias, and on a routed setup those calls need a destination too, or they fail in ways that look like random breakage.
If JSON by hand is not your idea of fun, the router also ships a small web UI — ccr ui — that edits the same providers and routes through a browser form and can import preset templates. The file remains the source of truth either way, and it is the file you will want in version control once the setup works.
Step 2: Start it and verify the route
Export your Gemini key, start a session through the router, and check where requests are going:
export GEMINI_API_KEY=<your key>
ccr code
Inside the session, /status shows the base URL Claude Code is using — it should name the local router, not Anthropic. Ask for something small and verifiable: explain one file, then make one trivial edit. A routed setup that answers chat but fumbles tool calls is the common half-working state, and you want to meet it on a test project.
Watch out: if you have ever exported a real ANTHROPIC_API_KEY, unset it for routed sessions. Claude Code gives that key precedence, and the failure mode — requests reaching the router carrying the wrong credential, or bypassing your intent entirely — wastes an hour the first time it happens to anyone.
What you lose: the honest list
The translation works. It is also lossy, in specific places that matter more the more serious the work gets:
| Claude Code feature | On a Gemini backend |
|---|---|
| Tool use (edit, bash, search) | Works through the transformer, but Gemini’s function calling is a different dialect; malformed or drifting tool calls are the most common complaint |
| Prompt caching | Anthropic’s cache markers do not carry over. Gemini has its own caching model, and the router does not map Claude Code’s cache breakpoints onto it — long sessions re-send context you would have cached |
| Extended thinking | Claude’s thinking blocks and effort controls have no Gemini equivalent in the translation; reasoning behaviour is whatever the Gemini model does by default |
| Context window handling | Claude Code assumes a window size for models it does not recognise (gateway docs note a 200K default assumption), so it may compact far earlier than Gemini’s larger window requires — or misjudge in the other direction |
| Model behaviour | Claude Code’s system prompts and tool descriptions are tuned against Claude models. Gemini follows them noticeably differently: more literal in places, looser in others |
| Support | None. If a routed session corrupts a workflow, the answer from every vendor involved is that the configuration is unsupported |
Add the free tier’s own terms, which apply no matter what sits in front of the key: free-tier content may be used to improve Google’s products, and the unpaid tier is not offered for serving users in the EEA, Switzerland or the UK. A routed Claude Code session sends your code through that tier. For learning projects that is a fair trade. For a client’s codebase it is not yours to make.
There is also a second route you will see recommended — transformers that borrow a Gemini CLI login instead of an API key. Treat it as what it is: an unofficial use of a consumer login, outside the documented API path, with account risk attached. The API-key route above is the defensible one.
When this is worth it, and when it is not
Worth it:
- Learning Claude Code’s workflow — the interface, the slash commands, the CLAUDE.md habits — before paying for the real thing. Skills transfer; the muscle memory is the valuable part. Pair it with the free habits at the bottom of the cost ladder and you arrive at a paid plan already knowing how to spend it well.
- Overflow and background work — bulk summaries, first-pass exploration of an unfamiliar repo, documentation drafts: jobs where a weaker tool call costs you a retry, not a release.
Not worth it:
- Production work on a codebase you ship. The tool-call drift and missing prompt cache tax exactly the long, careful sessions that matter, and the cost-optimisation habits close most of the honest price gap on the real backend.
- Anything under an NDA or with user data in the repo, for the free-tier data term alone.
- As a permanent arrangement. Free tiers change models and limits without asking — the wider free-tier map exists because two offers changed in a single recent quarter. A workflow that only works while one free tier holds still is a workflow with an expiry date.
Troubleshooting: the three failures everyone meets
Authentication errors immediately. The router is not running, or ccr code was not used to launch, or a stale ANTHROPIC_API_KEY is overriding the router’s token. Check /status first; it names the base URL and the credential source.
“Model not found” on some actions but not others. A request class has no route — usually background or long-context calls hitting an alias with no mapping. Route every class in the Router block, not just default.
The session answers but will not edit files. The transformer is missing or mismatched for the provider, so tool calls are not surviving translation. Confirm the gemini transformer is attached to the provider entry, and restart the router after any config change — config edits do not apply live.
How we researched this
Desk research from primary sources: Anthropic’s Claude Code environment-variable documentation and gateway guidance for the routing mechanism and the alias variables; the Claude Code Router repo’s README and config documentation for the provider, transformer and Router structure; Google’s Gemini API pricing and terms pages (via our limits guide) for the free tier’s models and data terms. We are describing a documented configuration, not reporting a benchmark: model behaviour through a translator varies by model version, which is why the losses above are stated as mechanisms rather than scores.
FAQ
Is this an official Google or Anthropic feature? No. Anthropic documents the base-URL mechanism for gateways serving Claude models and does not support non-Claude backends; the Gemini translation is a community open-source project. It works, and it is unsupported — both halves matter.
Can I point Claude Code directly at the Gemini API without a router? No. Claude Code sends Anthropic Messages-format requests; Gemini’s endpoints accept Google’s format and an OpenAI-compatible format, neither of which matches. The translation layer is the whole trick.
Does the free Gemini key really cost nothing? The key is free and the Flash models are free within your project’s rate limits. The price is the data term — free-tier content may be used to improve Google’s products — and the limits themselves, which live in AI Studio and vary by project.
Will my Claude Pro or Max subscription work through the router? No. A subscription is not API credit and does not travel through a base-URL change; the split is explained in Claude Pro is not API credits. Routed sessions bill — or in this case, don’t bill — against whatever key the router holds.
What is the better free route for serious practice? Alternate: use the routed setup for volume learning, and a small amount of real API credit or a subscription month for the projects you actually ship. The Claude Code cost ladder orders those paid options by impact.
Conclusion
Pointing Claude Code at a free Gemini key is a routing trick, not a plan feature: ANTHROPIC_BASE_URL sends the requests to a local router, the router’s transformer rewrites them for Gemini, and a free AI Studio key pays nothing at the end. In exchange you give up prompt caching, clean tool calls, thinking controls and any vendor’s support. For learning the workflow and for background volume, that trade is genuinely good. For work you ship, it is a false economy — learn here, then climb the cost ladder when the work starts to matter.
Category: Claude Code & Context Engineering · Tags: #claude-code #pricing #context-engineering
Next step: this article is part of Claude Code guides, which covers the full topic from first setup to advanced use.