Repository navigation
fix(rag): track LLM completion usage and spend for /v1/rag/query #32438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
yassin-berriai
merged 1 commit into
litellm_internal_staging
from
litellm_rag_query_spend_tracking
Jul 17, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
Empty file.
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Medium: Client disconnects bypass sub-call billing
The vector search, and the rerank below, finish before the streaming response is returned, but their own billing events are suppressed. An authenticated caller can request
stream=trueand disconnect before end-of-stream processing;CustomStreamWrapper.aclose()closes the provider stream without emitting the parent success event that consumesadditional_response_cost, so the completed search and rerank calls never enter spend tracking. Keep these sub-calls on separate billing events, or explicitly finalize the accumulated sub-call cost during stream cancellation.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the proxy-wide streaming disconnect behavior rather than a gap this PR introduces. When a streaming client disconnects mid-stream, neither the success nor the failure logging callback fires for the request (documented on _release_max_parallel_requests_on_disconnect in litellm/proxy/utils.py, which exists precisely because of that), so the completion tokens already streamed are not billed either; that holds for /chat/completions and every streamed route today. The sub-call cost rides the same single billing event as the completion, so it bills exactly when the platform bills. Verified live on this branch: a fully drained stream=true query writes the aquery SpendLogs row with completion plus sub-call cost, and a mid-stream disconnect writes no row at all, completion tokens included, which is the platform contract this PR inherits rather than creates
Splitting the sub-calls back onto standalone events would reintroduce the two defects fixed earlier in this review: a direct vector_stores.asearch billing event prices through the web-search call_type branch at 0 (the vector store branch is keyed on avector_store_search, which only the router factory sets), and a standalone rerank event carries no key metadata, so both would under-bill or orphan spend on every streamed request instead of only on early disconnects. Finalizing only the sub-call cost at cancellation would bill a request's search while dropping its typically larger completion tokens, which is inconsistent partial accounting. Disconnect-time finalization of streamed spend is platform infrastructure that should cover all streamed routes in one change; happy to see it tracked separately, but it is out of scope for this fix
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the detail. I can't automatically confirm this is safe to dismiss, so I'm leaving the thread open for a maintainer to make the call.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Follow-up: the platform-wide disconnect billing gap this finding pointed at is now fixed in #33736, which finalizes partial streamed spend (completion tokens plus the folded sub-call cost) at disconnect time in the shared streaming cleanup, covering every streamed route rather than only this pipeline