Repository navigation
Conversation
rujunchen
marked this pull request as ready for review
September 16, 2026 05:27
Contributor
Greptile SummaryThis PR separates the client-facing streaming chunk from the chunk retained for accounting
Confidence Score: 5/5The PR appears safe to merge, with the public alias and provider billing identity kept separate No actionable failures remain; the changed path preserves the retained chunk while serializing a restamped copy, and focused offline tests cover the reported zero-cost regression Important Files Changed
Reviews (1): Last reviewed commit: "fix(proxy): preserve streaming cost when..." | Re-trigger Greptile |
Contributor
|
#44341 on main stamps the client alias on a copy of each streamed chunk, so pricing keeps the real model. Reopen if something is missing |
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.
TLDR
Problem this solves:
How it solves it:
Reproduction
Configure this deployment without custom pricing or
base_model:For the non-streaming control, set
stream:falseand removestream_options; it is billed correctlyRoot cause
The server-side alias configuration above is valid. The defect occurs when the HTTP streaming path changes a chunk's public model name on an object also retained for billing
CustomStreamWrappercopies chunks containing usage, but retains ordinary text chunks by reference_restamp_streaming_chunk_modelmutates its model fromclaude-sonnet-4-6to the requested aliasclaude-sonnet-4.6. This also changes the retained text chunks; the copied usage chunks keep the original model_get_model_from_chunksprefers a model differing from the first chunk's model, selecting the dotted alias for the assembled responsebase_model, or a preserved provider-reported model, billing uses that response model.anthropic/claude-sonnet-4.6has no exact price entry in the tested price map. The Claude capability fallback supplies no prices, and missing input/output rates default to zeroToken counts therefore remain correct while the recorded cost becomes zero, displayed as
-in the request list.stream_options.include_usagedoes not fix this model-selection error; the regression covers both settingsThe provider-model preservation added in #38656 does not prevent this Anthropic case because the parser's intermediate chunks omit the provider-reported model. Non-streaming requests calculate and store their cost before the HTTP response alias rewrite
How this PR fixes it
Copy the outgoing chunk before changing its top-level
model. The retained original keeps the provider model for final accounting, while the client still receives the configured public alias. A shallow copy is sufficient because only the top-level model field changesUser Flow
Before: the streaming request succeeds but its cost is zero
http://localhost:4000/v1/chat/completionshttp://localhost:4000/ui/logs/; the successful request has tokens but zero cost, displayed as-After: the same request is billed using the actual model
http://localhost:4000/v1/chat/completionshttp://localhost:4000/ui/logs/; the request has a nonzero cost and still returns the configured aliasWorkaround
Until the fix is deployed, the proxy operator can add
model_info.base_modelto the existing deployment in the server's configuration YAML:model_infobelongs in the server configuration, at the same level aslitellm_params. Mergebase_modelinto any existingmodel_info, preserving the deployment's other settings. Clients can keep requestingclaude-sonnet-4.6For this standard-pricing configuration, the explicit base model takes priority over the assembled response model, so billing uses the priced
anthropic/claude-sonnet-4-6entry even if the stream still contains the aliasApply the configuration through the normal proxy deployment process, restarting or reloading as required. Rerun the streaming curl above and check that the new log entry has a nonzero cost matching its token counts and configured rates; use a non-streaming request as a control
The focused offline replay verified this workaround: the 8-input/23-output-token fixture changed from $0 to $0.000369 using the tested price map. The configuration change has not been validated against a live provider or deployed to production
Relevant issues
Related: #38656 preserves provider-reported model names, but Anthropic's intermediate stream chunks do not supply that field
Affected release
Observed on v1.92.0 and reproduced on main at
3b14631f06. The first affected release has not been establishedLinear ticket
Validation
The regression uses synthetic Anthropic events with the real parser, stream wrapper, SSE generator, final success callback, and cost calculator. It checks billing with
include_usageenabled and disabled, alongside preservation of the public aliasBefore the fix, both billing cases record zero cost and three chunk-preservation checks fail. After the fix, both related test files pass: 91 tests, with 100% changed-line coverage. Ruff, formatting, and the changed test file's quality check pass; the changed production file adds no strict lint or type diagnostics compared with the base
Pre-Submission checklist
Screenshots / Proof of Fix
Live-provider before/after proof is pending; provider credentials were unavailable. The checks above use synthetic events and do not claim live end-to-end validation
Type
Bug Fix
Caveats
Low
fcntl-based full lint gateFinal Attestation