fix: use BillingModel and upstream fallback for GPT billing in OpenAI gateway#1442
Open
octo-patch wants to merge 1 commit intoWei-Shaw:mainfrom
Open
fix: use BillingModel and upstream fallback for GPT billing in OpenAI gateway#1442octo-patch wants to merge 1 commit intoWei-Shaw:mainfrom
octo-patch wants to merge 1 commit intoWei-Shaw:mainfrom
Conversation
… gateway (fixes Wei-Shaw#1430) When a GPT account's model mapping uses a non-standard name (e.g. "gpt" without version), RecordUsage could not find pricing and silently billed zero. Similarly, the Anthropic Messages compat path set BillingModel to the correct GPT model but RecordUsage ignored it, billing at Claude rates instead. Two changes to OpenAIGatewayService.RecordUsage: 1. Prefer result.BillingModel when set (non-empty), falling back to the existing forwardResultBillingModel logic. This ensures the Messages compat path (CC to GPT) uses the correct GPT pricing. 2. When the resolved billing model has no pricing configured, fall back to result.UpstreamModel before giving up. The upstream model is always normalised to a known model (e.g. "gpt-5.1"), so this prevents billing from silently dropping to zero for non-standard model names.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1430
Problem
When using Claude Code (CC) with a GPT account through the
/v1/messagesendpoint, billing could show zero or incorrect amounts:Billing zero: When the account's model mapping uses a non-standard model name (e.g.
gptwithout a version number),RecordUsagefails to find pricing and silently sets cost to0. This is becausenormalizeCodexModelmapsgpt→gpt-5.1(for the upstream request) but the billing logic uses the original un-normalizedgptname which has no pricing entry.Wrong billing model on Messages path: The Anthropic Messages compat path (used by CC) correctly sets
result.BillingModelto the GPT billing model (e.g.gpt-5.1-codex), butRecordUsageignored this field and usedresult.Model(the Claude model name, e.g.claude-opus-4-6) for pricing instead, resulting in billing at Claude rates rather than GPT rates.Solution
Two targeted changes to
OpenAIGatewayService.RecordUsage:Prefer
result.BillingModelwhen it is explicitly set, falling back to the existingforwardResultBillingModellogic. This ensures the Messages compat path bills at the correct GPT rate.Upstream model fallback: When the resolved billing model has no configured pricing, try
result.UpstreamModelbefore giving up. The upstream model is always normalised to a known model (e.g.gpt-5.1), so this prevents billing from silently dropping to zero for non-standard model names.Testing
TestOpenAIGatewayServiceRecordUsage_UnknownBillingModelFallsBackToUpstream: verifies that whenbillingModel="gpt"(no pricing) andupstreamModel="gpt-5.1"(has pricing), cost is non-zero.TestOpenAIGatewayServiceRecordUsage_BillingModelPreferredOverRequestedModel: verifies that whenBillingModel="gpt-5.1-codex"andModel="claude-opus-4-6", billing uses GPT pricing.