Skip to content

test(responses): audit #47504 - #48787

Merged
mateo-berri merged 1 commit into
BerriAI:mainfrom
norkelbyte:litellm_audit_47504
Oct 28, 2026
Merged

mateo-berri merged 1 commit into
BerriAI:mainfrom
norkelbyte:litellm_audit_47504

Conversation

@norkelbyte

@norkelbyte norkelbyte commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Post-merge audit of #47504, which made a background response the poller bills count toward the creating key's project and toward the key's, team's, user's, and budget tier's per-model budgets. Every audited behavior holds at the merge commit and at this branch's tip, and each budget behavior fails at its parent

This adds one integration test, for a team-only model's budget under the name the caller used, which a mutation run showed the existing cells miss. No product code changes

Original

TLDR

Problem this solves:

How it solves it:

User Flow

This PR only adds a test. The flow below is the one #47504 changed, as observed in this audit

Before (06e3e32): a developer's background responses slip past their key's per-model budget

  1. The admin creates a project key with a per-model budget of half one response's price via POST http://localhost:4000/key/generate
  2. The developer sends a background POST http://localhost:4000/v1/responses and reads GET http://localhost:4000/v1/responses/{id} until it is completed
  3. A key with a user behind it adds no project spend
  4. The next background create on that model still returns 200

After (d776277): the same response counts toward the project and the model budget

  1. The admin creates the same key
  2. The developer sends the same request and reads it until it is completed
  3. The project's spend goes up with the key's
  4. The next background create is refused with exceeded budget for model=<model>, also for a team, user, or budget tier budget and a team model called by its public name

Relevant issues

Follow-up audit of #47504

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

Audit verdict PASS. Base is #47504's parent 06e3e32c88, head is its merge commit d776277647, and the cells also ran at this branch's tip f94e324199. Every cell ran in the accounting integration group as CircleCI runs it, with this branch's tests laid over both commits. R2 also ran live on OpenAI gpt-5.4-nano through a proxy at the parent and one at the tip

Row Contract Base Head Tip
R1 Project spend moves FAIL PASS x2 PASS x2
R2 Key model budget FAIL PASS x2 PASS x2
R2 live Key model budget, OpenAI FAIL n/a PASS
R3 Team model budget FAIL PASS x2 PASS x2
R4 User model budget FAIL PASS x2 PASS x2
R5 Budget tier, key sets none FAIL PASS x2 PASS x2
R6 Team model by public name FAIL PASS x2 PASS x2
R7 Model group selection rules n/a PASS (unit) PASS (unit)
R8 Batch billing, shared key lookup n/a PASS PASS
R9 Background billed once n/a FAIL, inherited PASS

R1 runs in every R2 to R6 cell, and only the user cell shows it at base, since base's cost tracking filled in the project for a key with no alias and no user. R6 is the new cell, and with the merge commit's requested-model check forced off it failed while R2 passed. R7 is #47504's unit table. R9 is 5 existing cells that charged the key twice at the merge commit, the same red main's CircleCI showed until #47510 fixed it after this merge

Before (06e3e32)

Project and per-model budgets

  1. uv run --no-sync python tests/integration/run.py accounting 'tests/integration/spend/test_background_response_billed_once.py::test_a_billed_background_response_counts_toward_its_creators_project_and_model_budget[key]' ...[team] ...[user] ...[budget_tier] 'tests/integration/spend/test_background_response_billed_once.py::test_a_billed_background_response_on_a_team_model_counts_toward_the_budget_of_the_model_name_it_was_created_with'
  2. 5 collected, 0 passed, 5 failed. [user] stops at the project check with key spend 0.066 and project spend 0.0, and the other four stop because the next background create keeps returning 200 OK

Key model budget, live on OpenAI

Steps
  1. A proxy at 06e3e32c88 with openai/gpt-5.4-nano, Postgres, and proxy_batch_polling_interval: 1
  2. Run the flow below with a key whose budget for the model is below one response's price
$ curl -s -X POST $PROXY/key/generate -H "Authorization: Bearer $MASTER_KEY" -d '{"model_max_budget": {"gpt-5.4-nano": {"max_budget": 0.0000001, "budget_duration": "1d"}}}'
key created
$ curl -s -X POST $PROXY/v1/responses -H "Authorization: Bearer $KEY" -d '{"model": "gpt-5.4-nano", "input": "Say hi in two words", "background": true}' | jq -c '{id, status, model, error: .error.message}'
{"id":"resp_...","status":"queued","model":"gpt-5.4-nano","error":null}
$ curl -s $PROXY/v1/responses/$ID -H "Authorization: Bearer $KEY" | jq -c '{status, text: ([.output[]? | select(.type=="message") | .content[]?.text] | join(" ")), total_tokens: .usage.total_tokens}'  # polled every 2s until completed
{"status":"completed","text":"Hi there","total_tokens":17}
$ curl -s "$PROXY/key/info?key=$KEY" -H "Authorization: Bearer $MASTER_KEY" | jq .info.spend
0.0000194
$ for i in $(seq 1 12); do c=$(curl -s -o next.json -w '%{http_code}' -X POST $PROXY/v1/responses -H "Authorization: Bearer $KEY" -d '{"model": "gpt-5.4-nano", "input": "Say hi in two words", "background": true}'); printf '%s ' $c; [ $c != 200 ] && break; sleep 5; done; echo; jq -c '{status, error: .error.message}' next.json
200 200 200 200 200 200 200 200 200 200 200 200
{"status":"queued","error":null}

After (d776277)

Project and per-model budgets

  1. The same command, twice with seed 4106602
  2. 5 collected, 5 passed on both runs

Controls

Steps
  1. uv run --no-sync python tests/integration/run.py accounting with the 5 billed-once cells in test_background_response_billed_once.py and test_completed_batch_spend_lands_under_batches_in_team_endpoint_activity, test_batch_cost_row_is_filtered_and_charged_once
  2. The 2 batch cells pass. The 5 billed-once cells fail with key spend 0.066 for a 0.033 response, the inherited double charge

After (f94e324, this branch)

Project and per-model budgets

  1. The same command, twice with seed 4106603
  2. 5 collected, 5 passed on both runs, and tests/unit/proxy/common_utils/test_check_responses_cost.py passes 80 of 80

Controls

Steps
  1. The same 7 control cells
  2. 7 collected, 7 passed, so the double charge the merge commit showed is gone here

Key model budget, live on OpenAI

Steps
  1. A proxy at f94e324199 with openai/gpt-5.4-nano, Postgres, and proxy_batch_polling_interval: 1
  2. Run the flow below with a key whose budget for the model is below one response's price
$ curl -s -X POST $PROXY/key/generate -H "Authorization: Bearer $MASTER_KEY" -d '{"model_max_budget": {"gpt-5.4-nano": {"max_budget": 0.0000001, "budget_duration": "1d"}}}'
key created
$ curl -s -X POST $PROXY/v1/responses -H "Authorization: Bearer $KEY" -d '{"model": "gpt-5.4-nano", "input": "Say hi in two words", "background": true}' | jq -c '{id, status, model, error: .error.message}'
{"id":"resp_...","status":"queued","model":"gpt-5.4-nano","error":null}
$ curl -s $PROXY/v1/responses/$ID -H "Authorization: Bearer $KEY" | jq -c '{status, text: ([.output[]? | select(.type=="message") | .content[]?.text] | join(" ")), total_tokens: .usage.total_tokens}'  # polled every 2s until completed
{"status":"completed","text":"Hi there!","total_tokens":18}
$ curl -s "$PROXY/key/info?key=$KEY" -H "Authorization: Bearer $MASTER_KEY" | jq .info.spend
0.00001095
$ for i in $(seq 1 12); do c=$(curl -s -o next.json -w '%{http_code}' -X POST $PROXY/v1/responses -H "Authorization: Bearer $KEY" -d '{"model": "gpt-5.4-nano", "input": "Say hi in two words", "background": true}'); printf '%s ' $c; [ $c != 200 ] && break; sleep 5; done; echo; jq -c '{status, error: .error.message}' next.json
200 422
{"status":null,"error":"LiteLLM Virtual Key: <hash>, key_alias: None, exceeded budget for model=gpt-5.4-nano"}

Type

✅ Test

Caveats (if any)

Medium

Low

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
Bugbot summary

[!NOTE]
Low Risk
Only integration test refactors and one new test; no runtime or billing logic changes.

Overview
Post-merge audit follow-up for background-response billing (#47504): tests only, no product changes.

Refactors the parametrized project/model budget test by pulling “next background create must be refused with exceeded budget for model=…” into _assert_the_next_create_exceeds_the_model_budget, so the same assertion is reused instead of duplicated inline.

Adds test_a_billed_background_response_on_a_team_model_counts_toward_the_budget_of_the_model_name_it_was_created_with, which registers a team-scoped upstream model, bills one completed background response against a key per-model budget set on that public model name, then asserts a second create is blocked once spend lands—covering the gap the audit found in existing matrix cells (R6).

Reviewed by Cursor Bugbot for commit f94e324. Bugbot is set up for automated code reviews on this repo. Configure here.

@mateo-berri

Copy link
Copy Markdown
Contributor

@litereview code-review

@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

@greptile-apps

greptile-apps Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Low impact] The test-only change appears safe to merge

Summary

Adds a test that checks a background response counts toward the key’s budget when a team-only model is called by its public name

  • The spend tests check a team model's public name against its budget.

Reviews (3) · Last reviewed commit: "test(responses): audit #47504" · Reviewed by Greptile

) -> None:
with gateway.scenario() as scenario:
team_id: Final = scenario.team()
upstream: Final = scripted_upstream(scenario, background_routes("completed"), model_info={"team_id": team_id})

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 Live-provider proof is missing

The claim “Every audited behavior holds at the merge commit and at this branch's tip, and each budget behavior fails at its parent” is backed only by integration tests with scripted responses. The repository requires live-provider proof for runtime claims, including test-only PRs. Before merging, show commands and outputs at this tip with a live proxy calling OpenAI through POST /v1/responses and GET /v1/responses/{id}, followed by the budget refusal

Rule Used: Apply this rule to every PR, including PRs that change only tests, docs, or catalog data. It does not require new live tests for test-only PRs; it only checks that each runtime claim the PR makes is backed by evidence at this PR's tip A runtime clai... (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

Choose a reason for hiding this comment

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

These cells run a real proxy and Postgres at this tip, only the upstream is scripted, and spend attribution never depends on which provider answered

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.

That’s a fair distinction: a real proxy and Postgres at this tip provide meaningful evidence for the accounting path, and the upstream’s response likely does not affect spend attribution. But the review rule explicitly excludes scripted upstreams as proof for runtime claims and asks for a live proxy call to a real provider. Since the PR description makes before/after runtime claims about budget enforcement, the live-provider finding still applies under that rule—even though these cells are useful evidence for the provider-independent accounting behavior.

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.

Added a live OpenAI gpt-5.4-nano run at the parent and this tip. The parent keeps answering 200, and the tip refuses with exceeded budget

@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

@codecov

codecov Bot commented Oct 28, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

@mateo-berri

Copy link
Copy Markdown
Contributor

bugbot run

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

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit f94e324. Configure here.

@mateo-berri mateo-berri 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.

LGTM

@mateo-berri
mateo-berri merged commit 91db681 into BerriAI:main Oct 28, 2026
94 of 96 checks passed
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.

2 participants