GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat: display Google Gemini cached token stats by thisisryanswift · Pull Request #6860 · anomalyco/opencode

Conversation

@thisisryanswift

@thisisryanswift thisisryanswift commented Jan 4, 2026

Copy link
Copy Markdown

feat: display Google Gemini cached token stats

Closes #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:

  • Google caching docs
  • Cline's implementation for reference

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

lukaswelte reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@thisisryanswift
feat: display Google Gemini cached token stats
b974489
Extract cached token counts from Google's response metadata so they're
visible in OpenCode's usage display.

Gemini 2.5+ models use implicit caching (automatic, server-side). However,
OpenCode wasn't reading the cached token counts from Google's metadata
location (usageMetadata.cachedContentTokenCount) rather than the standard
location.

This enables users to see their Gemini cache hits in the session context
usage display, and cost calculations will correctly account for cached
tokens.

Verified working: tested with gemini-3-flash-preview, observed cache.read
values of 16K, 49K, and 107K tokens in a multi-turn conversation.

Future opportunity: For guaranteed cache hits, explicit caching could be
implemented using GoogleAICacheManager + providerOptions.google.cachedContent.
See: https://ai.google.dev/gemini-api/docs/caching
@thisisryanswift
thisisryanswift force-pushed the feature/gemini-caching branch from 23f5298 to b974489 Compare January 4, 2026 20:21
@rekram1-node

rekram1-node commented Jan 4, 2026

Copy link
Copy Markdown
Member

so the google ai sdk provider doesnt track it?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@thisisryanswift

thisisryanswift commented Jan 4, 2026

Copy link
Copy Markdown
Author

Doesn't seem like it locally. Before and after of this change is that I can go back to sessions and actually see a cached token amount.

Could be something upstream in Vercel's AI SDK?

Transparently I mostly vibe coded here. I did try to be intentional though. Ultimately rolled back from my initial issue. I thought it wasn't caching at all. But really it's just not reporting/recording caching

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rekram1-node

rekram1-node commented Jan 5, 2026

Copy link
Copy Markdown
Member

what provider are u using? google directly? any plugins?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@thisisryanswift

thisisryanswift commented Jan 5, 2026

Copy link
Copy Markdown
Author

Google via apikey. Only plugin is a notifier I vibe coded. None of the antigravity or gemini cli auth provider plugins.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rekram1-node

rekram1-node commented Jan 5, 2026

Copy link
Copy Markdown
Member

Hmm I tested this several times and couldnt get any difference in token counting but it could just be happenstance..

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@thisisryanswift

thisisryanswift commented Jan 5, 2026

Copy link
Copy Markdown
Author

So I don't think it will change your total token count in the TUI. But, this change will correctly identify cached tokens. So it impacts the price displayed in the TUI and also would have those cached token counts if someone went back and analyzed their sessions. Previously, Gemini was showing zero cached tokens (at least it was for me locally) when you went back and looked at old sessions. Was just never recording the implicit/automatic caching.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@danchurko

danchurko commented Jan 6, 2026

Copy link
Copy Markdown

Do you think this change will apply the fix to the Vertex AI connections, too?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@thisisryanswift

thisisryanswift commented Jan 7, 2026

Copy link
Copy Markdown
Author

Do you think this change will apply the fix to the Vertex AI connections, too?

Iirc, the caching is by model not by provider generally. But there is a mix of both. If you use anthropic claude models via vertex I think current system will already cache correctly. But I didn't test this myself. If you use Gemini via vertex my assumption would be that server side implicit caching is working automatically (already). But it like suffers the same fate that this fixes where OpenCode doesn't record that. Not 100% sure.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot mentioned this pull request Jan 23, 2026
feat: add /stats dialog with token usage statistics #10234
Closed
4 of 7 tasks
@ajjucoder ajjucoder mentioned this pull request Jan 23, 2026
feat: Add /stats command for token usage statistics #10240
Closed
This was referenced Jan 23, 2026
feat: add openai-compatible endpoint support for google-vertex provider #10303
Merged
feat(cli): include cache tokens in stats #10582
Merged
@thdxr
thdxr force-pushed the dev branch from cbab81f to 2d3c7a0 Compare January 30, 2026 04:50
@opencode-agent
opencode-agent Bot force-pushed the dev branch from 00637c0 to 71e0ba2 Compare January 30, 2026 14:32
@thdxr
thdxr force-pushed the dev branch 3 times, most recently from f1ae801 to 08fa7f7 Compare January 30, 2026 14:37
@github-actions

github-actions Bot commented Mar 8, 2026

Copy link
Copy Markdown
Contributor

Closing this pull request because it has had no updates for more than 60 days. If you plan to continue working on it, feel free to reopen or open a new PR.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot closed this Mar 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

[FEATURE]: Google/VertexAI Context Caching

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@thisisryanswift @rekram1-node @danchurko
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.