Repository navigation
fix(proxy): keep the proxy spend writer running on no-log requests - #44514
apex-mochen wants to merge 1 commit into
Conversation
|
e0839a2 to
991678d
Compare
Merging this PR will not alter performance
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
991678d to
bf90200
Compare
|
Both remaining check failures on this head are not caused by the PR diff: codecov/patch 0.00% — the four new tests drive every branch of the changed documentation / code-quality — |
bf90200 to
3573b93
Compare
|
Hi @hektorjg — this fixes #44503: the proxy spend writer ( |
3573b93 to
afb560c
Compare
PR overviewAll previously flagged issues have been addressed. No open security concerns remain on this pull request. Security reviewNo open security issues remain on this pull request. Fixed/addressed: 2 · PR risk: 0/10 |
|
Heads-up for reviewers: the |
0914d02 to
985edc9
Compare
hektorjg
left a comment
There was a problem hiding this comment.
Thanks for the ping. I tested this branch end to end, not just the unit tests, against a disposable proxy with a fake OpenAI upstream and fixed per-token prices. I compared three builds: v1.104.0, v1.104.0 with this diff applied (it applies cleanly), and an image built from this branch (985edc97507) with its own Dockerfile.
What this PR fixes (confirmed on the branch image)
Without the fix, on v1.104.0, every one of these no-log: true requests is served and charged $0, with no LiteLLM_SpendLogs row and no key/team spend. With this branch, all of them are charged and logged:
Request shape, "no-log": true in body |
v1.104.0 | this branch |
|---|---|---|
/v1/chat/completions |
$0, 0 rows | charged, 3/3 rows |
/v1/chat/completions with stream: true |
$0, 0 rows | charged, 3/3 rows |
chat → Responses bridge (openai/responses/...) |
$0, 0 rows | charged, 3/3 rows |
/v1/responses |
$0, 0 rows | charged, 3/3 rows |
/v1/embeddings |
$0, 0 rows | charged, 3/3 rows |
- Key and team
max_budgettrip at the real spend again. - With
store_prompts_in_spend_logs: true, the restored rows for no-log requests carry no prompt or response. - Key
rpm_limitbehaves the same before and after, since_PROXY_MaxParallelRequestsHandler_v3was already exempt.
One more thing the fix resolves on v1.104.0: there, a single no-log request on a key or team with a tight budget left the pre-call budget reservation unsettled, because the spend writer never ran. Later requests got 422 Budget has been exceeded with Current cost showing the reserved worst case instead of the real spend. With this diff the reservation is settled and blocking happens at the real figure.
Variants that do not trigger the bypass, all charged normally on v1.104.0: metadata.no-log, litellm_metadata.no-log, no_log, the string "true", and x-litellm-disable-callbacks without premium.
Gaps
-
RouterBudgetLimitingis still skipped on no-log requests, so deployment, provider and tag budgets can still be bypassed. Repro: a deployment withlitellm_params.max_budget: 0.05at $0.02 per call. On this branch, 6/6no-logcalls are served, while 6 normal calls get 3 × 200 and then 429.RouterBudgetLimiting(litellm/router_strategy/budget_limiter.py) does its accounting inasync_log_success_event, and its name doesn't match_PROXY_. Only its subclass_PROXY_VirtualKeyModelMaxBudgetLimiteris exempt. -
A name list is the same fragility that caused the bug. The exemption broke because the spend writer's class name didn't match a string. #45170 then had to keep the
_PROXY_*class names specifically to "preserve internal callback filtering". An explicit class attribute onCustomLoggeris harder to break by a rename, inherits to subclasses, and documents intent. It would mirrorenforces_request_content, which already exists for the same kind of decision. For example:class CustomLogger: runs_on_no_log_requests: bool = False # accounting callbacks opt in; exporters never do class _ProxyDBLogger(CustomLogger): runs_on_no_log_requests = True class RouterBudgetLimiting(CustomLogger): runs_on_no_log_requests = True
with
should_run_callbackcheckingcallback.runs_on_no_log_requests or "_PROXY_" in name(keeping the name check for back-compat). I have this, with regression tests, on a branch and can share it as a suggestion or a follow-up. With it applied on v1.104.0, the deployment-budget repro above blocks after 3 calls underno-logtoo. It only covers accounting, so it needs this PR's body suppression next to it. -
Body suppression ignores
global_disable_no_log_param._request_is_no_logdoesn't check it (as the bot noted). An admin who disabled the param would still get bodies suppressed for requests that send it. -
Two spend-log paths still read the proxy-wide setting instead of the per-request one: the
error_informationsanitizer (should_store_prompts_and_responses_in_spend_logs()with no args, aroundspend_tracking_utils.py:1471on this branch) and_get_vector_store_request_for_spend_logs_payload(around:1661). Withstore_prompts_in_spend_logs: true, a failing or vector-store-backedno-logrequest could still persist prompt fragments.
Since any virtual-key holder can set no-log in the request body, this is a budget-enforcement bypass rather than only a logging bug. It may be worth an advisory once a fix ships, so self-hosted operators know to upgrade or set litellm_settings.global_disable_no_log_param: true in the meantime. The docs ("Turn off all tracking/logging") don't mention the budget implication either way.
|
Thanks @hektorjg for the end-to-end run, and @agnaylor for the summary on #44503. Pushed Fixed in this PR
Tests: a parametrized matrix over Intentionally left for follow-ups
|
|
Thanks for your comments on #44514 — all checks are green on the current head. If you get a chance to re-look, happy to adjust anything that comes up. |
98c90cf to
68de195
Compare
68de195 to
963c607
Compare
|
Thanks @hektorjg — all four gaps are addressed on the new head
New tests: One caveat on verification: On your advisory point: I agree the bypass is budget enforcement rather than a logging bug. If you'd rather keep the code change minimal, I can move the advisory prose (security note + docs) into a follow-up issue and keep this PR to the filter and the sanitizer — your call. |
Rebased onto upstream main (e9b1c11). No-log requests must still record spend accounting; content stays redacted. Signed-off-by: apex-mochen <2756823972@qq.com>
bbf017d to
0451363
Compare
Fixes #44503
Logging.should_run_callback()lets proxy-internal callbacks run even onno-log: truerequests (the proxy still has to do its own spend accounting), but it detected them with a"_PROXY_" in callback.__class__.__name__check. The proxy's spend writer is_ProxyDBLogger-- lowercaseproxy, no_PROXY_marker -- so on successfulno-logrequests it was filtered out: the request completed, but nothing was written toLiteLLM_SpendLogsand key spend was never incremented.Fix: keep the internal
_PROXY_handler-prefix check for the_PROXY_*handler family, and add the spend writer through an explicit allowlist (_PROXY_INTERNAL_CALLBACK_CLASS_NAMES). No substring widening: an unrelated custom logger whose name merely contains "proxy" (e.g.My_proxyAuditLogger) is still suppressed onno-logrequests.Tests (mapped unit file
tests/unit/litellm_core_utils/test_litellm_logging.py):_ProxyDBLoggerthrough the real async success dispatch of ano-logrequest (only the DB write is stubbed) and asserts the spend accounting executes -- fails on base with 0 tracked events;My_proxyAuditLoggerstays suppressed onno-log, and a differential control shows the same logger still runs withoutno-log;_PROXY_*handlers keep running onno-log.Local: full mapped file 489 passed; remaining reds are pre-existing optional-dependency failures with an identical failure set on base.
ruff format --checkclean; offline delta gates (strict, type-discipline, test-quality) green.AI-assisted disclosure: generated with AI assistance.