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

Skip to content

fix: ai cost control cap configurable AI spend limit - #27640

Merged
ssncferreira merged 2 commits into
mainfrom
ssncf/cap-ai-spend-limit
Jul 29, 2026
Merged

fix: ai cost control cap configurable AI spend limit#27640
ssncferreira merged 2 commits into
mainfrom
ssncf/cap-ai-spend-limit

Conversation

@ssncferreira

@ssncferreira ssncferreira commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Problem

A configured AI spend limit was only validated as gte=0, with no upper bound. The group spend query multiplies the per-member limit by the number of attributed members, so a large enough limit overflows bigint and fails the whole query, returning an error for every group in the request rather than just the misconfigured one.

Changes

  • Add MaxAISpendLimitMicros, $1,000,000 per member per budget period.
  • Reject group budgets and per-user overrides above the maximum with a 400 naming the limit.
  • Bound both budget forms in the UI so they show the valid range before submitting.

Follow-up #27589 (comment)
Depends on #27589

Note

Initially generated by Claude Opus 5, modified and reviewed by @ssncferreira

Copy link
Copy Markdown
Contributor Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@github-actions

github-actions Bot commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Docs preview

Check off each page once it's been reviewed. If a page changes in a later push, its checkbox clears automatically so it gets a fresh look. Pages not yet wired into the docs navigation aren't listed here.

@ssncferreira
ssncferreira force-pushed the ssncf/cap-ai-spend-limit branch 3 times, most recently from 9187241 to 515158f Compare July 29, 2026 09:29
@ssncferreira ssncferreira changed the title fix: ai cost control cap configurable AI spend limi fix: ai cost control cap configurable AI spend limit Jul 29, 2026
@ssncferreira
ssncferreira force-pushed the ssncf/cap-ai-spend-limit branch from 515158f to dd0c624 Compare July 29, 2026 10:26
@ssncferreira
ssncferreira marked this pull request as ready for review July 29, 2026 10:39
@ssncferreira
ssncferreira requested a review from a team as a code owner July 29, 2026 10:39
@ssncferreira
ssncferreira requested review from EhabY and johnstcn July 29, 2026 10:40
return (
<span className="text-left text-xs text-content-secondary">
<span className="font-medium text-content-primary">
{usdBudgetFormatter.format(budgetAmount * memberCount)}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should also use the total budget here no? Though I guess it depends on what we want to show 🤔

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought of that as well, but decided against it. The total returned by /groups/{group}/ai/spend is computed from the saved budget on the database, so it can't preview the value being typed. We'd need the group's member count and override sum instead, which the endpoint doesn't expose, and that query isn't free.
Nevertheless, I removed "maximum" from the message instead, since that was the misleading part.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes that would clear things up a bit

Comment thread codersdk/aibridge.go

type UpsertGroupAIBudgetRequest struct {
// SpendLimitMicros must not exceed MaxAISpendLimitMicros.
SpendLimitMicros int64 `json:"spend_limit_micros" validate:"gte=0"`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not enforce this? (lte)

Unless we want to configure this later on?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is enforced, just in the handler rather than the tag. A lte tag can't reference the constant, so the number would be duplicated (maintenance drift), and the generated message doesn't name the maximum. It would show something like

Validation failed for tag "lte" with value: "2000000000000"

While the handler returns Must not exceed 1000000000000. instead.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah fair enough

Comment thread codersdk/aibridge.go
Comment thread site/src/pages/GroupsPage/UserAIBudgetOverrideDialog.tsx Outdated
@ssncferreira
ssncferreira force-pushed the ssncf/cap-ai-spend-limit branch from dd0c624 to b2cd558 Compare July 29, 2026 12:33
@ssncferreira
ssncferreira requested a review from EhabY July 29, 2026 12:34
Comment thread codersdk/aibridge.go

type UpsertGroupAIBudgetRequest struct {
// SpendLimitMicros must not exceed MaxAISpendLimitMicros.
SpendLimitMicros int64 `json:"spend_limit_micros" validate:"gte=0"`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah fair enough

return (
<span className="text-left text-xs text-content-secondary">
<span className="font-medium text-content-primary">
{usdBudgetFormatter.format(budgetAmount * memberCount)}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes that would clear things up a bit

@ssncferreira
ssncferreira force-pushed the ssncf/cap-ai-spend-limit branch from b2cd558 to d3f2ea3 Compare July 29, 2026 12:48
@ssncferreira
ssncferreira merged commit e71249a into main Jul 29, 2026
32 of 33 checks passed
@ssncferreira
ssncferreira deleted the ssncf/cap-ai-spend-limit branch July 29, 2026 13:00
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 29, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants