Skip to content

fix(proxy): keep the proxy spend writer running on no-log requests - #44514

Open
apex-mochen wants to merge 1 commit into
BerriAI:mainfrom
apex-mochen:fix/no-log-keep-proxy-spend-logger
Open

apex-mochen wants to merge 1 commit into
BerriAI:mainfrom
apex-mochen:fix/no-log-keep-proxy-spend-logger

Conversation

@apex-mochen

@apex-mochen apex-mochen commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #44503

Logging.should_run_callback() lets proxy-internal callbacks run even on no-log: true requests (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 -- lowercase proxy, no _PROXY_ marker -- so on successful no-log requests it was filtered out: the request completed, but nothing was written to LiteLLM_SpendLogs and 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 on no-log requests.

Tests (mapped unit file tests/unit/litellm_core_utils/test_litellm_logging.py):

  • regression: drives the real _ProxyDBLogger through the real async success dispatch of a no-log request (only the DB write is stubbed) and asserts the spend accounting executes -- fails on base with 0 tracked events;
  • negative: My_proxyAuditLogger stays suppressed on no-log, and a differential control shows the same logger still runs without no-log;
  • guard: _PROXY_* handlers keep running on no-log.

Local: full mapped file 489 passed; remaining reds are pre-existing optional-dependency failures with an identical failure set on base. ruff format --check clean; offline delta gates (strict, type-discipline, test-quality) green.

AI-assisted disclosure: generated with AI assistance.

@apex-mochen
apex-mochen requested a review from a team October 4, 2026 16:38
@greptile-apps

greptile-apps Bot commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 4/5

[Medium risk] Fixes proxy spend tracking on no-log requests.

The PR should not merge until no-log requests cannot run unrelated custom loggers.

Findings

  1. P1 Unrelated loggers bypass no-log ▶
  2. P2 Spend regression remains untested ▶

Summary

The PR changes no-log callback filtering so the proxy spend writer remains eligible, and adds direct filter tests.

  • The broader name match also admits unrelated custom loggers with _proxy in their names.
  • The tests do not verify that a no-log request reaches the real spend writer.

Reviews (1) · Last reviewed commit: "[Bugfix][Proxy] Keep the proxy spend wri..."

@codspeed

codspeed Bot commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

⚠️ 1 benchmark spent significant time in system calls

System calls cannot be consistently instrumented, so they are not included in the measure, which understates the real cost. Please switch to the Walltime instrument to accurately measure system calls.

Measurement and system calls

✅ 32 untouched benchmarks


Comparing apex-mochen:fix/no-log-keep-proxy-spend-logger (68de195) with main (c9d081b)

Open in CodSpeed

@codecov

codecov Bot commented Oct 4, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@apex-mochen apex-mochen changed the title [Bugfix][Proxy] Keep the proxy spend writer running on no-log requests fix(proxy): Keep the proxy spend writer running on no-log requests Oct 4, 2026
@apex-mochen apex-mochen changed the title fix(proxy): Keep the proxy spend writer running on no-log requests fix(proxy): keep the proxy spend writer running on no-log requests Oct 4, 2026
@apex-mochen
apex-mochen force-pushed the fix/no-log-keep-proxy-spend-logger branch from 991678d to bf90200 Compare October 6, 2026 08:01
@apex-mochen

Copy link
Copy Markdown
Contributor Author

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 _proxy class-name check in should_run_callback (_ProxyDBLoggerStub and _PROXY_HandlerStub keep running under no-log, a plain CustomLogger is skipped, and no-log=False runs as before). Locally the full subset passes: pytest tests/unit/litellm_core_utils/test_litellm_logging.py -k no_log → 4 passed, so the changed line is executed. This looks like a Codecov base-report/merge hiccup rather than missing coverage; any fresh push will recompute it.

documentation / code-quality — test_env_keys.py fails with DEFER_PYDANTIC_BUILD read under ./litellm but mentioned nowhere in the docs. That env var is read by litellm main itself (not introduced by this diff), and the identical failure shows up on every open PR in the repo, so it is a repo-level baseline gap: main needs to register DEFER_PYDANTIC_BUILD in the docs before this job can go green for any contribution.

@apex-mochen
apex-mochen force-pushed the fix/no-log-keep-proxy-spend-logger branch from bf90200 to 3573b93 Compare October 7, 2026 08:27
@apex-mochen

Copy link
Copy Markdown
Contributor Author

Hi @hektorjg — this fixes #44503: the proxy spend writer (_ProxyDBLogger) was being filtered out of successful no-log: true requests by the _PROXY_ name check, so nothing was written to LiteLLM_SpendLogs and key spend was never incremented. The fix keeps the _PROXY_* handler check and adds an explicit allowlist (_PROXY_INTERNAL_CALLBACK_CLASS_NAMES), with no substring widening. Since you recently worked on spend logging in this same file (#44508), you're the best person to sanity-check the allowlist approach. 91 lines of mapped unit tests included.

@apex-mochen
apex-mochen force-pushed the fix/no-log-keep-proxy-spend-logger branch from 3573b93 to afb560c Compare October 8, 2026 13:48
@veria-ai

veria-ai Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

PR overview

All previously flagged issues have been addressed. No open security concerns remain on this pull request.

Security review

No open security issues remain on this pull request.

Fixed/addressed: 2 · PR risk: 0/10

@apex-mochen

Copy link
Copy Markdown
Contributor Author

Heads-up for reviewers: the rust-wheel (pytest tests/test_litellm_rust)
and rust-test (secret-manager feature combinations) failures on this run
are inside the Rust workspace and appear pre-existing on main — three
consecutive main pushes today (Rust runs #14903, #14916, #14958) show the
same red status. This PR only touches Python litellm/logging callbacks
plus Python unit tests. Happy to rebase once the Rust CI is green on main.

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

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_budget trip 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_limit behaves the same before and after, since _PROXY_MaxParallelRequestsHandler_v3 was 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

  1. RouterBudgetLimiting is still skipped on no-log requests, so deployment, provider and tag budgets can still be bypassed. Repro: a deployment with litellm_params.max_budget: 0.05 at $0.02 per call. On this branch, 6/6 no-log calls are served, while 6 normal calls get 3 × 200 and then 429. RouterBudgetLimiting (litellm/router_strategy/budget_limiter.py) does its accounting in async_log_success_event, and its name doesn't match _PROXY_. Only its subclass _PROXY_VirtualKeyModelMaxBudgetLimiter is exempt.

  2. 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 on CustomLogger is harder to break by a rename, inherits to subclasses, and documents intent. It would mirror enforces_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_callback checking callback.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 under no-log too. It only covers accounting, so it needs this PR's body suppression next to it.

  3. Body suppression ignores global_disable_no_log_param. _request_is_no_log doesn't check it (as the bot noted). An admin who disabled the param would still get bodies suppressed for requests that send it.

  4. Two spend-log paths still read the proxy-wide setting instead of the per-request one: the error_information sanitizer (should_store_prompts_and_responses_in_spend_logs() with no args, around spend_tracking_utils.py:1471 on this branch) and _get_vector_store_request_for_spend_logs_payload (around :1661). With store_prompts_in_spend_logs: true, a failing or vector-store-backed no-log request 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.

@apex-mochen

Copy link
Copy Markdown
Contributor Author

Thanks @hektorjg for the end-to-end run, and @agnaylor for the summary on #44503. Pushed d70f6dcbe5 plus a merge of latest main (98c90cfd75); no force-push.

Fixed in this PR

  1. Body suppression now honours litellm.global_disable_no_log_param (Veria thread / item 3): _request_is_no_log returns False when the admin has disabled the parameter, matching should_run_callback, so every content gate follows it.
  2. Vector-store results on no-log rows (item 4, second half): _get_vector_store_request_for_spend_logs_payload now receives the request's litellm_params, so they are redacted for no-log requests even with store_prompts_in_spend_logs: true.

Tests: a parametrized matrix over no-log × store_prompts_in_spend_logs × global_disable_no_log_param that asserts both content presence and that spend/tokens are recorded in every combination, plus a vector-store case. Reverting only spend_tracking_utils.py turns exactly 2 of them red (the admin-override case and the vector-store leak); with the fix, 18/18 pass. Unrelated-logger suppression (My_proxyAuditLogger) stays covered by the existing test. Locally: ruff 0.15.3 and the strict / LIT / TQ / OpenAPI / basedpyright delta gates are all OK against merge-base 102619d47a. The earlier lint / python failure was the new openapi_docs_gate.py step (#45843) missing from the old base; the merge brings it in.

Intentionally left for follow-ups

  • Error-information sanitizer on the failure hook (item 4, first half): that hook isn't filtered by should_run_callback, so the behaviour is the same on main today. Happy to send a separate PR.
  • RouterBudgetLimiting (item 1) and replacing the name allowlist with a runs_on_no_log_requests attribute (item 2): agreed that it's sturdier. Since @hektorjg already has that on a branch with tests, it seems cleaner as a follow-up on top of this PR than as growth here. Until then, the regression test drives the real _ProxyDBLogger through success dispatch, so a rename would fail CI instead of silently dropping spend tracking.

@apex-mochen

Copy link
Copy Markdown
Contributor Author

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.

@apex-mochen
apex-mochen force-pushed the fix/no-log-keep-proxy-spend-logger branch from 98c90cf to 68de195 Compare October 20, 2026 08:27
@apex-mochen
apex-mochen force-pushed the fix/no-log-keep-proxy-spend-logger branch from 68de195 to 963c607 Compare November 5, 2026 10:45
@apex-mochen

Copy link
Copy Markdown
Contributor Author

Thanks @hektorjg — all four gaps are addressed on the new head bbf017d5c.

  1. RouterBudgetLimiting is no longer skipped. It now sets the opt-in attribute, so deployment / provider / tag budgets keep seeing no-log traffic.
  2. Name list → attribute, as you suggested. CustomLogger.runs_on_no_log_requests: bool = False sits next to enforces_request_content, is set True on _ProxyDBLogger and RouterBudgetLimiting, and the filter reads callback.runs_on_no_log_requests or name.startswith("_PROXY_"). The _PROXY_INTERNAL_CALLBACK_CLASS_NAMES constant is deleted; the prefix check stays only as back-compat for hooks that predate the attribute.
  3. global_disable_no_log_param was already short-circuited in _request_is_no_log(), so an admin who turns the parameter off gets prompt storage back.
  4. The error_information sanitizer is now per-request. sanitize_error_information_for_spend_logs(..., request_litellm_params=...) delegates to should_store_prompts_and_responses_in_spend_logs(litellm_params), which short-circuits on no-log. The vector-store payload path had the same fix in the previous push.

New tests: test_no_log_runs_callback_that_opts_in_by_attribute, test_no_log_filters_default_callback_without_the_attribute (control returning True without no-log), test_accounting_callbacks_are_marked_in_the_real_classes, and test_error_information_follows_request_no_log over the no-log × global_disable_no_log_param matrix. The error bodies are shaped as input_value=... and 'messages': [...], the two forms the redactor actually recognises — I checked that directly by exercising the extracted _redact_prompt_leaks_in_error_string against both payloads rather than asserting on a shape it ignores.

One caveat on verification: import litellm on this machine stops at litellm.rust_bridge._native (no local build of the native bridge), so the suites themselves are running only in CI here. If CI comes back green I'll leave the head frozen; if it flags anything I'll fix it in place.

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>
@apex-mochen
apex-mochen force-pushed the fix/no-log-keep-proxy-spend-logger branch from bbf017d to 0451363 Compare November 7, 2026 02:48

This branch has not been deployed

No deployments
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.

[Bug]: no-log: true skips the proxy's own spend tracking (_ProxyDBLogger) on successful requests

2 participants