Skip to content

fix(alerting): send one budget alert when concurrent checks hit the same budget event - #45581

Open
Jayanth-reflex wants to merge 1 commit into
BerriAI:mainfrom
Jayanth-reflex:bugfix/slack-budget-alert-dedup
Open

Jayanth-reflex wants to merge 1 commit into
BerriAI:mainfrom
Jayanth-reflex:bugfix/slack-budget-alert-dedup

Conversation

@Jayanth-reflex

@Jayanth-reflex Jayanth-reflex commented Oct 9, 2026 •

Copy link
Copy Markdown

Problem this solves:

How it solves it:

  • Claim the cooldown key with an atomic increment before sending
  • Only the request that gets count 1 sends the alert
  • A failed send releases the claim so the next check retries
  • The existing cooldown read stays in front, so steady traffic still costs one read

User Flow

Before: a proxy admin with budget alerts on a webhook gets the same alert many times when traffic is concurrent

  1. The admin enables alerting: ["webhook"] and creates a key with soft_budget: 5 that has already spent $10
  2. An app sends 10 concurrent POST http://localhost:4000/v1/chat/completions requests with that key, all return 200
  3. The webhook receives 20 alerts: 10 identical soft_budget_crossed and 10 identical budget_crossed for the same key
  4. One more request a few seconds later sends nothing new, so only concurrent traffic duplicates

After: the same traffic produces one alert per budget event

  1. The admin enables alerting: ["webhook"] and creates a key with soft_budget: 5 that has already spent $10
  2. An app sends 10 concurrent POST http://localhost:4000/v1/chat/completions requests with that key, all return 200
  3. The webhook receives 2 alerts: one soft_budget_crossed and one budget_crossed
  4. One more request a few seconds later still sends nothing new

Relevant issues

Fixes #44919

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
  • I have received a Greptile Confidence Score of at least 4/5 before requesting a maintainer review (Greptile reviews automatically once the PR is opened; only comment @greptileai to re-request a review after pushing changes)

Screenshots / Proof of Fix

Setup shared by both runs: a local Postgres, a fresh database per run, and a webhook receiver on 127.0.0.1:8099 that writes one line per alert (event, token, spend) to webhook.log and answers 200 after 200 ms, roughly what a hosted chat webhook takes. The model uses mock_response because both alerts fire from the auth and spend-tracking steps, not from the provider response

model_list:
  - model_name: gpt-4o-mini
    litellm_params:
      model: openai/gpt-4o-mini
      api_key: sk-unused
      mock_response: "hello"

general_settings:
  master_key: os.environ/LITELLM_MASTER_KEY
  alerting: ["webhook"]
WEBHOOK_URL=http://127.0.0.1:8099/budget DATABASE_URL=postgresql://postgres@127.0.0.1:5433/litellm \
  python litellm/proxy/proxy_cli.py --config config.yaml --port 4000

KEY=$(curl -s http://localhost:4000/key/generate -H "Authorization: Bearer $LITELLM_MASTER_KEY" \
  -H "Content-Type: application/json" -d '{"key_alias": "over-soft-budget", "spend": 10, "soft_budget": 5}' | jq -r .key)

chat() {
  curl -s -o /dev/null -w "%{http_code} " http://localhost:4000/v1/chat/completions \
    -H "Authorization: Bearer $KEY" -H "Content-Type: application/json" \
    -d '{"model": "gpt-4o-mini", "messages": [{"role": "user", "content": "hi"}]}'
}

Before (a3236ba)

Concurrent requests on a key over its soft budget

  1. Send 10 concurrent requests: for i in $(seq 10); do chat & done; wait; echo

    200 200 200 200 200 200 200 200 200 200
    
  2. Count the alerts: wc -l < webhook.log; sort webhook.log | uniq -c

    20
         10 budget_crossed token=0d66371f2e0ffbdf03880f5868ede2b993094b6409fc8896f65a433020fa9d99 spend=10.0000135
         10 soft_budget_crossed token=0d66371f2e0ffbdf03880f5868ede2b993094b6409fc8896f65a433020fa9d99 spend=10.0
    
  3. Send one more request, then count again: chat; echo; wc -l < webhook.log

    200
    20
    

Webhook down during the concurrent requests, then back up

  1. With the receiver stopped, send 10 concurrent requests: for i in $(seq 10); do chat & done; wait; echo

    200 200 200 200 200 200 200 200 200 200
    
  2. Count the alerts: wc -l < webhook.log

    0
    
  3. Start the receiver, send one request, then count: chat; echo; wc -l < webhook.log; sort webhook.log | uniq -c

    200
    2
          1 budget_crossed token=b2af08b23c086f5f3bcc0e61ca240b3d329b7f8c53fe7a96e3ac3707efae1723 spend=10.0000135
          1 soft_budget_crossed token=b2af08b23c086f5f3bcc0e61ca240b3d329b7f8c53fe7a96e3ac3707efae1723 spend=10.0
    
  4. Send one more request, then count again: chat; echo; wc -l < webhook.log

    200
    2
    

After (a564b60)

Concurrent requests on a key over its soft budget

  1. Send 10 concurrent requests: for i in $(seq 10); do chat & done; wait; echo

    200 200 200 200 200 200 200 200 200 200
    
  2. Count the alerts: wc -l < webhook.log; sort webhook.log | uniq -c

    2
          1 budget_crossed token=53558162a1e0538d9d769233990e9cb0ea449f15dcfc2d116bce491ebf7022a6 spend=10.0000135
          1 soft_budget_crossed token=53558162a1e0538d9d769233990e9cb0ea449f15dcfc2d116bce491ebf7022a6 spend=10.0
    
  3. Send one more request, then count again: chat; echo; wc -l < webhook.log

    200
    2
    

Webhook down during the concurrent requests, then back up

  1. With the receiver stopped, send 10 concurrent requests: for i in $(seq 10); do chat & done; wait; echo

    200 200 200 200 200 200 200 200 200 200
    
  2. Count the alerts: wc -l < webhook.log

    0
    
  3. Start the receiver, send one request, then count: chat; echo; wc -l < webhook.log; sort webhook.log | uniq -c

    200
    2
          1 budget_crossed token=16b3a0e410688c9f7500f375a33c32bddcc837f15b8f98d68bdc4567d3a9a6b0 spend=10.0000135
          1 soft_budget_crossed token=16b3a0e410688c9f7500f375a33c32bddcc837f15b8f98d68bdc4567d3a9a6b0 spend=10.0
    
  4. Send one more request, then count again: chat; echo; wc -l < webhook.log

    200
    2
    

Type

🐛 Bug Fix

Caveats (if any)

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

@greptile-apps

greptile-apps Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 4/5

[Medium impact] Adds deduplication logic to budget alert sending.

The repository's live-QA and dependency-injection requirements must be satisfied before merging

Findings

  1. P2 Required live QA is missing ▶
  2. P2 Test replaces our own method ▶

Summary

budget_alerts now increments its cooldown key before sending. Only the first claim sends, and a raised send error deletes the claim

  • Concurrent budget checks send one alert for each event.

Acknowledged limits from Jayanth-reflex: Redis failures leave each worker deduping locally; cancelled sends keep their claim until expiry; failed sends leave other workers' local claims intact. The description states that these match the email path

Reviews (1) · Last reviewed commit: "fix(alerting): send one budget alert whe..." · Reviewed by Greptile

@codspeed

codspeed Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing Jayanth-reflex:bugfix/slack-budget-alert-dedup (a564b60) with main (bbe5595)

Open in CodSpeed

@codecov

codecov Bot commented Oct 9, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@Jayanth-reflex

Copy link
Copy Markdown
Author

@mateo-berri @yucheng-berri could please you review?

Same claim-before-send fix as #32011
Applied to the Slack/webhook budget alerts
CI is green
Greptile 4/5

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]: Concurrent budget checks deliver duplicate webhook alerts for the same entity and budget event

1 participant