Repository navigation
Description
Check for existing issues
- I searched the existing issues and confirmed this is not a duplicate. Related reports cover missing spend rows or unclosed upstream streams; this issue is specifically about overbilling when explicit zero usage is replaced by local estimates.
What happened?
When a streaming proxy client disconnects, LiteLLM can build a partial response and dispatch success handlers for partial spend tracking.
If the provider usage carried by the collected chunks is explicitly zero:
{
"prompt_tokens": 0,
"completion_tokens": 0,
"total_tokens": 0
}stream_chunk_builder treats those zero values as missing because calculate_usage() uses:
returned_usage.prompt_tokens = prompt_tokens or token_counter(
model=model,
messages=messages,
)
returned_usage.completion_tokens = completion_tokens or token_counter(
model=model,
text=completion_output,
count_response_tokens=True,
)The explicit provider zero is replaced with locally estimated prompt/completion tokens. The disconnect cleanup path then calls dispatch_success_handlers, and the cost calculator prices those estimates using the local model map. This can create a positive spend row even though the provider reported zero billable usage.
Relevant paths on current main:
litellm/litellm_core_utils/streaming_chunk_builder_utils.py::calculate_usagelitellm/proxy/common_request_processing.py::_bill_partial_streamed_spend_on_disconnectlitellm/proxy/spend_tracking/spend_tracking_utils.py::_get_status_for_spend_log
The spend row may also be stored with top-level status=success while its metadata contains ClientDisconnected / 499.
Expected behavior
- Explicit provider zero usage must remain zero.
- Missing usage and explicit zero usage must be distinguishable.
- Local token estimates may be retained for observability, but they must not be silently used as provider-reported raw cost or deducted spend.
- Client-disconnect partial billing must continue to preserve genuinely non-zero provider usage/cost.
- The spend-log status should not represent a client-disconnected request as a complete success.
Steps to Reproduce
-
Build a streamed response chunk with explicit zero usage:
chunk = ModelResponseStream( id="chatcmpl-partial-zero", created=1, model="gpt-4o-mini", object="chat.completion.chunk", choices=[ StreamingChoices( finish_reason=None, index=0, delta=Delta(content="partial output", role="assistant"), ) ], usage=Usage( prompt_tokens=0, completion_tokens=0, total_tokens=0, ), )
-
Rebuild the partial response with request messages, matching the disconnect billing path:
partial = litellm.stream_chunk_builder( chunks=[chunk], messages=[{"role": "user", "content": "a non-empty prompt"}], )
-
Observe that
partial.usage.prompt_tokensand/orcompletion_tokensbecome positive even though the provider usage was explicitly zero. -
Pass the partial response through the disconnect success-billing path.
-
Observe a positive locally calculated response cost/spend row.
A regression test should cover both boundaries:
- explicit zero or no provider billable usage on disconnect -> no provider raw cost / deducted spend;
- non-zero provider usage/cost on disconnect -> preserve partial billing.
Relevant log output
provider usage: prompt_tokens=0 completion_tokens=0 total_tokens=0
provider billable usage/cost: absent
disconnect metadata: error_class=ClientDisconnected error_code=499
partial spend usage: prompt_tokens=<locally estimated non-zero value>
partial spend cost: <positive local model-map cost>
spend status: successWhat part of LiteLLM is this about?
Proxy
What LiteLLM version are you on ?
Current main: v1.99.0, commit 007bd43cfb6eeeabe94d6aa77bd05dd3aa6aa1bf
Twitter / LinkedIn details
No response
Activity
lucastononro commentedon Nov 11, 2026
Any updates on this?
I am tired of token usage deduplication