Skip to content

Fix Google streaming usage billing#5

Merged
pablopunk merged 2 commits into
mainfrom
chore/roadmap-checklist
Jun 6, 2026
Merged

Fix Google streaming usage billing#5
pablopunk merged 2 commits into
mainfrom
chore/roadmap-checklist

Conversation

@pablopunk
Copy link
Copy Markdown
Owner

@pablopunk pablopunk commented Jun 5, 2026

Summary

Fixes Google/Gemini streaming usage billing in the Nevermind gateway.

Production showed successful Google streaming requests being logged with:

  • input_tokens=0
  • output_tokens=0
  • cost_credits=0

Root cause: the Google SSE usage parser assumed each chunk contained complete data: lines. In production, usageMetadata can arrive split across stream chunks, so the parser silently missed the final token usage payload.

Changes

  • Buffer pending stream text for usage parsers and flush the final partial line at stream end.
  • Make Google streaming usage parsing resilient to split SSE lines.
  • Count Google thoughtsTokenCount as output tokens alongside candidatesTokenCount.
  • Add a backend contract test that reproduces split Google streaming usage and verifies non-zero recorded billing.
  • Document two process learnings:
    • bug fixes should include repro tests and red/green verification when practical
    • vercel logs is live-only when Axiom has no runtime rows

Verification

  • Confirmed the new test fails against the old parser and reproduces the prod symptom (0 tokens / 0 credits).
  • Confirmed the same test passes with this fix (inputTokens=12, outputTokens=6, costCredits=1).
  • mise exec -- pnpm -C backend test
  • mise exec -- pnpm test

Roadmap handoff

The remaining self-serve gateway roadmap has been moved into focused GitHub issues and should be treated as the source of truth:

Summary by CodeRabbit

  • Bug Fixes

    • Fixed Google model token accounting to properly include candidate and thoughts token counts in output calculations
    • Fixed streaming usage accounting when usage metadata is fragmented across multiple stream chunks
  • Documentation

    • Updated Vercel logs debugging guidance with improved troubleshooting instructions
  • Tests

    • Added comprehensive test coverage for Google model streaming with fragmented usage metadata

@vercel
Copy link
Copy Markdown

vercel Bot commented Jun 5, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
nvm Ready Ready Preview, Comment Jun 6, 2026 12:17am

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jun 5, 2026

Looking for one thing? Review this PR in Change Stack to search files, summaries, diffs, and code without losing your place.

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: a1f3b708-b377-444c-8861-051fc74091cf

📥 Commits

Reviewing files that changed from the base of the PR and between 5fb8cc7 and c083996.

📒 Files selected for processing (5)
  • .agents/skills/production-debugger/vercel.md
  • AGENTS.md
  • backend/src/lib/proxy.ts
  • backend/src/pages/api/contract-routes.test.ts
  • backend/src/pages/api/v1/models/[...path].ts

Walkthrough

This PR addresses correct handling of Google API streaming usage metadata that arrives split across multiple HTTP response chunks. It extends StreamUsageAccumulator with a pendingText buffer field, adds helper functions to reassemble streaming data: JSON lines and compute output tokens from both candidatesTokenCount and thoughtsTokenCount, and reworks the streaming parser to use line reassembly while buffering partial lines. The streaming flush logic now sends a final newline to finalize any buffered text. Comprehensive test coverage validates correct reconstruction and token accounting when upstream metadata splits across chunk boundaries. Documentation updates clarify Vercel logging workflow and add verification guidelines for streaming parser chunk-boundary test cases.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Fix Google streaming usage billing' is concise, clear, and directly describes the primary fix in the changeset.
Description check ✅ Passed The PR description covers all required sections: it includes a clear summary of the root cause, detailed changes, verification steps, and backend API compatibility confirmation with test updates.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


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 and usage tips.

@pablopunk pablopunk changed the title Roadmap checklist (tracking) Self-serve AI gateway (tracking) Jun 5, 2026
@pablopunk pablopunk changed the title Self-serve AI gateway (tracking) Self-serve AI gateway Jun 5, 2026
@pablopunk pablopunk force-pushed the chore/roadmap-checklist branch from 0e116ea to c083996 Compare June 6, 2026 00:16
@pablopunk pablopunk changed the title Self-serve AI gateway Fix Google streaming usage billing Jun 6, 2026
@pablopunk pablopunk marked this pull request as ready for review June 6, 2026 00:17
@pablopunk pablopunk merged commit 1bb3ba1 into main Jun 6, 2026
8 checks passed
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.

1 participant