Skip to content

chore(release): backport #44508 to stable/1.102.x and cut 1.102.4 - #45215

Merged
yuneng-berri merged 3 commits into
stable/1.102.xfrom
litellm_backport_1_102_x_bp44508
Oct 8, 2026
Merged

yuneng-berri merged 3 commits into
stable/1.102.xfrom
litellm_backport_1_102_x_bp44508

Conversation

@yuneng-berri

@yuneng-berri yuneng-berri commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • Large non-streaming chat requests to openai/responses/* models log twice on 1.102.x
  • Key, team and daily spend get charged twice for one request

How it solves it:

User Flow

Before: a developer sends one large chat request and is billed twice in LiteLLM's own accounting

  1. They send POST https://litellm-domain/v1/chat/completions to an openai/responses/gpt-5.6-luna model with ~300k characters of message text, no streaming
  2. The response comes back normally, and the provider bills one call
  3. https://litellm-domain/ui/?page=logs shows two rows for the request, and the key budget drops by twice the real cost

After: the same request is logged once

  1. They send the same POST https://litellm-domain/v1/chat/completions
  2. The response comes back normally
  3. The logs page shows one row, and the key budget drops by the real cost

Relevant issues

Backports #44508 (fixes #44500) from main onto stable/1.102.x and bumps the line to 1.102.4. The bug is present on 1.102.x: the success handler checks its "already logged" flag, then awaits the large-message offload, and only sets the flag afterwards, so both nested wrappers log the same request

What is included

Adaptation notes

#44508 is adapted, not verbatim. 1.102.x predates _schedule_async_success_logging (added on main later by 22b377f, which is on 1.103.x and up), so the claim is called inline in the line's _dispatch_success_logging instead. It guards only the immediate (non-deferred) branch. The deferred branch, used when a post-call guardrail holds logging until after the guardrail runs, is left exactly as 1.102.x ships it: that path already logs once, and moving it to the inner result belongs to 22b377f, which this PR does not backport

claim_async_success_log and _async_success_scheduled are copied verbatim into Logging, placed before has_run_logging (main renamed it mark_logging_complete)

The two unit tests moved from tests/unit/ to tests/test_litellm/, which is where this line keeps them; only the PR's own test hunks were taken. The integration test tests/integration/spend/test_responses_bridge_spend_once.py is dropped because the tests/integration/_support harness it imports does not exist on 1.102.x. The live proxy run below covers the same assertions (one spend row, key charged once, the row is the inner Responses result)

test_background_interaction_completion_logs_while_in_progress_handler_is_parked is upstream's own test and is kept as is. On this line it passes with or without the fix, since background completion calls the success handler directly and never goes through the claim, so it guards against a future regression rather than proving this change

Known noise on this line

These fail on the untouched stable/1.102.x tip too and are unrelated (model capability lookups):

  • tests/test_litellm/test_utils.py::TestProxyFunctionCalling::test_proxy_function_calling_support_consistency (groq/gemma-7b-it, groq/llama-3.3-70b-versatile)
  • tests/test_litellm/test_utils.py::test_supports_function_calling_github_anthropic_alias
  • tests/test_litellm/test_utils.py::test_supports_tool_choice_simple_tests

With the pick, the two mapped files go from 4 failed / 671 passed to 4 failed / 673 passed. The new test_nested_wrapper_exits_schedule_one_async_success_log fails (assert 2 == 1) with the claim removed from _dispatch_success_logging

Pre-Submission checklist

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. uv run pytest tests/test_litellm/<your_test_file>.py -v
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • 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 before requesting a maintainer review

Screenshots / Proof of Fix

Adversarial review (gauntlet-mini, 5 lenses, 13 min) returned SURVIVED with 0 confirmed regressions. Its own runs reproduced the double log without the fix (2 events, 3 of 3 runs) and one inner ResponsesAPIResponse event with it, and found the same log counts with and without the fix for small requests, streaming, plain chat, retries, fallbacks, sync calls and cache hits. The new nested-wrapper test kills 5 of 5 mutants of the claim

Shared setup: the worktree's own proxy (python litellm/proxy/proxy_cli.py --config config.yaml) backed by a fresh Postgres, real OpenAI calls, and this config:

model_list:
  - model_name: luna-bridge
    litellm_params:
      model: openai/responses/gpt-5.6-luna
      api_key: os.environ/OPENAI_API_KEY
general_settings:
  master_key: sk-bp44508-master

small.json is a ~2,000-char prompt and large.json a ~300,000-char prompt (random words plus "Reply OK"), both {"model": "luna-bridge", "messages": [{"role": "user", "content": "..."}]} with no streaming

Before (d174b05)

Small prompt

  1. curl -s http://localhost:4061/key/generate -H 'Authorization: Bearer sk-bp44508-master' -d '{"key_alias":"bp44508-base"}' returns a new key
  2. curl -s http://localhost:4061/v1/chat/completions -H "Authorization: Bearer $KEY" -d @small.json returns OK, 334 prompt tokens, x-litellm-response-cost: 8.96e-05
  3. One LiteLLM_SpendLogs row for it at 8.96e-05

Large prompt

  1. curl -s http://localhost:4061/v1/chat/completions -H "Authorization: Bearer $KEY" -d @large.json returns OK, 48705 prompt tokens, x-litellm-response-cost: 0.0121821
  2. Two LiteLLM_SpendLogs rows for the one request, both at 0.0121821: one with the raw upstream id resp_09ff5d8c..., one with the encoded id resp_bGl0ZWxsbTpj...
  3. select key_alias, spend from "LiteLLM_VerificationToken" shows bp44508-base | 0.0244538, which is 8.96e-05 + 2 x 0.0121821, so the large request was charged twice

After (346cbb9)

Small prompt

  1. curl -s http://localhost:4061/key/generate -H 'Authorization: Bearer sk-bp44508-master' -d '{"key_alias":"bp44508-after"}' returns a new key
  2. curl -s http://localhost:4061/v1/chat/completions -H "Authorization: Bearer $KEY" -d @small.json returns OK, 334 prompt tokens, x-litellm-response-cost: 7.28e-05
  3. One LiteLLM_SpendLogs row for it at 7.28e-05

Large prompt

  1. curl -s http://localhost:4061/v1/chat/completions -H "Authorization: Bearer $KEY" -d @large.json returns OK, 48705 prompt tokens, x-litellm-response-cost: 0.00098064
  2. One LiteLLM_SpendLogs row for the request at 0.00098064, with the encoded id resp_bGl0ZWxsbTpj..., so the inner Responses result is the one logged
  3. select key_alias, spend from "LiteLLM_VerificationToken" shows bp44508-after | 0.00105344, which is exactly 7.28e-05 + 0.00098064, so each request was charged once

The per-request cost is lower in After because OpenAI served the repeated prompt from its cache; the row and charge counts are the signal

Type

🐛 Bug Fix
✅ Test

Caveats (if any)

Medium

  • Deferred logging (post-call guardrail) still logs the outer wrapper's result
    • Unchanged from 1.102.x today; switching it is 22b377f, not backported
  • If the single queued handler is cancelled, the other wrapper does not retry
    • Same as main

Low

  • Integration test dropped; its harness is missing on 1.102.x

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

hektorjg and others added 3 commits October 7, 2026 17:39
…chat to Responses bridge (#44508)

* fix(logging): claim the async success dedup flag before the first await

Nested @client wrappers on the chat -> Responses bridge schedule two async
success tasks on one logging object. The dedup flag was checked at the top
of _async_success_handler_body but only set after several awaits; the
worker-thread base64 offload for messages >= 256 KiB yields there, so the
second task passed the same check and the request was logged twice
(two SpendLogs rows, key/team/daily spend counted twice).

Fixes #44500

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix(logging): serialise async success handlers per logging object instead of claiming early

Claiming the dedup flag before the first await (previous commit) let a first
handler that is cancelled before its callbacks run (e.g. by the logging
worker's per-coroutine timeout) suppress the second one, losing the request's
success accounting. Serialise non-streaming handlers on the same Logging
object with an asyncio.Lock kept in a module-level WeakKeyDictionary: the
second handler waits, then skips if the first logged, or logs itself if the
first raised or was cancelled first. Streaming chunk logging is not locked.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* test(integration): pin one spend row and one key charge for large bridged chat completions

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

* test(integration): wait for the large request's spend row before asserting

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

* test(logging): use fixed timestamps in async success dedup tests

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

* style(logging): trim the async success dedup lock comment

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

* fix(logging): reset background interaction dedupe under the success dedup lock

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

* test(logging): use a fixed start time in the interactions logging helper

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

* fix: claim the async success log at schedule time instead of locking the handler

Nested @client wrappers (chat over the Responses bridge, Anthropic Messages over the chat adapter) exit with one shared Logging object and each queued their own async success handler. The handler checked has_logged_async_success before its awaited work and set it after, so both could pass the check and the request was logged and billed twice.

_schedule_async_success_logging now claims the log on the Logging object synchronously through claim_async_success_log, and a later wrapper returns without enqueuing. There is no await between the check and the decision, so no lock is needed. The per-object asyncio.Lock, the WeakKeyDictionary that held it and the locked block in async_log_background_interaction_completion are gone.

Tests: the regression test moves to tests/unit/test_utils.py and drives two _dispatch_success_logging exits with one Logging object through the logging worker, asserting one log with the inner result. It fails without the claim. The cancelled-handler test is dropped since a second wrapper no longer retries. The background interaction completion test stays.
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* test(logging): pin the background completion event order and name the innermost wrapper in the claim docstrings

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

* test(integration): send one bridged request and assert the inner Responses result is the logged one

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

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-authored-by: kerry <kerry@berri.ai>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
(cherry picked from commit 157d96d)
@yuneng-berri
yuneng-berri requested a review from a team October 8, 2026 00:41
@greptile-apps

greptile-apps Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 4/5

[Medium risk] Fixes duplicate async logging in nested wrapper calls.

The logging fix appears sound, but required test annotations must be completed before merging

Findings

  1. P2 New tests miss required types ▶
  2. P2 Settlement test skips the claim ▶

Summary

Backports the fix for duplicate success logs on non-streaming chat requests bridged to Responses and prepares 1.102.4

  • The first wrapper claims async success logging before scheduling work
  • Adds tests for nested wrappers and background interaction settlement
  • Required test annotations remain incomplete, and the settlement test should set the new claim flag
  • yuneng-berri explicitly leaves deferred outer-result logging unchanged, accepts no retry after handler cancellation, and omits the integration test because its harness is absent

Reviews (1) · Last reviewed commit: "chore: refresh uv.lock for 1.102.4" · Reviewed by Greptile

super().__init__()
self.logged_results: list[object] = []

async def async_log_success_event(self, kwargs, response_obj, start_time, end_time) -> 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.

P2 New tests miss required types

The new callback and helper parameters are untyped in both test files, and calls lacks Final. CLAUDE.md requires strongly typed parameters and Final variables. Add the missing annotations before merging

Context Used: CLAUDE.md (source)

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!

Comment on lines +7073 to +7075
first: Final = asyncio.create_task(logging_obj.async_success_handler(result=in_progress))
await in_progress_parked.wait()
completion: Final = asyncio.create_task(logging_obj.async_log_background_interaction_completion(result=completed))

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 Settlement test skips the claim

The initial handler runs directly, so claim_async_success_log never sets the new flag. This test would still pass if settlement were wrongly dropped whenever that flag was set. Claim the initial log before parking its handler to cover that state

Knowledge Base Used: Logging callbacks and telemetry

@yuneng-berri
yuneng-berri merged commit 0de9a17 into stable/1.102.x Oct 8, 2026
11 checks passed
@yuneng-berri
yuneng-berri deleted the litellm_backport_1_102_x_bp44508 branch October 8, 2026 01:23
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