litellm/litellm/integrations/SlackAlerting
rioiart 1ac2655b17
Fix/organization max budget not enforced (#17334)
* test: add failing tests for organization budget enforcement bug

Add comprehensive tests exposing that organization-level budgets are
retrieved but never enforced during request authentication. Tests verify:

1. Basic org budget exceeded scenario (team under budget, org over)
2. Multiple teams collectively exceeding org budget
3. Organization budget fields exist but are never checked
4. Inconsistency between team budget enforcement (works) and org (doesn't)

Tests intentionally fail to document the bug. Will be fixed in next commit.

Related to organization_max_budget not being enforced in auth_checks.py

* fix: enforce organization budget in auth checks

Add organization budget enforcement to common_checks() in auth_checks.py.
Previously, organization_max_budget was retrieved from DB but never checked,
allowing teams to collectively exceed their organization's budget limit.

Changes:
- Add _organization_max_budget_check() function following team budget pattern
- Call org budget check after team budget check in common_checks()
- Add "organization_budget" to budget_alerts type literals
- Update tests to verify org budget is enforced

Budget hierarchy is now properly enforced:
  Organization Budget (hard ceiling)
    └─ Team Budget (sub-allocation)
        └─ Team Member Budget (per-user within team)
            └─ Key Budget (per-key)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* fix: add organization_id to budget alerts, fix enum comparison and linting of newly added code

- Add organization_id field to CallInfo class for better alert context
- Include organization_id in budget alerts (token, soft, team, org)
- Fix event_group enum comparison (was comparing enum to string)
- Add OrganizationBudgetAlert class for organization budget alerting
- Add organization_budget to test parameterizations
- Apply Black formatting to slack_alerting.py

---------

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-02 22:46:03 -08:00
..
batching_handler.py
budget_alert_types.py Fix/organization max budget not enforced (#17334) 2025-12-02 22:46:03 -08:00
hanging_request_check.py [Feat] Performance - Don't create 1 task for every hanging request alert (#11385) 2025-06-03 21:12:54 -07:00
Readme.md [Fix + Refactor] Trigger Soft Budget Webhooks When Key Crosses Threshold (#10491) 2025-05-02 07:06:07 -07:00
slack_alerting.py Fix/organization max budget not enforced (#17334) 2025-12-02 22:46:03 -08:00
utils.py [Fix] Reliability Fix - Removing code that was creating threads on errors (#11066) 2025-05-22 18:04:15 -07:00

Slack Alerting on LiteLLM Gateway

This folder contains the Slack Alerting integration for LiteLLM Gateway.

Folder Structure

  • slack_alerting.py: This is the main file that handles sending different types of alerts
  • batching_handler.py: Handles Batching + sending Httpx Post requests to slack. Slack alerts are sent every 10s or when events are greater than X events. Done to ensure litellm has good performance under high traffic
  • types.py: This file contains the AlertType enum which is used to define the different types of alerts that can be sent to Slack.
  • utils.py: This file contains common utils used specifically for slack alerting

Budget Alert Types

The budget_alert_types.py module provides a flexible framework for handling different types of budget alerts:

  • BaseBudgetAlertType: An abstract base class with abstract methods that all alert types must implement:
    • get_event_group(): Returns the Litellm_EntityType for the alert
    • get_event_message(): Returns the message prefix for the alert
    • get_id(user_info): Returns the ID to use for caching/tracking the alert

Concrete implementations include:

  • ProxyBudgetAlert: Alerting for proxy-level budget concerns
  • SoftBudgetAlert: Alerting when soft budgets are crossed
  • UserBudgetAlert: Alerting for user-level budget concerns
  • TeamBudgetAlert: Alerting for team-level budget concerns
  • TokenBudgetAlert: Alerting for API key budget concerns
  • ProjectedLimitExceededAlert: Alerting when projected spend will exceed budget

Use the get_budget_alert_type() factory function to get the appropriate alert type class for a given alert type string:

from litellm.integrations.SlackAlerting.budget_alert_types import get_budget_alert_type

# Get the appropriate handler
budget_alert_class = get_budget_alert_type("user_budget")

# Use the handler methods
event_group = budget_alert_class.get_event_group()  # Returns Litellm_EntityType.USER
event_message = budget_alert_class.get_event_message()  # Returns "User Budget: "
cache_id = budget_alert_class.get_id(user_info)  # Returns user_id

To add a new budget alert type, simply create a new class that extends BaseBudgetAlertType and implements all the required methods, then add it to the dictionary in the get_budget_alert_type() function.

Further Reading