Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions coderd/aibridgedserver/aibridgedserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2454,6 +2454,10 @@ func TestRecordTokenUsageBudgetNotifications(t *testing.T) {
require.Equal(t, wantThreshold[tmpl], sent[0].Labels["threshold"])
require.Equal(t, "$100.00", sent[0].Labels["limit"])
require.Equal(t, "Engineering", sent[0].Labels["effective_group_name"])
// The interception is recorded at 2026-06-25, so its budget
// period runs June 1 - July 1, 2026.
require.Equal(t, "June 1, 2026", sent[0].Labels["period_start"])
require.Equal(t, "July 1, 2026", sent[0].Labels["period_end"])
}
})
}
Expand Down
10 changes: 10 additions & 0 deletions coderd/aibridgedserver/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ type budgetThresholdCrossing struct {
spendLimitMicros int64
thresholdPercent int
notificationTemplate uuid.UUID
// periodStart and periodEnd bound the budget period [start, end) the
// crossing occurred in.
periodStart time.Time
periodEnd time.Time
}

// detectBudgetThresholdCrossings checks whether this interception's cost pushed
Expand Down Expand Up @@ -94,6 +98,8 @@ func (s *Server) detectBudgetThresholdCrossings(ctx context.Context, tx database
spendLimitMicros: limit,
thresholdPercent: t.percent,
notificationTemplate: t.notificationTemplate,
periodStart: period.Start,
periodEnd: period.End,
})
}
}
Expand All @@ -113,6 +119,10 @@ func (s *Server) notifyBudgetThresholdCrossing(ctx context.Context, crossing bud
"limit": formatSpendLimit(crossing.spendLimitMicros),
"period": s.budgetPeriod.Adjective(),
"effective_group_name": group.Name,
// Both bounds carry the year so a period straddling a year boundary
// (e.g. December 1, 2026 - January 1, 2027) is unambiguous.
"period_start": crossing.periodStart.UTC().Format("January 2, 2006"),
"period_end": crossing.periodEnd.UTC().Format("January 2, 2006"),
}

//nolint:gocritic // Enqueuing notifications requires the notifier actor.
Expand Down
12 changes: 10 additions & 2 deletions coderd/database/migrations/000552_ai_budget_notifications.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ VALUES (
'b5db9597-de2a-4dea-87e9-25cee6906b86',
'AI Budget Warning',
E'You''re approaching your {{.Labels.period}} AI budget limit',
E'You have used more than {{.Labels.threshold}}% of your {{.Labels.period}} AI budget ({{.Labels.limit}}). Effective group: **{{.Labels.effective_group_name}}**.',
$$You have used more than {{.Labels.threshold}}% of your {{.Labels.period}} AI budget ({{.Labels.limit}}).

Effective group: **{{.Labels.effective_group_name}}**

AI budget period: {{.Labels.period_start}} - {{.Labels.period_end}}$$,
'[]'::jsonb,
'AI Cost Control Events',
NULL,
Expand All @@ -36,7 +40,11 @@ VALUES (
'cdcf2ecd-f003-4169-9800-abb2661ea522',
'AI Budget Limit Reached',
E'You''ve reached your {{.Labels.period}} AI budget limit',
E'You have reached your {{.Labels.period}} AI budget limit ({{.Labels.limit}}). Subsequent requests will be blocked. Effective group: **{{.Labels.effective_group_name}}**.',
$$You have reached your {{.Labels.period}} AI budget limit ({{.Labels.limit}}). Subsequent requests will be blocked.

Effective group: **{{.Labels.effective_group_name}}**

AI budget period: {{.Labels.period_start}} - {{.Labels.period_end}}$$,
'[]'::jsonb,
'AI Cost Control Events',
NULL,
Expand Down
4 changes: 4 additions & 0 deletions coderd/notifications/notifications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1473,6 +1473,8 @@ func TestNotificationTemplates_Golden(t *testing.T) {
"limit": "$1000.00",
"period": "monthly",
"effective_group_name": "Engineering",
"period_start": "July 1, 2026",
"period_end": "August 1, 2026",
},
Data: map[string]any{},
},
Expand All @@ -1489,6 +1491,8 @@ func TestNotificationTemplates_Golden(t *testing.T) {
"limit": "$1000.00",
"period": "monthly",
"effective_group_name": "Engineering",
"period_start": "July 1, 2026",
"period_end": "August 1, 2026",
},
Data: map[string]any{},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ Content-Type: text/plain; charset=UTF-8
Hi Bobby,

You have reached your monthly AI budget limit ($1000.00). Subsequent reques=
ts will be blocked. Effective group: Engineering.
ts will be blocked.

Effective group: Engineering

AI budget period: July 1, 2026 - August 1, 2026


--bbe61b741255b6098bb6b3c1f41b885773df633cb18d2a3002b68e4bc9c4
Expand Down Expand Up @@ -46,8 +50,11 @@ argin: 8px 0 32px; line-height: 1.5;">
<div style=3D"line-height: 1.5;">
<p>Hi Bobby,</p>
<p>You have reached your monthly AI budget limit ($1000.00). Subseq=
uent requests will be blocked. Effective group: <strong>Engineering</strong=
>.</p>
uent requests will be blocked.</p>

<p>Effective group: <strong>Engineering</strong></p>

<p>AI budget period: July 1, 2026 - August 1, 2026</p>
</div>
<div style=3D"text-align: center; margin-top: 32px;">
=20
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ Content-Type: text/plain; charset=UTF-8

Hi Bobby,

You have used more than 85% of your monthly AI budget ($1000.00). Effective=
group: Engineering.
You have used more than 85% of your monthly AI budget ($1000.00).

Effective group: Engineering

AI budget period: July 1, 2026 - August 1, 2026


--bbe61b741255b6098bb6b3c1f41b885773df633cb18d2a3002b68e4bc9c4
Expand Down Expand Up @@ -46,7 +49,11 @@ argin: 8px 0 32px; line-height: 1.5;">
<div style=3D"line-height: 1.5;">
<p>Hi Bobby,</p>
<p>You have used more than 85% of your monthly AI budget ($1000.00)=
. Effective group: <strong>Engineering</strong>.</p>
.</p>

<p>Effective group: <strong>Engineering</strong></p>

<p>AI budget period: July 1, 2026 - August 1, 2026</p>
</div>
<div style=3D"text-align: center; margin-top: 32px;">
=20
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
"effective_group_name": "Engineering",
"limit": "$1000.00",
"period": "monthly",
"period_end": "August 1, 2026",
"period_start": "July 1, 2026",
"threshold": "100"
},
"data": {},
"targets": null
},
"title": "You've reached your monthly AI budget limit",
"title_markdown": "You've reached your monthly AI budget limit",
"body": "You have reached your monthly AI budget limit ($1000.00). Subsequent requests will be blocked. Effective group: Engineering.",
"body_markdown": "You have reached your monthly AI budget limit ($1000.00). Subsequent requests will be blocked. Effective group: **Engineering**."
"body": "You have reached your monthly AI budget limit ($1000.00). Subsequent requests will be blocked.\n\nEffective group: Engineering\n\nAI budget period: July 1, 2026 - August 1, 2026",
"body_markdown": "You have reached your monthly AI budget limit ($1000.00). Subsequent requests will be blocked.\n\nEffective group: **Engineering**\n\nAI budget period: July 1, 2026 - August 1, 2026"
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
"effective_group_name": "Engineering",
"limit": "$1000.00",
"period": "monthly",
"period_end": "August 1, 2026",
"period_start": "July 1, 2026",
"threshold": "85"
},
"data": {},
"targets": null
},
"title": "You're approaching your monthly AI budget limit",
"title_markdown": "You're approaching your monthly AI budget limit",
"body": "You have used more than 85% of your monthly AI budget ($1000.00). Effective group: Engineering.",
"body_markdown": "You have used more than 85% of your monthly AI budget ($1000.00). Effective group: **Engineering**."
"body": "You have used more than 85% of your monthly AI budget ($1000.00).\n\nEffective group: Engineering\n\nAI budget period: July 1, 2026 - August 1, 2026",
"body_markdown": "You have used more than 85% of your monthly AI budget ($1000.00).\n\nEffective group: **Engineering**\n\nAI budget period: July 1, 2026 - August 1, 2026"
}
Loading