Repository navigation
fix(proxy): read a batched spend counter as a float - #52439
narmstrong-berri wants to merge 1 commit into
Conversation
|
[AI] @greptileai review Generated by Claude Code |
[Low impact] The change is limited to how budget refusals display spend totals. Satisfy the test annotation requirement before merging. No behavioral defect was found in the conversion. Findings
|
| async def test_get_current_spend_inside_the_batch_scope_reads_an_integral_counter_as_a_float(monkeypatch): | ||
| spend = _spend_counters(monkeypatch, redis={"spend:key:abc": 1}) | ||
|
|
||
| with spend_counter_batch_scope(spend.store(), frozenset({"spend:key:abc"})): | ||
| result = await ps.get_current_spend(counter_key="spend:key:abc", fallback_spend=0.0) |
There was a problem hiding this comment.
New test omits required types.
AGENTS.md requires typed parameters and Final locals. Type monkeypatch and annotate spend and result.
Context Used: AGENTS.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!
There was a problem hiding this comment.
[AI] Fixed in 5c97d73. monkeypatch is typed, the test returns None, and spend and result are Final.
Generated by Claude Code
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Merging this PR will not alter performance1 warning
Comparing Footnotes |
a004777 to
00501a1
Compare
00501a1 to
5c97d73
Compare
Handwritten 1-3 sentence description
[insert handwritten description here]
Details
TLDR
Problem this solves:
Current cost: 1Current cost: 1.0float()the single read appliesHow it solves it:
float()User Flow
Before: a client whose key is out of budget gets a refusal text that changed shape
max_budget: 1.0max_tokensis in flight and holds the whole budgetCurrent cost: 1, Max budget: 1.0After: the refusal keeps the format clients already parse
max_budget: 1.0max_tokensis in flight and holds the whole budgetCurrent cost: 1.0, Max budget: 1.0Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
uv run pytest tests/unit/<your_test_file>.py -v. Leave the suites (make test-unit-*,make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or moreDelays 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
E2E exception at 00501a1
Scope is
read_spend_counter_cache_valueinlitellm/proxy/proxy_server.py. A spend counter is written withINCRBYFLOAT, so a counter holding exactly 1.0 comes back from Redis as1and the native store decodes it as an int. The unbatched branch already turned it into a float, but the batched branch (the one MGET the admission check shares across counters) returned the value as is. The native budget admission innative_budgets.observed_counter_spendreads every counter throughget_current_spendinside that batch scope, so the int reached the Rust budget check, which prints it withto_string. The fix routes the batched result through the samefloat()as the unbatched one. Every caller of the function already declaresfloat | None, so nothing else changes. The new test drivesget_current_spendinside a batch scope against a counter holding1, the same path the admission check takes, and checks the text it formats. That covers the whole change, so no live proxy run was done.The bug was found by the Dec 1 integration contract tests (draft PR #50853), where
test_dec1_budgets_admission_hold::test_a_concurrent_call_is_refused_while_another_call_holds_the_rest_of_the_key_budgetandtest_dec1_budgets_two_instances::test_a_hold_taken_on_one_instance_refuses_a_concurrent_call_on_the_otherare skipped with BUG for it.Commands, run from the repo root with
PYTHONPATHset to it:Without the
proxy_server.pychange the new test fails withAssertionError: assert 'Current cost: 1' == 'Current cost: 1.0'. With it the test passes, and both spend counter test files pass (126 passed). Ruff reports no new findings on the changed files and the formatter has nothing to change in the new lines.Type
Bug Fix
Caveats (if any)
Low
test_dec1_budgets2_hold_lifecycle::test_a_long_hold_still_refuses_a_concurrent_call_after_several_ttlspinsCurrent cost: 1and needs updating to1.0Final Attestation
Generated by Claude Code