fix(gemini,count_tokens): make the Gemini CLI setup usable and stop stalling on rate-limited token counts - #40
Merged
Conversation
…talling on rate-limited token counts Three independent defects, all reachable from a stock `--setup --gemini`. `GOOGLE_GEMINI_BASE_URL` carried an API version. The Gen AI SDK appends its own, so every request went to `/v1beta/v1beta/models/...` and 404ed before reaching a handler. Google's own documented example is a bare origin. Re-running setup rewrites a stale value. The default model written for the CLI was `gemini-2.5-pro`, which Copilot has never served, so even a correctly routed request came back `model_not_supported`. There were no `gemini-*` mappings at all: the catalog offers `gemini-3.1-pro-preview`, `gemini-3.5-flash` and `gemini-3.6-flash`, and nothing pointed at them. A catch-all `gemini-` prefix now resolves to the pro tier, with the flash spellings listed separately so the longest-match rule keeps a flash request on a flash model rather than silently upgrading it to a pricier tier. Config version 5 seeds these into existing files, leaving any id the user already pointed somewhere untouched. `/v1/messages/count_tokens` inherited the shared retry helper, so a 429 cost up to 14s of backoff (2s + 4s + 8s) before falling through to the local estimate it would have produced immediately. Clients call this endpoint before every turn, so under a rate limit the stall repeated on each one. It now issues a single request, and a 429 pauses upstream counting for 60s instead of re-asking a limiter that has already refused. Verified against the live proxy: `gemini-2.5-pro` and `gemini-3.5-flash` both answer and report the expected `modelVersion`, and a stale `.env` migrates while preserving a user-set `GEMINI_API_KEY`. The auth type was examined and deliberately left as `gemini-api-key`. Newer CLIs auto-select the new `gateway` type from the base URL alone, but the `gemini-api-key` path still honours `GOOGLE_GEMINI_BASE_URL`, and a placeholder key is always written, so the existing value works on both old and new releases.
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Three independent defects, all reachable from a stock
--setup --gemini, plus a latency fix on the token-counting path.1.
GOOGLE_GEMINI_BASE_URLcarried an API versionSetup wrote
http://127.0.0.1:8314/v1beta. The Gen AI SDK appends its own version, so the request URL became/v1beta/v1beta/models/...:Gemini CLI passes only
{ baseUrl, headers }, so the SDK default survives. Google's documented example is a bare origin (https://my-proxy.com).Measured before the fix:
/v1beta/models/gemini-2.5-pro:generateContent/v1beta/v1beta/models/gemini-2.5-pro:generateContentRe-running setup rewrites a stale value.
2. The configured model was one Copilot never served
GEMINI_DEFAULT_MODELwasgemini-2.5-pro, and there were nogemini-*mappings at all — so even a correctly routed request returnedmodel_not_supported. The catalog actually offersgemini-3.1-pro-preview,gemini-3.5-flashandgemini-3.6-flash.A catch-all
gemini-prefix now resolves to the pro tier. Flash spellings are listed separately so the existing longest-match rule keeps a flash request on a flash model — otherwise the catch-all would quietly bill the caller for a pricier tier.gemma-*is a different family and is deliberately left alone.Config version 5 seeds these into existing files via
.entry().or_insert_with(), so an id the user already pointed somewhere stays pointed there.3.
count_tokensstalled up to 14s under a rate limitIt used the shared retry helper, which treats 429 as retryable: 2s + 4s + 8s of backoff before falling through to the local estimate it would have produced immediately. Clients call this endpoint before every turn, so the stall repeated on each one.
It now issues a single request, and a 429 pauses upstream counting for 60s rather than re-asking a limiter that has already refused. The endpoint already degrades to a tiktoken estimate, so callers see no error either way.
Verification
Against the live proxy, after migration:
A stale
.envmigrates correctly, preserving unrelated vars and a user-setGEMINI_API_KEY:167 tests pass;
cargo fmt --all --checkandcargo clippy --all-targets -- -D warningsare clean.Deliberately not changed
Auth type. Newer CLIs auto-select a new
gatewaytype from the base URL alone, but thegemini-api-keypath still honoursGOOGLE_GEMINI_BASE_URLand setup always writes a placeholder key, so the existing value works on both old and new releases. Switching would have broken older installs for no gain.Context window. Gemini CLI cannot be told a model's window —
tokenLimit()is a hardcoded table whose default branch returns 1,048,576, so every model this proxy serves is assumed to be 1M. There is no server-side lever;docs/claude-code.mdnow documents settingmodel.compressionThreshold(~0.15 for a 200K model, ~0.1 for 128K) so compaction fires before the upstream rejects the request.