Skip to content

fix: use BillingModel and upstream fallback for GPT billing in OpenAI gateway#1442

Open
octo-patch wants to merge 1 commit intoWei-Shaw:mainfrom
octo-patch:fix/issue-1430-billing-zero-unknown-gpt-model
Open

fix: use BillingModel and upstream fallback for GPT billing in OpenAI gateway#1442
octo-patch wants to merge 1 commit intoWei-Shaw:mainfrom
octo-patch:fix/issue-1430-billing-zero-unknown-gpt-model

Conversation

@octo-patch
Copy link
Copy Markdown

Fixes #1430

Problem

When using Claude Code (CC) with a GPT account through the /v1/messages endpoint, billing could show zero or incorrect amounts:

  1. Billing zero: When the account's model mapping uses a non-standard model name (e.g. gpt without a version number), RecordUsage fails to find pricing and silently sets cost to 0. This is because normalizeCodexModel maps gptgpt-5.1 (for the upstream request) but the billing logic uses the original un-normalized gpt name which has no pricing entry.

  2. Wrong billing model on Messages path: The Anthropic Messages compat path (used by CC) correctly sets result.BillingModel to the GPT billing model (e.g. gpt-5.1-codex), but RecordUsage ignored this field and used result.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:

  1. Prefer result.BillingModel when it is explicitly set, falling back to the existing forwardResultBillingModel logic. This ensures the Messages compat path bills at the correct GPT rate.

  2. Upstream model fallback: When the resolved billing model has no configured pricing, try 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.

Testing

  • Added TestOpenAIGatewayServiceRecordUsage_UnknownBillingModelFallsBackToUpstream: verifies that when billingModel="gpt" (no pricing) and upstreamModel="gpt-5.1" (has pricing), cost is non-zero.
  • Added TestOpenAIGatewayServiceRecordUsage_BillingModelPreferredOverRequestedModel: verifies that when BillingModel="gpt-5.1-codex" and Model="claude-opus-4-6", billing uses GPT pricing.

… 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.
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.

【扣费0元BUG】CC中使用GPT,当模型设置为GPT时,会出现计费0元的问题

1 participant