Skip to content

fix(logging): preserve pass-through endpoint response_cost - #21844

Merged
ishaan-jaff merged 2 commits into
mainfrom
fix/passthrough-spend-tracking
Feb 21, 2026
Merged

ishaan-jaff merged 2 commits into
mainfrom
fix/passthrough-spend-tracking

Conversation

@ishaan-jaff

Copy link
Copy Markdown
Contributor

Relevant issues

Fixes proxy_pass_through_endpoint_tests CI failures where spend=0 for Gemini/Vertex pass-through endpoints.

Pre-Submission checklist

  • I have Added testing in the tests/litellm/ directory
  • My PR passes all unit tests on make test-unit
  • My PR's scope is as isolated as possible, it only solves 1 specific problem
  • I have requested a Greptile review

CI (LiteLLM team)

Type

🐛 Bug Fix

Changes

Pass-through handlers (Gemini/Vertex) calculate response_cost via completion_cost() before the logging pipeline runs. Two places in litellm_logging.py were overwriting that cost:

  1. _process_hidden_params_and_response_cost - fell through to _response_cost_calculator which returns None for pass-through calls. Added an elif to preserve already-set cost.
  2. async_success_handler pass-through branch (PR fix: Set standard_logging_object for pass-through endpoints #19887) - unconditionally set response_cost = None. Now only sets None if cost isn't already calculated.

Added regression test that verifies async_success_handler preserves pre-calculated response_cost for pass-through endpoints.

…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.
@vercel

vercel Bot commented Feb 21, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
litellm Ready Ready Preview, Comment Feb 21, 2026 11:01pm

Request Review

@greptile-apps

greptile-apps Bot commented Feb 21, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a regression where pass-through endpoint handlers (Gemini/Vertex) would calculate response_cost via completion_cost() before the logging pipeline runs, but two places in litellm_logging.py would overwrite that cost — _process_hidden_params_and_response_cost falling through to _response_cost_calculator (which returns None for pass-through calls), and the async_success_handler pass-through branch unconditionally setting response_cost = None.

  • Adds an elif guard in _process_hidden_params_and_response_cost to preserve response_cost already set on model_call_details (e.g., by pass-through handlers)
  • Wraps the response_cost = None assignment in async_success_handler's pass-through branch with a conditional check, so pre-calculated costs are not overwritten
  • Similarly guards standard_logging_object construction to avoid rebuilding if already built with correct cost data
  • Adds a regression test that verifies async_success_handler preserves pre-calculated response_cost for pass-through endpoints (mock-only, no real network calls)
  • Minor import reordering in existing tests for alphabetical consistency

Confidence Score: 4/5

  • This PR is safe to merge — it fixes a clear regression with minimal, well-scoped changes and includes a targeted regression test.
  • The changes are well-isolated to the logging pipeline and correctly address two specific overwrite points. The logic is straightforward (conditional guards to preserve existing values). The regression test validates the fix. One minor style nit (no-op assignment) doesn't affect correctness.
  • No files require special attention beyond the minor style nit in litellm/litellm_core_utils/litellm_logging.py.

Important Files Changed

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 ✓
Loading

Last reviewed commit: 639fa68

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

Comment on lines +2506 to +2507
if self.model_call_details.get("response_cost") is None:
self.model_call_details["response_cost"] = None

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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."

Suggested change
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

@ishaan-jaff
ishaan-jaff merged commit 9f459c5 into main Feb 21, 2026
12 of 33 checks passed
songkuan-zheng added a commit to GhishaDev/litellm that referenced this pull request Jun 5, 2026
…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.
fzowl pushed a commit to fzowl/litellm that referenced this pull request Jun 24, 2026
…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

This branch was successfully deployed

1 active deployment
Preview — 639fa68c Deployed Feb 21, 2026 by vercel[bot]
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