fix(spend_logs): billing_status='partial' when cancel dispatched to upstream - #81
Merged
songkuan-zheng merged 1 commit intoJun 10, 2026
Merged
Conversation
…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.
5 tasks
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.
Tier classification
litellm/coreTried upstream first?
v1.87.0-internal.Nhas one release of production data confirming the corrected mapping. Tracked inUPSTREAM_PR_QUEUE.md.Summary
Refines the
_derive_delivery_billing_statusrules introduced in PR #78. The old derivation mislabeled two cancel scenarios as(delivery=none, billing=none)even though the SpendLogs row carried a positivespendfromcompute_prompt_only_costin the failure-hook path.What was wrong
Two cancel paths produced rows where
metadata.billing_status="none"contradicted the row's ownspendcolumn:streaming_partialcompute_prompt_only_cost> 0none❌partialduring_upstreamcompute_prompt_only_cost> 0none❌partialIn 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_statusmust reflect that, otherwise dashboards aggregating bybilling_statuswill 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:
Verification
tests/test_litellm/proxy/spend_tracking/+tests/test_litellm/litellm_core_utils/test_cancel_*.pytest_zero_chunk_cancel_yields_none_none→test_before_upstream_cancel_yields_none_none(unchanged behaviour, only path that still returnsnone/none)test_streaming_cancel_with_zero_bytes_yields_none_none→test_streaming_cancel_with_zero_bytes_yields_none_partialtest_cancel_during_upstream_no_completion_yields_none_partial.Conflict resolutions
N/A — single commit, branch linear from
ship/v1.87.0HEADd1dbe6b004(post-#80 merge).Pre-submission
tests/test_litellm/make test-unitequivalent passes (217/217)