Skip to content

fix(responses): avoid duplicate spend logs in async chat bridge - #42977

Closed
sorgfresser wants to merge 2 commits into
BerriAI:mainfrom
sorgfresser:litellm_fix_responses_duplicate_spend
Closed

sorgfresser wants to merge 2 commits into
BerriAI:mainfrom
sorgfresser:litellm_fix_responses_duplicate_spend

Conversation

@sorgfresser

@sorgfresser sorgfresser commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • Async Responses-to-Chat requests can create duplicate spend logs
  • One user request appears twice under the same call ID

How it solves it:

  • Marks only the nested Chat Completions request as internal
  • Always restores the request context, including on errors

User Flow

Before: a non-streaming Responses request succeeds, but the proxy records its spend twice

  1. An app sends POST /v1/responses through a chat-only provider
  2. The app receives one successful Responses API result
  3. The admin opens /ui/?page=logs and sees two rows for the same call ID

After: the same request succeeds and produces one spend record

  1. An app sends POST /v1/responses through a chat-only provider
  2. The app receives one successful Responses API result
  3. The admin opens /ui/?page=logs and sees one row for the call ID

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; it only solves 1 specific problem
  • I have received a Greptile Confidence Score of at least 4/5

Screenshots / Proof of Fix

Before (bf01870)

  1. Ran the successful OpenAI Responses-to-Chat bridge regression against the parent commit
  2. The test failed with Expected one spend log, got 2

After (f9703e3)

  1. Ran the same successful bridge regression at the rebased PR tip
  2. The test passed with exactly one spend callback
  3. The full mapped test file passed: 6 passed
  4. CI reported 100% patch coverage

Type

Bug Fix

Final Attestation

  • The tests cover successful spend logging, nested-call marking, and exception-safe context restoration

@codecov

codecov Bot commented Sep 24, 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 24, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing sorgfresser:litellm_fix_responses_duplicate_spend (f9703e3) with main (22b36cb)

Open in CodSpeed

@sorgfresser
sorgfresser force-pushed the litellm_fix_responses_duplicate_spend branch from b96edf7 to 03d8595 Compare September 24, 2026 16:29
@sorgfresser
sorgfresser marked this pull request as ready for review September 24, 2026 16:40
@sorgfresser
sorgfresser requested a review from a team September 24, 2026 16:40
@greptile-apps

greptile-apps Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 4/5

The PR appears safe to merge, though its spend-log outcome lacks a direct regression assertion.

Findings

  1. P2 Successful spend logging remains untested ▶

Summary

The PR marks the nested async Chat Completions call as internal while it runs and restores the previous context even if it raises.

  • The changed test verifies the marker and exception-path restoration, but does not assert the successful spend-log outcome.

Reviews (1) · Last reviewed commit: "fix(responses): suppress duplicate bridg..."

@sorgfresser
sorgfresser force-pushed the litellm_fix_responses_duplicate_spend branch from 03d8595 to 23fdbe7 Compare September 24, 2026 22:57
@sorgfresser
sorgfresser force-pushed the litellm_fix_responses_duplicate_spend branch from 23fdbe7 to f9703e3 Compare September 27, 2026 16:35
@mateo-berri

Copy link
Copy Markdown
Contributor

Thanks Simon. #44508 fixed this on main (in v1.106.0-rc.1), and your spend-once test passes there without the handler change, so I'm closing this as superseded

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.

2 participants