Skip to content

fix(logging): price terminal Responses stream events from their inner response - #42385

Merged
kerry-berri merged 5 commits into
mainfrom
litellm_fix_responses_stream_cost_breakdown
Sep 22, 2026
Merged

kerry-berri merged 5 commits into
mainfrom
litellm_fix_responses_stream_cost_breakdown

Conversation

@devin-ai-integration

@devin-ai-integration devin-ai-integration Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • CircleCI integration-cost red on main: 23 cases, integration-providers red: 1 case
  • Streamed Responses API requests store an all-zero cost breakdown (spend itself is right)
  • One cost case and one fal.ai case fail because of the test fixtures, not the proxy

How it solves it:

  • Cost calculator unwraps terminal Responses stream events (response.completed etc) and prices the inner response
  • Integration proxy config enables allow_unmanaged_response_ids for the previous_response_id case
  • fal.ai H3 fixture serves the mp4 download before checking the provider auth header, like its Seedance twin

User Flow

Before: a developer streams a Responses call and the spend log shows the right total but a zero cost breakdown

  1. They send POST https://litellm-domain/v1/responses with "model": "gpt-5.6", "stream": true
  2. The stream ends with a response.completed event carrying usage with 1840 input and 412 output tokens
  3. They open https://litellm-domain/ui/?page=logs: spend reads $0.0156 but the cost breakdown says input $0, output $0, total $0

After: the same request stores a breakdown that matches the spend

  1. They send POST https://litellm-domain/v1/responses with "model": "gpt-5.6", "stream": true
  2. The stream ends with the same response.completed event
  3. https://litellm-domain/ui/?page=logs shows spend $0.0156 and a breakdown of input $0.00736, output $0.00824, total $0.0156

Chat completions routed through the Responses bridge (gpt-5.x-codex, -pro) hit the same path and are fixed the same way.

Relevant issues

The two fixture fixes have no product-facing change: the previous_response_id case sends an id the proxy never issued, which the proxy rejects with 403 by design since #39548, and the fal.ai H3 fake upstream required the fal auth header on the bare mp4 download.

Affected release

Linear ticket

Pre-Submission checklist

  • I have added meaningful tests
  • The handful of test files covering my change pass locally
  • My PR passes all required CI/CD checks
  • My PR's scope is as isolated as possible
  • Greptile confidence score 4/5

Screenshots / Proof of Fix

Run with the canonical CircleCI runner (.circleci/scripts/run_integration.sh) against a live proxy, Postgres, Redis and the scripted upstream on this machine. No real provider calls: the cost suite is a pricing contract and pins exact spend, so a scripted upstream is the correct oracle.

Before (275c0c4, merge base)

integration cost

  1. bash .circleci/scripts/run_integration.sh cost
  2. 23 failed, 460 passed, e.g. gpt-5.5-pro-stream_no_usage: row spend 0.00216 != breakdown total_cost 0.0 and gpt-5.6-responses_previous_response_id: proxy returned 403 ... allow_unmanaged_response_ids

integration providers

  1. bash .circleci/scripts/run_integration.sh providers
  2. 1 failed, 22 passed: test_fal_h3_video_create_uses_canonical_body_and_status_path, content download returns 500 (KeyError: 'authorization' in the fake upstream)

unit

  1. pytest tests/test_litellm/litellm_core_utils/test_litellm_logging.py -k terminal_responses_event
  2. FAILED ... assert (0.0 is not None and 0.0 > 0)

After (3ae43d3)

integration cost

  1. bash .circleci/scripts/run_integration.sh cost
  2. 483 passed in 124.40s

integration providers

  1. bash .circleci/scripts/run_integration.sh providers
  2. 23 passed in 75.61s (plus 4 passed replay controls)

unit

  1. pytest tests/test_litellm/litellm_core_utils/test_litellm_logging.py tests/test_litellm/responses/test_streaming_iterator.py
  2. 340 passed

Same 24 failures were red on CircleCI main (pipeline 90023, jobs 2198864 and 2198859), so this should turn the integration workflow green.

Type

🐛 Bug Fix
✅ Test

Caveats (if any)

Low

  • Persisted spend was already correct; only the stored breakdown (and anything reading it) was zero

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

Link to Devin session: https://app.devin.ai/sessions/bb25954394a4480d83a4fae2a50cb954
Open in Devin Desktop: https://app.devin.ai/desktop/session/bb25954394a4480d83a4fae2a50cb954?variant=devin


Note

Low Risk
Logging/pricing path change with targeted tests; spend totals were already correct and integration config changes are test-only.

Overview
Fixes zero cost breakdown on streamed /v1/responses (and related bridges) when spend was already correct: _response_cost_calculator now unwraps terminal stream events (response.completed / incomplete / failed) and runs pricing, hidden-params, and response_object logic on the inner ResponsesAPIResponse instead of the event wrapper.

Integration-only adjustments: proxy test config sets allow_unmanaged_response_ids so previous_response_id cases are not rejected with 403, and the fal.ai H3 wire mock serves mp4 GET before enforcing the auth header (matching Seedance). A unit test asserts event cost equals inner-response cost and populates cost_breakdown.

Reviewed by Cursor Bugbot for commit 2f6a9eb. Bugbot is set up for automated code reviews on this repo. Configure here.

kerry-berri and others added 3 commits September 22, 2026 01:05
… in post-success hooks

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…o bytes without auth

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
… response

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@devin-ai-integration
devin-ai-integration Bot requested a review from a team September 22, 2026 01:14
@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@CLAassistant

CLAassistant commented Sep 22, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@greptile-apps

greptile-apps Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The PR appears safe to merge, with no outstanding correctness or repository-rule findings

Findings

  1. P2 Shared config bypass ▶

Summary

This PR corrects cost breakdowns for terminal Responses streaming events by pricing their inner response. It also updates integration fixtures for unmanaged response IDs and fal.ai video downloads, and adds focused regression coverage

  • Unwraps completed, incomplete, and failed terminal Responses events before reading pricing data
  • Preserves pricing overrides and additional headers from the inner response
  • Updates integration configuration and the fal.ai fake upstream to match the tested request flows
  • Verifies that terminal-event pricing equals direct inner-response pricing and produces a nonzero input cost

Reviews (2) · Last reviewed commit: "test(logging): drop docstring from termi..."

store_model_in_db: true
disable_spend_logs: false
proxy_batch_write_at: 1
allow_unmanaged_response_ids: true

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.

P2 Shared config bypass Enabling this here disables default unmanaged-ID rejection across shared-config integration tests, reducing full-proxy coverage. Scope it to the affected fixture.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

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.

This is a proxy-wide general_settings flag, not scopable per test. No shared-config integration test exercises unmanaged-id rejection, so coverage is unchanged.

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.

You're right. allow_unmanaged_response_ids is a proxy-wide setting in this integration configuration, so it cannot be scoped to a single test. Since the shared integration suite does not exercise unmanaged-ID rejection, enabling it here does not reduce existing coverage. This comment does not apply.

kerry-berri and others added 2 commits September 22, 2026 01:17
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

@codecov

codecov Bot commented Sep 22, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@codspeed

codspeed Bot commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing litellm_fix_responses_stream_cost_breakdown (2f6a9eb) with main (275c0c4)1

Open in CodSpeed

Footnotes

  1. No successful run was found on main (a59ecfb) during the generation of this report, so 275c0c4 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@mateo-berri

Copy link
Copy Markdown
Contributor

bugbot run

@cursor cursor 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.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 2f6a9eb. Configure here.

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.

3 participants