Skip to content

fix(spend_logs): billing_status='partial' when cancel dispatched to upstream - #81

Merged
songkuan-zheng merged 1 commit into
ship/v1.87.0from
fix/cancel-billing-status-zero-chunk
Jun 10, 2026
Merged

fix(spend_logs): billing_status='partial' when cancel dispatched to upstream#81
songkuan-zheng merged 1 commit into
ship/v1.87.0from
fix/cancel-billing-status-zero-chunk

Conversation

@songkuan-zheng

Copy link
Copy Markdown
Collaborator

Tier classification

  • D — Universal mechanism + company opinion in litellm/ core

Tried upstream first?

Summary

Refines the _derive_delivery_billing_status rules introduced in PR #78. The old derivation mislabeled two cancel scenarios as (delivery=none, billing=none) even though the SpendLogs row carried a positive spend from compute_prompt_only_cost in the failure-hook path.

What was wrong

Two cancel paths produced rows where metadata.billing_status="none" contradicted the row's own spend column:

Scenario phase bytes spend Old billing Real
Streaming cancel before first chunk streaming_partial 0 compute_prompt_only_cost > 0 none partial
Non-stream shield gave up (no_completion) during_upstream 0 compute_prompt_only_cost > 0 none partial

In both cases the proxy had already dispatched the prompt to upstream (we were inside the streaming generator or the non-stream upstream wait). Upstream received the prompt and started consuming tokens. The failure-hook then bills the prompt-only baseline. The derived billing_status must reflect that, otherwise dashboards aggregating by billing_status will under-report cancel revenue.

What's fixed

Reorder the derivation so phase=='before_upstream' is the ONLY path that returns (none, none). Everything else inside the cancel branch falls through to (none, partial) when no positive-delivery or shield-success signal matches.

The updated semantic mapping in the docstring:

| scenario                                       | status   | delivery | billing |
| streaming cancel WITH chunks                   | success  | partial  | partial |
| streaming cancel BEFORE first chunk            | success  | none     | partial |  ← new
| non-stream shield success                      | success  | none     | full    |
| non-stream shield_timeout                      | success  | none     | partial |
| non-stream cancel during upstream wait         | success  | none     | partial |  ← new
| cancel BEFORE any dispatch (before_upstream)   | success  | none     | none    |  ← only legit none/none cancel
| real failure                                   | failure  | none     | none    |

Verification

  • 217/217 pass across tests/test_litellm/proxy/spend_tracking/ + tests/test_litellm/litellm_core_utils/test_cancel_*.py
  • The two affected tests were renamed + their assertions inverted:
    • test_zero_chunk_cancel_yields_none_nonetest_before_upstream_cancel_yields_none_none (unchanged behaviour, only path that still returns none/none)
    • test_streaming_cancel_with_zero_bytes_yields_none_nonetest_streaming_cancel_with_zero_bytes_yields_none_partial
  • Added test_cancel_during_upstream_no_completion_yields_none_partial.

Conflict resolutions

N/A — single commit, branch linear from ship/v1.87.0 HEAD d1dbe6b004 (post-#80 merge).

Pre-submission

…pstream

Phase 3's _derive_delivery_billing_status mislabeled two cancel
scenarios as (delivery=none, billing=none) when in reality the
SpendLogs row carried a positive ``spend`` from
``compute_prompt_only_cost`` in the failure-hook path:

  - Streaming cancel BEFORE first chunk (phase=streaming_partial,
    bytes_delivered=0) — upstream had received the prompt and started
    generating; cancel arrived before the first SSE chunk reached the
    client. compute_prompt_only_cost bills the prompt baseline.

  - Non-stream cancel during upstream wait when shield gave up via
    _fallback_to_failure_hook with usage_source in {no_completion,
    None} (e.g. upstream errored mid-shield). Same billing logic
    applies — prompt was dispatched, prompt-only cost recovered.

Under the old rule both lands on (none, none), which contradicts the
positive ``spend`` column on the same row. Dashboards filtering by
billing_status would under-report cancel revenue.

Fix:
  - Reorder the derivation so phase=='before_upstream' is the only
    path that returns (none, none). Everything else inside the cancel
    branch falls through to (none, partial) when no positive-delivery
    or shield-success signal matches.
  - Update existing tests that pinned the wrong (none, none) values
    and add a test for the during_upstream no_completion case.

Existing tests covering the unaffected scenarios (normal success,
streaming with chunks, shield_timeout, shield_success, real failure,
before_upstream zero-chunk) all stay green — 217/217 spend_tracking +
cancel_billing + cancel_finalize tests pass.

Tier: D (refines the orthogonal-taxonomy derivation shipped in PR #78).
Tried upstream first: not yet — same dogfood window as #78's Tier-D
candidate; will be folded into the upstream issue once the v1.87.0-
internal.N line has one release of production data confirming the
mapping.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant