Repository navigation
fix(gateway): detect ai studio streaming served tier - #2851
Conversation
WalkthroughAdds ChangesGoogle service-tier body fallback
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/actions/src/apply-google-service-tier.spec.ts (1)
97-103: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winAssert actual header precedence here.
This only verifies the
null-header fallback, so a regression that starts preferringserviceTierBodywhen both are present would still pass. Add one conflicting-value case in this test.Suggested assertion
it("prefers the header but falls back to the body field", () => { + expect( + resolveServedServiceTier({ + serviceTierHeader: "priority", + serviceTierBody: "flex", + }), + ).toBe("priority"); expect( resolveServedServiceTier({ serviceTierHeader: null, serviceTierBody: "flex", }), ).toBe("flex"); });🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/actions/src/apply-google-service-tier.spec.ts` around lines 97 - 103, The resolveServedServiceTier test only covers the null-header fallback, so it misses a regression where serviceTierBody could win when both values exist; update the existing spec in apply-google-service-tier.spec.ts to add a conflicting-value case for resolveServedServiceTier that explicitly asserts the header takes precedence over the body while keeping the null-header fallback check.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@packages/actions/src/apply-google-service-tier.spec.ts`:
- Around line 97-103: The resolveServedServiceTier test only covers the
null-header fallback, so it misses a regression where serviceTierBody could win
when both values exist; update the existing spec in
apply-google-service-tier.spec.ts to add a conflicting-value case for
resolveServedServiceTier that explicitly asserts the header takes precedence
over the body while keeping the null-header fallback check.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: cfacdba5-36db-4871-b12b-019334468358
📒 Files selected for processing (3)
apps/gateway/src/chat/chat.tspackages/actions/src/apply-google-service-tier.spec.tspackages/actions/src/apply-google-service-tier.ts
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: c5544ca817
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
AI Studio streaming responses omit the `x-gemini-service-tier` header and instead report the served tier in the body as `usageMetadata.serviceTier` (`flex` / `priority` / `standard`); unary responses include both. The gateway only read the header, so every streamed flex/priority AI Studio request resolved to standard and was mis-billed. `resolveServedServiceTier` now also reads the body field (the header still takes precedence). Verified against the live API: streaming AI Studio flex now resolves `flex`, priority resolves `priority`. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
c5544ca to
e5457b0
Compare
Summary
On AI Studio (Gemini Developer API),
service_tierflex/priority does work — a direct call returns the served tier. But on streaming responses Google omits thex-gemini-service-tierresponse header and instead reports the served tier in the body asusageMetadata.serviceTier(flex/priority/standard). Unary responses include both.The gateway only read the header, so every streamed Flex/Priority AI Studio request resolved to
null(standard) — mis-billing, and making the selected tier look like it did nothing.resolveServedServiceTiernow also reads theusageMetadata.serviceTierbody field; the header still takes precedence when present.Verification
Verified against the live Google API with
gemini-3.1-pro-preview:x-gemini-service-tier: flexand bodyusageMetadata.serviceTier: flex; streaming response has only the body field.flexnow resolvesflex,priorityresolvespriority(previously both resolved to standard).usageMetadata.trafficType).pnpm format, fullpnpm build, and unit tests pass (18 tests inapply-google-service-tier.spec.ts, incl. new body-field cases).Scope
This PR only fixes served-tier detection for AI Studio streaming. It does not change how web search is routed across service tiers (a separate Vertex Flex + grounding change was considered and dropped for now).
🤖 Generated with Claude Code