Skip to content

[Bug]: Client-disconnect partial billing treats explicit zero usage as missing and charges estimated tokens #37992

Open

Description

@linshaoyong

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_usage
  • litellm/proxy/common_request_processing.py::_bill_partial_streamed_spend_on_disconnect
  • litellm/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

  1. 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,
        ),
    )
  2. 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"}],
    )
  3. Observe that partial.usage.prompt_tokens and/or completion_tokens become positive even though the provider usage was explicitly zero.

  4. Pass the partial response through the disconnect success-billing path.

  5. 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: success

What 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

  1. added
    Cost & Budgeting
    on Oct 24, 2026
  2. lucastononro commented on Nov 11, 2026

    @lucastononro

    Any updates on this?
    I am tired of token usage deduplication

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Cost & Budgetingproxy

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions