feat: notify users when AI spend crosses the budget threshold (#27346)

Implements:
https://linear.app/codercom/issue/AIGOV-289/notify-users-and-admins-on-budget-warning-and-limit-reached

Notify users when their AI spend crosses a budget threshold for their
effective group. Two thresholds are covered: a warning at 85%, and a
limit-reached notification at 100%.

Detection runs on the post-response path, right after the interception's
cost is added to the user's daily spend. It reads the user's AI spend on
the same transaction where token usage is recorded and AI daily spend is
incremented, and derives the pre-interception total by subtracting this
interception's cost. In case of `oldSpend < threshold && newSpend >=
threshold` - notification is sent. A single interception that crosses
both thresholds enqueues both notifications.

Detection and delivery are best-effort: a failure is logged and never
fails usage recording. The payload uses only stable values (the
threshold percentage and the spend limit, not the exact spend), so
duplicate enqueues are deduplicated by the notification system.

The two templates are added via migration and appear in each user's
notification settings under the "AI Budget" group.

Admin notifications (owners and user admins) are a follow-up: #27415.

## Screenshots:
<img width="1102" height="252" alt="image"
src="https://github.com/user-attachments/assets/62291510-09ca-4cdf-a1f5-4bdc11a1db4b"
/>

<img width="466" height="384" alt="image"
src="https://github.com/user-attachments/assets/030460ff-6fe2-4d59-b247-3550c543ef30"
/>

---------

Co-authored-by: Cian Johnston <cian@coder.com>
This commit is contained in:
Yevhenii Shcherbina
2026-07-27 12:09:21 -04:00
committed by GitHub
co-authored by Cian Johnston
parent c9e68987c1
commit ce4ee923c2
18 changed files with 940 additions and 3 deletions
+16
View File
@@ -612,6 +612,22 @@ var AIBudgetPeriods = []string{
string(AIBudgetPeriodMonth),
}
// Adjective renders the period as the adjective used in user-facing text (e.g. "monthly").
func (p AIBudgetPeriod) Adjective() string {
switch p {
case "day":
return "daily"
case "week":
return "weekly"
case AIBudgetPeriodMonth:
return "monthly"
case "year":
return "yearly"
default:
return string(p)
}
}
// NewAIBudgetPeriodFromString converts s to an AIBudgetPeriod, falling back to
// AIBudgetPeriodMonth when s is empty or not a recognized period.
func NewAIBudgetPeriodFromString(s string) AIBudgetPeriod {
+13
View File
@@ -149,6 +149,19 @@ func TestDeploymentValues_HighlyConfigurable(t *testing.T) {
}
}
func TestAIBudgetPeriodAdjective(t *testing.T) {
t.Parallel()
// Every selectable period must have a real adjective.
for _, p := range codersdk.AIBudgetPeriods {
period := codersdk.AIBudgetPeriod(p)
require.NotEqual(t, p, period.Adjective(),
"add an adjective for AI budget period %q in AIBudgetPeriod.Adjective", p)
}
require.Equal(t, "monthly", codersdk.AIBudgetPeriodMonth.Adjective())
}
func TestParseSSHConfigOption(t *testing.T) {
t.Parallel()