-
Notifications
You must be signed in to change notification settings - Fork 14.8k
Token usage double-counting after AI SDK v6 upgrade #19757
Description
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
- Use Anthropic provider directly with prompt caching enabled
- Observe
tokensin session usage data total≈ 2x expected value;inputincludes 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