Skip to content

[Bug]: Antigravity usage double-counts cached prompt tokens (inputTokens already includes cachedReadTokens) #37686

Closed

Description

@Dodhon

T3 counts cached prompt tokens twice for the Antigravity (Gemini) provider. acpPerTurnPromptUsage adds cachedReadTokens on top of ACP inputTokens, but Antigravity 1.4.5 already includes cached reads in inputTokens. So each turn's input is roughly doubled, the cache hit rate shows about half its real value, and the list-price cost in Usage comes out about 5x too high.

Before submitting

  • I searched existing issues and did not find a duplicate.
  • I included enough detail to reproduce or investigate the problem.

Area

apps/server (the mapping itself is in packages/provider-acp)

Steps to reproduce

  1. Use the Antigravity provider (Gemini 4 Hydrogen or Gemini 4 Argon) and run a few multi-step turns in one thread so the prompt cache warms up.
  2. Look at the turn's turnTokenUsage (or the Usage view): compare inputTokens and cachedInputTokens.
  3. Compare them with Antigravity's own per-call usage records for the same conversation (the gen_metadata protobuf in the conversation .db: field 2 = uncached prompt tokens, field 5 = cached; this is the field mapping antigravityUsageReader.ts uses).

Expected behavior

inputTokens equals the real prompt total (uncached + cached), and cachedInputTokens / inputTokens matches Antigravity's own cache hit rate (about 90% in a long agentic thread).

Actual behavior

inputTokens is the real prompt total plus the cached tokens a second time. Every Antigravity turn shows a cache rate of about 46-49%, whatever the turn size: with true hit rate h, T3 shows h / (1 + h).

Example turn: T3 recorded inputTokens 24,256,450 and cachedInputTokens 11,859,532. The real prompt total is about 12.4M, of which about 11.9M is cached (95.7%).

Aggregate over 63 completed turns where one T3 turn maps to exactly one Antigravity conversation (Gemini 4 Hydrogen):

Metric Antigravity's own records T3 turnTokenUsage
Cache hit rate 90.0% 47.4%
Prompt tokens, raw / T3 0.52 1.00
List-price cost $25.18 $120.01
List-price cost, recomputed with input = T3 inputTokens - cachedInputTokens $25.36 (within 1% of raw)

One Gemini 4 Argon turn showed the same pattern: raw hit rate 95.1%, T3 shows 48.0%, and the raw/T3 prompt ratio is 0.50.

The repo's own fixture fits this too. In AcpAdapterV2.test.ts, the Antigravity sample has inputTokens: 42, outputTokens: 3, thoughtTokens: 9, cachedReadTokens: 24, totalTokens: 54. Since 42 + 3 + 9 = 54, the 24 cached tokens are already inside the 42. If they weren't, the total would be 78.

Suspected cause

These links are to 322f654. The same code is still on main @ 5a20d59 (adapter.ts L313 and L600, AntigravityAdapterV2.ts L283, AcpAdapterV2.test.ts L202-L221).

The output half (outputTokens + thoughtTokens) looks right: in the raw records, field 3 is about field 9 + field 10. That's our reading of the data, not something we've proven.

Suggested fix

  • For Antigravity, use ACP inputTokens as-is and don't add cachedReadTokens. Keep cachedInputTokens as a subset of it. Either split geminiPromptUsage into separate input and output flags, or drop the input half for this flavor.
  • Fix the doc comment at adapter.ts L509.
  • Update the AcpAdapterV2.test.ts expectation so it expects inputTokens: 42, not 66, and keep totalTokens consistent.
  • Optional: flag a turn whose input + output (+ thought) doesn't match the reported totalTokens. That check would have caught this.

Impact

Major degradation or frequent failure. Every Antigravity turn reports wrong input tokens, cache rate and cost. Only usage reporting is affected; runs themselves work.

Version or commit

Desktop 0.1.9, source 322f6544dfad0a3c8a0c8cbecf2c7f7874b14268; code unchanged on main @ 5a20d59db38abef8e0b98cfe47fed81d074bcecc

Environment

Windows 11, T3 Code desktop 0.1.9, Antigravity CLI 1.4.5 (ACP), Gemini 4 Hydrogen and Gemini 4 Argon

Workaround

None in the app. Recompute input as inputTokens - cachedInputTokens to get the real prompt total.

🤖 Generated with Claude Code

Activity

  1. JinJieBeWater commented on Nov 17, 2026

    @JinJieBeWater

    Related observation on Antigravity ACP Server 1.5.9: unlike Claude Code and Codex, agy acp / T3's provider event logs (events.<threadId>.log) also don't surface per-step input_tokens and cache_read_tokens breakdowns (only agy --output-format stream-json CLI mode exposes per-step usage.cache_read_tokens), making it difficult to verify prompt-cache hit rates or spot cache-busting behavior in T3 threads.

  2. juliusmarminge commented on Nov 19, 2026

    @juliusmarminge
    Member

    I tried to reproduce this on the current build and could not.

    Commit origin/main 410109d. Ran the Antigravity CLI that T3 uses when it has no managed runtime (agy acp, Antigravity ACP Server 1.5.12) with T3's spawn arguments (acp --gemini-dir <dir> --app-data-dir antigravity-acp), GEMINI_API_KEY set and GOOGLE_GEMINI_BASE_URL pointing at the hub. Steps: initialize, then authenticate with methodId gemini-api-key, then session/new, then set_model gemini-4-hydrogen-low, then two tool-using prompts (create three files and list them; read them back and append text). The session/prompt results reported usage {inputTokens:18102, outputTokens:588, cachedReadTokens:29364, thoughtTokens:155, totalTokens:48054} and {inputTokens:15551, outputTokens:578, cachedReadTokens:59079, thoughtTokens:83, totalTokens:75208}. totalTokens equals input + cachedRead + output exactly. Decoded the conversation's gen_metadata rows (the field mapping antigravityUsageReader.ts uses). Summed over both turns: field 2 (uncached) is 33653 and equals the ACP inputTokens sum; field 5 (cached) is 88443 and equals the cachedReadTokens sum; field 3 is 1166, equals the outputTokens sum, and equals field 9 (thought, 238) plus field 10 (928). So on 1.5.12, inputTokens leaves out cached reads (main's input + cachedRead is correct) and outputTokens already holds thought tokens (main adds thoughtTokens again, giving output 743 instead of 588 for turn 1). The reporter's doubled input happens on Antigravity 1.4.5, which main's model-manifest.json still lists as supported. The repo fixture (42 + 3 + 9 = 54 with 24 cached) and the reporter's gen_metadata data confirm that shape, but I could not install 1.4.5 because T3's installer refuses versions below 1.5.0, so I did not see the doubled input myself. I did not run the full T3 server or UI; the adapter passes this per-turn usage straight to acpPerTurnPromptUsage (adapter.ts around line 7999). The failing test feeds both shapes to that function: on main, 1.4.5 gives inputTokens 66 instead of 42, and the real 1.5.12 sample gives outputTokens 743 instead of 588.

    Closing as not planned. If it still happens for you, reply with the exact steps and the version you are on and it will be looked at again.

  3. juliusmarminge commented on Nov 19, 2026

    @juliusmarminge
    Member

    Reopening. The check above ran in a different setup from the report (an API key instead of a Google account, and Antigravity 1.5.12), so not seeing the bug there does not show it is gone.

  4. artursoler commented on Nov 20, 2026

    @artursoler

    Same machine and the same Google sign-in (oauth-personal), T3 Code 0.1.10 on Linux. One Gemini 4 Neon session on each Antigravity version. The decoded gen_metadata usage fields are summed over each session: 2 is uncached input, 5 is cache reads, 3 is output, 9 is thinking and 10 is text output.

    Antigravity 1.4.5 Antigravity 1.5.13
    Field 2 / field 5 / field 3 (= 9 + 10) 208,984 / 1,350,560 / 43,708 (= 32,749 + 10,959) 54,567 / 170,194 / 2,720 (= 2,206 + 514)
    T3 inputTokens / cachedInputTokens 2,910,104 / 1,350,560 224,761 / 170,194
    T3 outputTokens / reasoningTokens 43,708 / 32,749 4,926 / 2,206

    Antigravity reversed both ACP conventions between these versions. The account type doesn't matter: this matches the 1.5.12 API-key result above.

    • 1.4.5: inputTokens includes cache reads and outputTokens excludes thinking. T3's input is doubled (= 2 + 2 × 5), and its output is right.
    • 1.5.13: inputTokens excludes cache reads and outputTokens includes thinking. T3's input is right, but its output counts thinking twice (4,926 = 2,720 + 2,206).

    #31703 as written would therefore fix 1.4.5 input but under-report 1.5.x input, and it leaves the 1.5.x output double count. Possible approaches:

    • Choose the mapping by the server version from initialize (agentInfo.version).
    • Detect it per sample from totalTokens. On 1.4.5 it equals input + output + thought (the existing 42 + 3 + 9 fixture). On 1.5.12 it equals input + cachedRead + output (the run above).
  5. Dodhon commented on Nov 21, 2026

    @Dodhon
    Author

    Closing as resolved.

    Both usage double-counts are addressed on main:

    Maintainer note: open PR #31703 targets the obsolete 1.4.5 input convention and would under-report input tokens on 1.5+, so it can be closed as superseded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions