Usage alerts#1
Open
MatthiasDurrieu wants to merge 2 commits into
Open
Conversation
Users can now subscribe to per-workspace budget alerts via DM commands (alerts on/off/status). The bot checks costs hourly via APScheduler and sends a DM at 25%, 50%, 75%, 100% of each user's monthly share, then every 25% beyond that. Subscriptions are persisted in a JSON file mounted as a Docker volume so they survive restarts. Adds MONTHLY_PLAN_LIMIT and PLAN_MEMBER_COUNT env vars (defaulting to $100/9 members). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I added usage alerts options for the slackbot. I assumed everyone uses our lab plan, which means 9 workspaces so 11.11 $ per person. Based on that, if the usage alert is activated it's gonna DM the user whenever 25% of this has been used on the workspace. Let me know if that sounds useful and if that's a good way to implement it!
Here is in depth automatically generated summary of the changes:
Adds three new DM commands:
alerts on <workspace>,alerts off,alerts statusA new hourly APScheduler job checks each subscriber's current-month cost against their personal share of the plan budget and DMs them at 25 / 50 / 75 / 100 % — and every 25 % beyond that for over-budget situations
Subscriptions are persisted in a JSON file (
./data/alerts.jsonby default, configurable viaALERTS_FILE). A./data:/app/datavolume is added todocker-compose.ymlso the file survives container restartsTwo new env vars —
MONTHLY_PLAN_LIMIT(default100) andPLAN_MEMBER_COUNT(default9) — control the per-user share; no changes needed for the current setupHow it works (design notes)
New module —
claude-usage-bot/alerts.pyAlertManageris a thread-safe wrapper around a JSON file. It stores, per Slack user ID: which workspace they watch, whether alerts are enabled, and which percentage thresholds have already triggered a DM this calendar month. Using a month key ("2026-05") means the threshold tracking resets automatically each month without any cron cleanup.Scheduled job —
_check_usage_alerts()Runs every hour via APScheduler. For each active subscriber it calls
query_cost_current_month()+filter_data_for_workspace()(both already existed), sums the cost in cents, converts to USD, and walks 25 % increments up to the current spend percentage. Any threshold not yet recorded gets a DM and is then written to the JSON file so it isn't sent again.Workspace validation on subscribe
alerts on <workspace>callsworkspace_name_to_id()immediately, so the user gets a clear error message if they mistype a workspace name rather than silently never receiving alerts.Test plan
DM the bot
alerts on <your-workspace>— should reply with confirmation and your monthly share amountDM
alerts status— should show workspace name and "none yet this month"DM
alerts off— should confirm;alerts statusshould then show no subscriptionDM
alerts on <invalid-workspace>— should reply with a workspace-not-found errorVerify
./data/alerts.jsonis created and updated on subscribe/unsubscribeAdd
MONTHLY_PLAN_LIMITandPLAN_MEMBER_COUNTto your.env(or leave them out to use the defaults) and restart the botConfirm
docker-compose upcreates the./datamount and the file persists acrossdocker-compose restart