Skip to content

Token usage double-counting after AI SDK v6 upgrade #19757

@ualtinok

Description

@ualtinok

Description

Since v1.3.4 (which includes AI SDK v6 via c33d9996f feat: AI SDK v6 support (#18433)), token usage total is double-counted for Anthropic/Bedrock providers. The total field reports roughly 2x the actual token count, and tokens.input (the non-cached portion) is inflated.

Root cause: AI SDK v6 normalized inputTokens to include cached tokens for all providers (PR vercel/ai#10975). Previously, Anthropic excluded cache from inputTokens. The excludesCachedTokens flag in getUsage() still assumes the old v5 behavior — it skips subtracting cache tokens for Anthropic, then adds them back when computing total, effectively counting cache twice.

Evidence from real sessions (v1.3.4, Anthropic direct):

input: 139499, cache.read: 139417, cache.write: 81, total: 279057

input - cache.read - cache.write = 1 (only 1 non-cached token), proving inputTokens now includes cache. But total = 139499 + 60 + 139417 + 81 = 279057 — cache counted twice. Correct total should be 139559.

This pattern is consistent across all requests: input ≈ cache.read + cache.write + 1.

Plugins

None

OpenCode version

v1.3.4 / v1.3.5

Steps to reproduce

  1. Use Anthropic provider directly with prompt caching enabled
  2. Observe tokens in session usage data
  3. total ≈ 2x expected value; input includes cached tokens but code doesn't subtract them

Screenshot and/or share link

N/A — data-level issue, not visual

Operating System

macOS

Terminal

OpenCode Desktop

Metadata

Metadata

Assignees

Labels

coreAnything pertaining to core functionality of the application (opencode server stuff)

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions