Skip to content

fix(proxy): read a batched spend counter as a float - #52439

Open
narmstrong-berri wants to merge 1 commit into
mainfrom
nate_fix_batched_spend_counter_float
Open

narmstrong-berri wants to merge 1 commit into
mainfrom
nate_fix_batched_spend_counter_float

Conversation

@narmstrong-berri

@narmstrong-berri narmstrong-berri commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Handwritten 1-3 sentence description

[insert handwritten description here]

Details

TLDR

Problem this solves:

  • A budget refusal at exactly the budget reads Current cost: 1
  • Before the Rust budget checks it read Current cost: 1.0
  • The batched counter read skipped the float() the single read applies

How it solves it:

  • Both counter read paths now go through the same float()

User Flow

Before: a client whose key is out of budget gets a refusal text that changed shape

  1. The admin creates a key with max_budget: 1.0
  2. A call without max_tokens is in flight and holds the whole budget
  3. A second POST https://litellm-domain/v1/chat/completions with the same key answers 422
  4. The message ends Current cost: 1, Max budget: 1.0

After: the refusal keeps the format clients already parse

  1. The admin creates a key with max_budget: 1.0
  2. A call without max_tokens is in flight and holds the whole budget
  3. A second POST https://litellm-domain/v1/chat/completions with the same key answers 422
  4. The message ends Current cost: 1.0, Max budget: 1.0

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. 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 more
  • 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
  • Every automated review on my PR's latest commit has finished, and I have addressed or replied to each of its findings before requesting a maintainer review

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

E2E exception at 00501a1

Scope is read_spend_counter_cache_value in litellm/proxy/proxy_server.py. A spend counter is written with INCRBYFLOAT, so a counter holding exactly 1.0 comes back from Redis as 1 and 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 in native_budgets.observed_counter_spend reads every counter through get_current_spend inside that batch scope, so the int reached the Rust budget check, which prints it with to_string. The fix routes the batched result through the same float() as the unbatched one. Every caller of the function already declares float | None, so nothing else changes. The new test drives get_current_spend inside a batch scope against a counter holding 1, 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_budget and test_dec1_budgets_two_instances::test_a_hold_taken_on_one_instance_refuses_a_concurrent_call_on_the_other are skipped with BUG for it.

Commands, run from the repo root with PYTHONPATH set to it:

python -m pytest tests/unit/proxy/proxy_server/test_spend_counters.py::test_get_current_spend_inside_the_batch_scope_reads_an_integral_counter_as_a_float -q
python -m pytest tests/unit/proxy/proxy_server/test_spend_counters.py tests/unit/proxy/spend_tracking/test_spend_counter_batch.py -q
ruff check litellm/proxy/proxy_server.py tests/unit/proxy/proxy_server/test_spend_counters.py
ruff format --check litellm/proxy/proxy_server.py tests/unit/proxy/proxy_server/test_spend_counters.py

Without the proxy_server.py change the new test fails with AssertionError: 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_ttls pins Current cost: 1 and needs updating to 1.0

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

Generated by Claude Code

@veria-ai

veria-ai Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

PR overview

No security issues were found in this pull request.

Reviews

@devin-ai-integration devin-ai-integration Bot 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.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

Copy link
Copy Markdown
Contributor Author

[AI] @greptileai review


Generated by Claude Code

@greptile-apps

greptile-apps Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 4/5

[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

  1. P2 New test omits required types ▶

Summary

Batched spend reads now use the same float conversion as single reads, preserving decimal formatting for whole-number costs.

  • Budget refusals keep spend totals in float format.

The new test needs the annotations required by the repository guide.

Reviews (1) · Last reviewed commit: "fix(proxy): read a batched spend counter..." · Reviewed by Greptile

Comment on lines +90 to +94
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)

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 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!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[AI] Fixed in 5c97d73. monkeypatch is typed, the test returns None, and spend and result are Final.


Generated by Claude Code

@codecov

codecov Bot commented Nov 18, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@codspeed

codspeed Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

1 warning

⚠️ 2 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 45 untouched benchmarks


Comparing nate_fix_batched_spend_counter_float (5c97d73) with main (bb29e9a)1

Open in CodSpeed

Footnotes

  1. No successful run was found on main (4c8ae9b) during the generation of this report, so bb29e9a was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@narmstrong-berri
narmstrong-berri force-pushed the nate_fix_batched_spend_counter_float branch from a004777 to 00501a1 Compare November 18, 2026 05:19
@narmstrong-berri
narmstrong-berri force-pushed the nate_fix_batched_spend_counter_float branch from 00501a1 to 5c97d73 Compare November 18, 2026 21:13

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.

1 participant