Files
magpie/docs
yetone 7f11dfc77f gateway: a Gemini CLI sign-in goes without Claude Code's billing line, so Gemini's cache reads its prefix
Reported on X by @niyezaimenglima: Claude Code on a Gemini model through
magpie showed a cache hit rate of 0.

Cause: Claude Code's first system line, x-anthropic-billing-header, carries
a cch= that changes every turn (#1600). On a Gemini CLI sign-in (Code
Assist, Account.Agent "gemini") magpie kept that line at the head of
systemInstruction, so no two turns shared a prefix and Gemini's implicit
cache never matched. dropsBillingLine now drops it there. forwardTranslated
also built the Code Assist body from req instead of sent, so a dropped line
went out anyway; it builds from sent now, as buildFor does.

Scoped to that one route:
- The built-in Antigravity account's cloak already removes the line, and
  its refusal check (#666) judges the system as written. Unchanged.
- The antigravity, agy-cli and zed plugin cloaks cut the line to its end,
  and Claude Code's identity sentence (joined onto the same line) goes with
  it. Cutting only the fields in magpie, as a first version of this did,
  left "You are Claude Code..." on line 1, and a real Antigravity plugin
  account answered its 429 refusal (400 to Claude Code). Plugins keep the
  line.
- Other vendors: unchanged (dropsBillingLine's non-Code-Assist branch is as
  before).

Measured on a real Antigravity plugin account (sandbox HOME, copied
credentials, antigravity/gemini-3.8-flash-low, three Claude Code-shaped
turns, ~8.3k tokens):
- old code: turn 1 cache_read 0, turns 2 and 3 read 6238/6278 (in ~2084).
- a per-turn changing line near the head of the system (what Gemini CLI's
  kept billing line is): cache_read 0/0/0, in 8297/8331/8381. This is the
  reporter's 0.
- new code on the plugin route, stream and non-stream: 6248/6242/6274, so
  the route is unchanged; usage.jsonl matches the response.
- history in the first user message instead of the system: 6246/6288, so
  conversation content is cached too. The last ~2k tokens are never cached
  (Gemini's chunking).

Tests (both red on the old billing_line.go):
- TestBillingLineLeftOutForCodeAssist: a Gemini CLI login end to end, two
  turns with different cch; neither GenerateContent body has the line and
  systemInstruction is the same in both.
- TestBillingLineCodeAssistOthers: Antigravity built-in and an Antigravity
  plugin keep the line; Gemini CLI drops it.

Run:
- Full suite, go test -timeout 45m ./..., on mote at 72e6f9542 (this change
  on 593ea437d): every package ok except internal/gateway, whose only red is
  TestOpenCodeGoOwnFreeModelGetsNothingOfOpenCodes, #7117, red on
  origin/main too (3 of 3 alone). mica's disk was full (1.2G), so its run
  tested nothing and was discarded.
- Rebased onto 2926b7bef (one commit, usage-record Wire field, no overlap):
  go test ./internal/gateway -run 'BillingLine|CodeAssist|Antigravity|Gemini'
  ok on mote; GOOS=linux and GOOS=windows go vet ./internal/gateway ok;
  gofmt -l clean.

Not tried: a real Gemini CLI account (Google refuses personal Code Assist
sign-ins here), so the Gemini CLI numbers stand in through the per-turn
line above; an AI Studio API key (that route already drops the line,
whether Google reports cached_tokens there is unverified). Not changed: the
agy-cli plugin runs the local agy CLI with its own session and compaction,
and reports agy's cachedReadTokens; that is agy's or the plugin's.
2026-11-19 14:52:25 +08:00
..