Repository navigation
fix(llm): capture LiteLLM cost headers and inject into finish providerMetadata - #51957
Closed
BrunoSilva-neuro wants to merge 1 commit into
Closed
BrunoSilva-neuro wants to merge 1 commit into
BrunoSilva-neuro wants to merge 1 commit into
Conversation
…rMetadata LiteLLM 1.98+ omits the primary x-litellm-response-cost header when its value equals x-litellm-response-cost-original (an optimisation introduced to reduce response size). Callers that only read the primary header record $0.00 cost for those responses. Changes: - packages/llm/src/route/transport/http.ts: add a Ref<Record<string,string>> to HttpPrepared as a side-channel; populate it from the HTTP response headers in frames(), reading x-litellm-response-cost with a fallback to x-litellm-response-cost-original when the primary header is absent; also capture x-litellm-key-spend for audit purposes. - packages/llm/src/route/client.ts: after the protocol stream step, map finish and step-finish events to merge providerMetadata.litellm with the captured headers so downstream cost accounting always sees the true value.
Contributor
|
Thanks for your contribution! This PR doesn't have a linked issue. All PRs must reference an existing issue. Please:
See CONTRIBUTING.md for details. |
Contributor
|
The following comment was made by an LLM, it may be inaccurate: |
Contributor
|
Thanks for updating your PR! It now meets our contributing guidelines. 👍 |
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.
Issue for this PR
Closes #52127
Type of change
What does this PR do?
When using a LiteLLM proxy, some turns were recorded with $0.00 cost in
opencode stats. Two root causes:x-litellm-response-costwhen it equalsx-litellm-response-cost-original, so reading only the primary header misses those responses regardless.This PR captures the authoritative cost directly from the proxy's response headers and attaches it to
finish/step-finishevents viaproviderMetadata.litellm:packages/llm/src/route/transport/http.tsresponseHeaders: Ref.Ref<Record<string,string>>toHttpPreparedas a side-channel, populated after the HTTP response arrives.x-litellm-response-costwith fallback tox-litellm-response-cost-original(handles the 1.98+ omission case). Also capturesx-litellm-key-spendfor audit purposes.packages/llm/src/route/client.tsfinish/step-finishevents to mergeproviderMetadata.litellmwith the captured headers.Preparedkeeps WebSocket transport unaffected without interface changes.How did you verify your code works?
Manually called the proxy
/v1/messagesendpoint twice with identical payloads. Request 1 returnedx-litellm-response-cost: 0.000434; request 2 omitted that header but includedx-litellm-response-cost-original: 0.000434. The fallback logic covers both cases. Could not runbun typecheck(tsgounavailable in the environment) — happy to iterate on types if CI catches anything.Screenshots / recordings
N/A — no UI change.
Checklist