Skip to content

fix(gateway): extract vertex-anthropic streaming token usage - #3264

Merged
steebchen merged 2 commits into
theopenco:mainfrom
serhiizghama:fix/vertex-anthropic-streaming-token-usage
Jul 27, 2026
Merged

steebchen merged 2 commits into
theopenco:mainfrom
serhiizghama:fix/vertex-anthropic-streaming-token-usage

Conversation

@serhiizghama

@serhiizghama serhiizghama commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

Streaming usage for vertex-anthropic was silently coming back empty. extractTokenUsage switches on the provider, and the Anthropic block is case "anthropic": only — so vertex-anthropic fell through to the OpenAI-shaped default, which reads data.usage.prompt_tokens/completion_tokens. Vertex Anthropic streams the Anthropic Messages wire format (message.usage.input_tokens, output_tokens, cache and thinking fields), so the default branch found nothing and returned all-null usage.

The effect is real cost drift: with usage null the streaming path falls back to estimateTokens, and cachedTokens, cacheCreationTokens and reasoning/thinking tokens are dropped entirely, so the cache-read discount never applies. The direct anthropic provider bills the same chunk correctly; vertex-anthropic did not.

Every sibling handler already groups the two the same way — map-finish-reason-to-openai, extract-tool-calls, extract-reasoning, the finish-reason switch in chat.ts, and the non-streaming parse-provider-response — so this was just a missing case label on the streaming usage extractor. Added it, and added a spec that asserts vertex-anthropic extracts the streaming message_start shape and matches anthropic for an identical chunk.

Summary by CodeRabbit

  • Bug Fixes
    • Improved token usage reporting for Vertex Anthropic conversations.
    • Prompt, cached, completion, reasoning, and total token counts are now extracted consistently without double-counting reasoning tokens.

@coderabbitai

coderabbitai Bot commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 38ef9bd5-1b62-4432-8c26-6b51fbd58daf

📥 Commits

Reviewing files that changed from the base of the PR and between 07d9b2c and 6859e67.

📒 Files selected for processing (2)
  • apps/gateway/src/chat/tools/extract-token-usage.spec.ts
  • apps/gateway/src/chat/tools/extract-token-usage.ts

Walkthrough

Adds vertex-anthropic support to token usage extraction by reusing Anthropic handling and covering prompt, cache, completion, reasoning, total tokens, and provider parity in tests.

Changes

Vertex Anthropic token usage

Layer / File(s) Summary
Provider routing and extraction validation
apps/gateway/src/chat/tools/extract-token-usage.ts, apps/gateway/src/chat/tools/extract-token-usage.spec.ts
vertex-anthropic uses the Anthropic token extraction path, with tests covering token fields and matching Anthropic extraction results.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: smakosh

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely summarizes the main change: fixing streaming token usage extraction for vertex-anthropic.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@steebchen
steebchen added this pull request to the merge queue Jul 27, 2026
Merged via the queue into theopenco:main with commit b698197 Jul 27, 2026
11 checks passed
steebchen added a commit that referenced this pull request Jul 27, 2026
#3264 taught extract-token-usage about vertex-anthropic; azure-anthropic
streams the same Anthropic Messages wire format and was still falling
through to the OpenAI-shaped default branch, losing cache and reasoning
token accounting.

Co-Authored-By: Claude <noreply@anthropic.com>
steebchen added a commit that referenced this pull request Aug 22, 2026
#3264 taught extract-token-usage about vertex-anthropic; azure-anthropic
streams the same Anthropic Messages wire format and was still falling
through to the OpenAI-shaped default branch, losing cache and reasoning
token accounting.

Co-Authored-By: Claude <noreply@anthropic.com>
steebchen added a commit that referenced this pull request Aug 24, 2026
#3264 taught extract-token-usage about vertex-anthropic; azure-anthropic
streams the same Anthropic Messages wire format and was still falling
through to the OpenAI-shaped default branch, losing cache and reasoning
token accounting.

Co-Authored-By: Claude <noreply@anthropic.com>
steebchen added a commit that referenced this pull request Aug 24, 2026
#3264 taught extract-token-usage about vertex-anthropic; azure-anthropic
streams the same Anthropic Messages wire format and was still falling
through to the OpenAI-shaped default branch, losing cache and reasoning
token accounting.

Co-Authored-By: Claude <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants