Skip to content

feat: add per-chat tree-level spend limits#24151

Draft
ibetitsmike wants to merge 5 commits intomainfrom
mike/chat-spend-limit
Draft

feat: add per-chat tree-level spend limits#24151
ibetitsmike wants to merge 5 commits intomainfrom
mike/chat-spend-limit

Conversation

@ibetitsmike
Copy link
Copy Markdown
Collaborator

Adds an optional per-chat spend limit that governs the total cost of a
chat tree (root chat plus all subagent descendants). When the cumulative
total_cost_micros across the tree reaches the limit, further messages
are rejected with a 409 containing scope: "chat_lifetime".

Database: migration 000463 adds nullable spend_limit_micros column
with a CHECK (> 0) constraint. GetChatTreeSpendTotal sums priced
messages across the root and all descendants.

Enforcement: checkChatTreeSpendLimit resolves the root chat,
fetches its limit, and compares against tree spend. Runs before the
existing account-period check in SendMessage, EditMessage, and
PromoteQueued. Fails open on lookup errors to avoid blocking users
during transient DB issues.

API: CreateChatRequest accepts optional spend_limit_micros
(validated > 0). ChatUsageLimitExceededResponse gains a scope field
(account_period or chat_lifetime) and resets_at is now optional
(nil for lifetime limits).

This PR was authored by Mux on behalf of Mike.

@ibetitsmike ibetitsmike changed the title feat(coderd): add per-chat tree-level spend limits feat: add per-chat tree-level spend limits Apr 8, 2026
@ibetitsmike ibetitsmike force-pushed the mike/chat-spend-limit branch 2 times, most recently from 2f43ec0 to 52de662 Compare April 8, 2026 16:33
@github-actions github-actions bot added the stale This issue is like stale bread. label Apr 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stale This issue is like stale bread.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant