← Back to PRs

#13885: feat: add session cost monitoring and alerting system

by trevorgordon981 open 2026-02-11 04:48 View on GitHub →
stale
## Summary Adds a session cost monitoring system that checks cumulative session and daily costs against configurable thresholds, enabling operators to get alerted before costs spiral unexpectedly. ## Changes ### New files - `src/infra/session-cost-alerts.ts` - Core alerting logic with session and daily threshold checks - `src/infra/session-cost-alerts.test.ts` - Comprehensive test suite (20+ test cases) ### Features - **Configurable thresholds** via `costAlerts.sessionThreshold` and `costAlerts.dailyThreshold` (USD) - **Two-tier alerting**: warning (80-100% of threshold) and critical (>=100%) - **Structured results** with level, message, cost details for downstream consumption - **Formatted output** with emoji indicators suitable for Slack/Discord delivery - **Graceful defaults** - no alerts when thresholds are unconfigured ### API - `checkSessionCostAlert(cost, thresholds)` - Check single session cost - `checkDailyCostAlert(cost, thresholds)` - Check aggregate daily cost - `getApplicableCostAlerts(sessionCost, dailyCost, config)` - Get all triggered alerts from config - `formatCostAlerts(alerts)` - Format for messaging (Slack-compatible markdown) ## Testing Full test coverage including: - Below-threshold (no alert) - Warning range (80-100%) - Critical range (>=100%) - Missing/undefined thresholds - Multiple simultaneous alerts - Formatting with emoji sections ## Motivation Long-running or expensive sessions can accumulate significant API costs without the operator noticing. This provides a lightweight, opt-in guard rail that integrates with existing config and messaging infrastructure.

Most Similar PRs