Repository navigation
fix(billing): bill cache tokens on chat calls served over the Responses API - #296
Merged
Merged
Conversation
(cherry picked from commit f116414)
…nses API The cherry-picked upstream fix (QuantumNous#6892) makes settlement read the cache counts a Responses upstream reports under input_tokens_details. This test drives the real producer (relayconvert.UsageFromResponsesUsage) into the gpt-6.2-sol expression production carries and checks hand-typed quotas for the standard tier, the 272K long-context tier and a cache-free call. Why: the conversion is what lets gpt-6.2 take tools on /v1/chat/completions, and it was left off for those models because it billed cached and cache-write tokens at the full input price (117 converted gpt-6-astra calls in production logged 0 cached tokens). The test fails on the pre-fix source with "expected 6000, actual 0". Co-Authored-By: Claude Fable 5.2 <[email protected]>
Author
|
APPROVED-TEST-CHANGE 1b5ccdf -- agent self-approval (operator rule 2026-11-18): service/text_quota_test.go only gains three test functions from upstream QuantumNous#6892; no existing assertion is edited or removed. |
2 tasks done
Yuchengw
added a commit
that referenced
this pull request
Nov 18, 2026
…ep their system prompt cacheable (#297) gpt-6.2-sol and gpt-6.2-astra refuse function tools on /v1/chat/completions ("use /v1/responses"). The chat-to-Responses policy is the way to serve those calls, and with #296 it bills cache correctly. Turning it on for 6.2 in production showed two more reasons it could not stay on: 1. The policy converts every matching call. A plain chat call already works natively; converting it gains nothing and loses whatever the Responses API has no field for (stop, n, logprobs, the penalties). 2. The conversion lifts system messages into `instructions`, and gpt-6.2 never caches `instructions`: four identical 8.7K-token prompts in a row reported no cache write and no cache read, against a write then reads for the same text sent as a leading developer message. gpt-6-astra caches both. Measured against the supplier directly on 2026-11-18. A converted call paid the full input price for a prompt native chat bills at a tenth. Two options on the policy row, both false by default so upstream's behaviour is unchanged until a row asks for them: - only_with_tools: a matching call that carries no tools stays native. - instructions_as_input: a converted call sends its system prompt as the first input message (role developer) instead of `instructions`. The logic lives in two new files; the upstream files gain one struct field pair and three one-line hooks, each marked UNIFYAPI-FORK. Configuration was checked first: param_override runs on the chat body before conversion, so it cannot reach `instructions`, and nothing in the policy can see the request body. Verified with a local build against a fake upstream, production's own billing rows and the policy set to both options: 16 billed cases exact; plain gpt-6.2 chat reached /v1/chat/completions, tool calls reached /v1/responses with input[0].role=developer and no instructions. Co-authored-by: Claude Fable 5.2 <[email protected]>
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.
📝 变更描述 / Description
The defect. When
global.chat_completions_to_responses_policyserves a/v1/chat/completionscall over the Responses API, the converted usage carries a billing snapshot of the raw Responses usage. That snapshot holds the cache counts underinput_tokens_details. Settlement (usageFromOpenAIBillingUsage) prefers the snapshot and reads onlyprompt_tokens_details, so cached and cache-write tokens were billed as fresh input.Production evidence: channel 189 (
gpt-6-astra, policy on since 2026-11-16) logged 0 cached and 0 cache-write tokens on 117 converted calls, against 88% cache reads on the same channel the week before. All 117 are one internal account; roughly $8 overbilled.The fix. Cherry-pick of upstream QuantumNous/new-api QuantumNous#6892 (
f11641428,-x), unmodified: 23 lines inservice/billing_usage.gothat fillprompt_tokens_detailsfrominput_tokens_details(and fromprompt_cache_hit_tokens) only where the canonical field is zero. Taken verbatim so the next upstream merge carries no conflict.What number moves, and for whom. Only calls whose billing snapshot is OpenAI-shaped, i.e. format-converted calls:
prompt_cache_hit_tokens: now billed as a cache read (down);/v1/chat/completions, native/v1/responses, Claude and Gemini snapshots: unchanged.No price, ratio, discount or catalogue row is touched.
Why code and not configuration.
gpt-6.2-sol/gpt-6.2-astraanswer 400 to every tool-bearing chat-completions call ("use /v1/responses"). Checked:param_overridecannot help (the models rejectreasoning_effort: "none", the Luna workaround); the Responses policy is the configuration answer, and it is this bug that makes it unsafe to switch on. After this ships, enabling the policy for channels 225/226 is a configuration change.Existing test file changed (
service/text_quota_test.go, three test functions added by the upstream commit; no existing assertion is edited or removed):prompt_cache_hit_tokensis honoured.New file
service/unifyapi_responses_conversion_cache_billing_test.godrives the real producer into the productiongpt-6.2-solexpression with hand-typed quotas (7,550 / 248,750 / 12,500).🚀 变更类型 / Type of change
🔗 关联任务 / Related Issue
AGENTS.md("OPEN DEFECT ... chat→Responses policy drops cache tokens").✅ 提交前检查项 / Checklist
relaykitUsageFromResponsesUsage) → snapshot →effectiveBillingUsage→BuildTieredTokenParams; stream, buffered-stream and non-stream handlers share that producer.📸 运行证明 / Proof of Work
For the release (staging): exercise a policy-converted chat call with cache against a fake Responses upstream on the exact image:
input 10000 / cached 6000 / cache_write 3000 / output 500on the gpt-6.2-sol expression must bill 7,550 quota at group ratio 1 (12,500 before), stream and non-stream; a native chat call and a native/v1/responsescall with the same usage must bill the same before and after. No migration, seed, option or env var.🤖 Generated with Claude Code