[PR #6860] feat: display Google Gemini cached token stats #12142

Open
opened 2026-02-16 18:17:04 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/6860

State: open
Merged: No


feat: display Google Gemini cached token stats

Closes https://github.com/anomalyco/opencode/issues/6851

What

One-line fix to read cached token counts from Google's metadata so they show up in session stats.

Why

Google returns cached token counts in a different spot than Anthropic:

  • Anthropic: usage.cachedInputTokens
  • Google: providerMetadata.google.usageMetadata.cachedContentTokenCount

Implicit caching was already working server-side (and saving money), we just weren't displaying it.

The fix

- const cachedInputTokens = input.usage.cachedInputTokens ?? 0
+ const cachedInputTokens = input.usage.cachedInputTokens ?? 
+   (input.metadata?.["google"] as any)?.usageMetadata?.cachedContentTokenCount ?? 0

Tested

Verified locally with a couple of gemini conversations.


Future: Explicit Caching

Google has two caching modes:

  • Implicit (what we're using): Automatic, server-side, probabilistic
  • Explicit: Guaranteed cache hits, requires managing cache objects

For explicit caching, we'd need:

  1. Add @google/generative-ai dependency
  2. Use GoogleAICacheManager to create/update/delete caches with TTL
  3. Pass cache name via providerOptions.google.cachedContent

This is a bigger lift but would give guaranteed savings. See:

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/6860 **State:** open **Merged:** No --- # feat: display Google Gemini cached token stats Closes https://github.com/anomalyco/opencode/issues/6851 ## What One-line fix to read cached token counts from Google's metadata so they show up in session stats. ## Why Google returns cached token counts in a different spot than Anthropic: - Anthropic: `usage.cachedInputTokens` - Google: `providerMetadata.google.usageMetadata.cachedContentTokenCount` Implicit caching was already working server-side (and saving money), we just weren't displaying it. ## The fix ```diff - const cachedInputTokens = input.usage.cachedInputTokens ?? 0 + const cachedInputTokens = input.usage.cachedInputTokens ?? + (input.metadata?.["google"] as any)?.usageMetadata?.cachedContentTokenCount ?? 0 ``` ## Tested Verified locally with a couple of gemini conversations. --- ## Future: Explicit Caching Google has two caching modes: - **Implicit** (what we're using): Automatic, server-side, probabilistic - **Explicit**: Guaranteed cache hits, requires managing cache objects For explicit caching, we'd need: 1. Add `@google/generative-ai` dependency 2. Use `GoogleAICacheManager` to create/update/delete caches with TTL 3. Pass cache name via `providerOptions.google.cachedContent` This is a bigger lift but would give guaranteed savings. See: - [Google caching docs](https://ai.google.dev/gemini-api/docs/caching) - [Cline's implementation](https://github.com/cline/cline/pull/3181) for reference
yindo added the pull-request label 2026-02-16 18:17:04 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#12142