feat: notify admins when a user crosses an AI budget threshold (#27415)

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

Notify admins when a user crosses an AI budget threshold, complementing
the user-facing notifications from
https://github.com/coder/coder/pull/27346

When a priced interception pushes a user's period spend across the
warning (85%) or limit (100%) threshold, the Owners and User Admins now
receive an admin notification naming the affected user, alongside the
user's own notification. The affected user is excluded from the admin
recipients since they already get the user-facing copy. Delivery is
best-effort: a failure to enqueue is logged and never blocks recording
the interception.

The admin templates always show the effective group the spend is
attributed to, and note when the limit comes from a per-user override
rather than the group budget.

Depends on https://github.com/coder/coder/pull/27346

## Screenshots:
<img width="1101" height="440" alt="image"
src="https://github.com/user-attachments/assets/eb731088-05c8-47bd-9d06-fc9d07f63a08"
/>

<img width="468" height="391" alt="image"
src="https://github.com/user-attachments/assets/b89b76a6-3fa8-4735-99a2-43e119a7a7e3"
/>
This commit is contained in:
Yevhenii Shcherbina
2026-07-27 17:02:28 -04:00
committed by GitHub
parent 51ac968d5a
commit 2574e6b785
13 changed files with 602 additions and 54 deletions
@@ -0,0 +1,5 @@
DELETE FROM notification_templates
WHERE id IN (
'2a7b0ac1-00e1-4625-9cd5-1e5933972c77',
'0bafe0ea-a78b-4217-ad05-1ef12e92e025'
);
@@ -0,0 +1,61 @@
INSERT INTO notification_templates (
id,
name,
title_template,
body_template,
actions,
"group",
method,
kind,
enabled_by_default
)
VALUES (
'2a7b0ac1-00e1-4625-9cd5-1e5933972c77',
'User Approaching AI Budget Limit',
E'{{.Labels.username}} is approaching their {{.Labels.period}} AI budget limit',
$$User **{{.Labels.username}}** has used more than {{.Labels.threshold}}% of their {{.Labels.period}} AI budget ({{.Labels.limit}}).
Effective group: **{{.Labels.effective_group_name}}**
{{- if eq .Labels.limit_source "user_override"}}
This limit is a per-user override.
{{- end}}
AI budget period: {{.Labels.period_start}} - {{.Labels.period_end}}$$,
'[]'::jsonb,
'AI Cost Control Admin Events',
NULL,
'system'::notification_template_kind,
true
);
INSERT INTO notification_templates (
id,
name,
title_template,
body_template,
actions,
"group",
method,
kind,
enabled_by_default
)
VALUES (
'0bafe0ea-a78b-4217-ad05-1ef12e92e025',
'User Reached AI Budget Limit',
E'{{.Labels.username}} has reached their {{.Labels.period}} AI budget limit',
$$User **{{.Labels.username}}** has reached their {{.Labels.period}} AI budget limit ({{.Labels.limit}}). Subsequent requests will be blocked.
Effective group: **{{.Labels.effective_group_name}}**
{{- if eq .Labels.limit_source "user_override"}}
This limit is a per-user override.
{{- end}}
AI budget period: {{.Labels.period_start}} - {{.Labels.period_end}}$$,
'[]'::jsonb,
'AI Cost Control Admin Events',
NULL,
'system'::notification_template_kind,
true
);