Repository navigation
refactor(auth): derive temp budget increase without mutating the token - #34121
Merged
ryan-crabbe-berri merged 3 commits intoJul 21, 2026
Merged
Conversation
…th datetimes _update_key_budget_with_temp_budget_increase mutated max_budget in place, so correctness depended on every resolution path handing it a fresh copy of the cached token; one future re-cache of a live token would compound the bump per request. Return a model_copy instead so no caller can leak an increased budget into shared state. Also fixes the three remaining DTZ005 naive datetime.now() calls in user_api_key_auth.py (auth span start, builder start_time, service-log end_time; all consumers convert to epoch or subtract same-pair datetimes) and ratchets the DTZ005 strict budget 244 -> 241.
Contributor
Greptile SummaryThis PR prevents temporary budget increases from mutating cached authentication tokens. The main changes are:
Confidence Score: 5/5This looks safe to merge.
|
| Filename | Overview |
|---|---|
| litellm/proxy/auth/user_api_key_auth.py | Derives temporary budget increases without mutating the input token and uses UTC-aware authentication timestamps. |
| tests/proxy_unit_tests/test_proxy_utils.py | Verifies that budget derivation returns a distinct object and preserves the input budget. |
| tests/test_litellm/proxy/auth/test_user_api_key_auth.py | Verifies repeated cache-hit authentication does not compound or persist the temporary increase. |
| ruff-strict-budget.json | Updates the strict lint budget after replacing three naive datetime calls. |
Reviews (2): Last reviewed commit: "test: pin non-mutation of the temp budge..." | Re-trigger Greptile
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Adversarial mutation-testing showed reverting the helper to in-place mutation still passed every test: the cache's copy-on-read layer masks the mutation in the integration test and the direct unit test only inspected the return value. Assert the input object is left untouched and the result is a distinct object so the purity guarantee itself is load-bearing.
ryan-crabbe-berri
force-pushed
the
litellm_temp_budget_purity_dtz
branch
from
July 21, 2026 18:41
1f27f05 to
f219ffe
Compare
Contributor
Author
|
@greptileai re review |
yuneng-berri
approved these changes
Jul 21, 2026
ryan-crabbe-berri
enabled auto-merge (squash)
July 21, 2026 20:50
…itellm_temp_budget_purity_dtz
ryan-crabbe-berri
merged commit Jul 21, 2026
76c9eca
into
litellm_internal_staging
77 of 78 checks passed
yuneng-berri
pushed a commit
that referenced
this pull request
Jul 26, 2026
#34121) * refactor(auth): derive temp budget bump without mutation, tz-aware auth datetimes _update_key_budget_with_temp_budget_increase mutated max_budget in place, so correctness depended on every resolution path handing it a fresh copy of the cached token; one future re-cache of a live token would compound the bump per request. Return a model_copy instead so no caller can leak an increased budget into shared state. Also fixes the three remaining DTZ005 naive datetime.now() calls in user_api_key_auth.py (auth span start, builder start_time, service-log end_time; all consumers convert to epoch or subtract same-pair datetimes) and ratchets the DTZ005 strict budget 244 -> 241. * test: pin non-mutation of the temp budget helper input Adversarial mutation-testing showed reverting the helper to in-place mutation still passed every test: the cache's copy-on-read layer masks the mutation in the integration test and the direct unit test only inspected the return value. Assert the input object is left untouched and the result is a distinct object so the purity guarantee itself is load-bearing. (cherry picked from commit 76c9eca)
yuneng-berri
added a commit
that referenced
this pull request
Jul 28, 2026
ap-anton-r-susilo
pushed a commit
to ap-anton-r-susilo/litellm
that referenced
this pull request
Jul 29, 2026
BerriAI#34121) * refactor(auth): derive temp budget bump without mutation, tz-aware auth datetimes _update_key_budget_with_temp_budget_increase mutated max_budget in place, so correctness depended on every resolution path handing it a fresh copy of the cached token; one future re-cache of a live token would compound the bump per request. Return a model_copy instead so no caller can leak an increased budget into shared state. Also fixes the three remaining DTZ005 naive datetime.now() calls in user_api_key_auth.py (auth span start, builder start_time, service-log end_time; all consumers convert to epoch or subtract same-pair datetimes) and ratchets the DTZ005 strict budget 244 -> 241. * test: pin non-mutation of the temp budget helper input Adversarial mutation-testing showed reverting the helper to in-place mutation still passed every test: the cache's copy-on-read layer masks the mutation in the integration test and the direct unit test only inspected the return value. Assert the input object is left untouched and the result is a distinct object so the purity guarantee itself is load-bearing. (cherry picked from commit 76c9eca)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Relevant issues
Follow-up hardening of #33841 (issue #25760)
Linear ticket
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
@greptileaito re-request a review after pushing changes)Delays in PR merge?
If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).
Screenshots / Proof of Fix
Run live at commit
1b456994deagainst a local proxy from this branch (Postgres-backed,dev_config.yaml). The budget gate decision this PR touches happens at auth time, before any provider call, so the proof drives it directly: a key is givenspendfar over itsmax_budgetvia/key/update, then requests are sent while varyingtemp_budget_increase. A 429 naming "Budget has been exceeded" means the gate blocked; any other error (this environment has no provider key wired) means the request cleared the gateOutput:
Phase 3 is the purity proof: five consecutive cache-hit requests all report the effective budget as exactly 0.0101. Under a compounding bug (mutated token leaking back into the cache) that number would grow by 0.01 per request.
key/infoconfirms the storedmax_budgetis still the original 0.0001 after everythingType
🧹 Refactoring
Changes
_update_key_budget_with_temp_budget_increasemutatedvalid_token.max_budgetin place. That is only safe because every auth resolution path happens to hand it a fresh copy of the cached token (cache reads returnmodel_copy(),_cache_key_objectcopies before storing). If any future change re-caches a live token or drops one of those copies, the increase would compound into shared state by+increaseper request. The helper now returnsmodel_copy(update={"max_budget": ...})instead, so no caller can leak an increased budget anywhere, regardless of what the cache doesThe regression test from #33841 is extended to pin this as an observable invariant: resolving the same cached key twice must yield the effective budget (102.0) both times, and the cached object must still hold the original 2.0 afterwards. Adversarial mutation-testing during review showed that alone was not enough; reverting the helper to in-place mutation still passed because the cache's copy-on-read layer masks it, so the direct unit test in test_proxy_utils.py now also asserts the input object is left unmutated and the result is a distinct object
While in the file, the three remaining
DTZ005naivedatetime.now()calls are fixed (auth span start, builderstart_time, service-logend_time); all their consumers either convert to epoch via.timestamp()or subtract same-pair datetimes, so switching todatetime.now(timezone.utc)is behavior-preserving. TheDTZ005strict-budget ceiling ratchets 244 -> 241Note for reviewers:
make pre-commitcurrently fails on staging inlint-e2e-basedpyrightbecausetests/e2e/llm_translation/test_messages_mid_conversation_system_native_providers_e2e.pyreferences agatewayattribute thatEndpointsClientdoes not have (introduced in 23b5b7d); that is unrelated to this diff and tracked separatelyFinal Attestation