Repository navigation
fix(logging): preserve pass-through endpoint response_cost - #21844
Conversation
…uccess_handler Two places in the logging pipeline were overwriting response_cost that pass-through handlers (Gemini/Vertex) had already calculated: 1. _process_hidden_params_and_response_cost fell through to _response_cost_calculator which returns None for pass-through calls 2. async_success_handler pass-through branch unconditionally set response_cost = None (introduced in PR #19887) Now both places check if response_cost is already set before overwriting.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Greptile SummaryThis PR fixes a regression where pass-through endpoint handlers (Gemini/Vertex) would calculate
Confidence Score: 4/5
|
| Filename | Overview |
|---|---|
| litellm/litellm_core_utils/litellm_logging.py | Two fixes to preserve pre-calculated response_cost for pass-through endpoints: adds elif guard in _process_hidden_params_and_response_cost and conditional checks in async_success_handler pass-through branch. Contains a minor no-op style issue at line 2506-2507. |
| tests/test_litellm/litellm_core_utils/test_litellm_logging.py | Adds a well-structured regression test verifying that async_success_handler preserves pre-calculated response_cost for pass-through endpoints. Uses mocking (no real network calls). Also includes minor import reordering for alphabetical consistency. |
Sequence Diagram
sequenceDiagram
participant PT as Pass-through Handler<br>(Gemini/Vertex)
participant MCD as model_call_details
participant ASH as async_success_handler
participant HELPER as _success_handler_helper_fn
participant PHRC as _process_hidden_params<br>_and_response_cost
PT->>MCD: Set response_cost = 0.0000047<br>(via completion_cost)
ASH->>HELPER: _success_handler_helper_fn()
HELPER->>PHRC: _process_hidden_params_and_response_cost()
Note over PHRC: NEW: elif model_call_details<br>["response_cost"] is not None → pass
PHRC-->>MCD: response_cost preserved ✓
HELPER-->>ASH: return
Note over ASH: call_type == "pass_through_endpoint"
Note over ASH: NEW: if response_cost is None → skip
ASH-->>MCD: response_cost still 0.0000047 ✓
Note over ASH: NEW: if standard_logging_object<br>is None → build SLO with correct cost
ASH->>MCD: standard_logging_object.response_cost = 0.0000047 ✓
Last reviewed commit: 639fa68
| if self.model_call_details.get("response_cost") is None: | ||
| self.model_call_details["response_cost"] = None |
There was a problem hiding this comment.
No-op assignment when value is None
This conditional assigns None to response_cost only when it's already None, which is a no-op. The guard correctly prevents overwriting a pre-calculated cost, but the body does nothing useful. Consider simplifying to a comment or removing the block entirely, since the real intent is just "don't touch response_cost if it's already set."
| if self.model_call_details.get("response_cost") is None: | |
| self.model_call_details["response_cost"] = None | |
| if self.model_call_details.get("response_cost") is None: | |
| # cost calculation not possible for pass-through endpoints | |
| # that haven't already computed it | |
| pass |
…router retry (#74) Backports BerriAI#28476 (merged 2026-05-22) to ship/v1.83.10. ## Root cause When Router retries an LLM call, every attempt shares one `Logging` instance. `failure_handler` sets `model_call_details["response_cost"] = 0` on each failed attempt (introduced by upstream BerriAI#4604). The success path in `_process_hidden_params_and_response_cost` then short-circuits via a preserve branch (introduced by BerriAI#21844) that treats any non-None existing cost as authoritative — including the stale 0 from the prior failed attempt — so SpendLog records $0 despite a successful response with real usage. ## Fix Tighten the preserve branch with a walrus-bound `!= 0` guard so a stale zero from failure_handler falls through to recalculation. Legitimate zero costs (cache_hit, pass-through handlers writing `result._hidden_params["response_cost"] = 0`) are still preserved by the earlier two branches. ```python elif ( existing_cost := self.model_call_details.get("response_cost") ) is not None and existing_cost != 0: pass # preserve pass-through cost ``` ## Scope of backport Cherry-picked from upstream merge commit b557492. Kept the `litellm_logging.py` fix + the 3 new regression tests in `tests/test_litellm/litellm_core_utils/test_litellm_logging.py`. Dropped the `tests/local_testing/test_amazing_vertex_completion.py` hunk — that's an upstream-CI Google Maps Platform flake skip, unrelated to this fix and not run in our CI. ## Tier C — universal bug fix in `litellm/` core. Already merged upstream (BerriAI#28476), so this is purely a backport for v1.83.10. ## Verification ``` $ pytest tests/test_litellm/litellm_core_utils/test_litellm_logging.py \ -k "process_hidden_params or async_success_handler_preserves" -v ... 4 passed in 0.72s ``` E2E reproduction (Key1 503 → fallback Key2 200) was previously confirmed against this exact mechanism — spend went from 0 to 0.000968 with the fix applied.
…1844) * fix(logging): preserve pass-through endpoint response_cost in async_success_handler Two places in the logging pipeline were overwriting response_cost that pass-through handlers (Gemini/Vertex) had already calculated: 1. _process_hidden_params_and_response_cost fell through to _response_cost_calculator which returns None for pass-through calls 2. async_success_handler pass-through branch unconditionally set response_cost = None (introduced in PR BerriAI#19887) Now both places check if response_cost is already set before overwriting. * test: add regression test for pass-through endpoint response_cost preservation
Relevant issues
Fixes proxy_pass_through_endpoint_tests CI failures where spend=0 for Gemini/Vertex pass-through endpoints.
Pre-Submission checklist
tests/litellm/directorymake test-unitCI (LiteLLM team)
Type
🐛 Bug Fix
Changes
Pass-through handlers (Gemini/Vertex) calculate
response_costviacompletion_cost()before the logging pipeline runs. Two places inlitellm_logging.pywere overwriting that cost:_process_hidden_params_and_response_cost- fell through to_response_cost_calculatorwhich returns None for pass-through calls. Added anelifto preserve already-set cost.async_success_handlerpass-through branch (PR fix: Set standard_logging_object for pass-through endpoints #19887) - unconditionally setresponse_cost = None. Now only sets None if cost isn't already calculated.Added regression test that verifies
async_success_handlerpreserves pre-calculatedresponse_costfor pass-through endpoints.