agents: enforce aggregate AI usage limits - #511
Open
demattosanthony wants to merge 1 commit into
Open
Conversation
This was referenced Sep 1, 2026
demattosanthony
marked this pull request as ready for review
September 1, 2026 17:46
demattosanthony
force-pushed
the
ai-limits/03-enforcement
branch
from
September 1, 2026 17:56
6cc4b45 to
7da5c80
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Enforce aggregate AI usage limits across direct Agent conversations and workflow Agent nodes.
This PR adds a fast request-time preflight and an authoritative atomic reservation immediately
before every provider call. Completed calls record usage, valuation, and reservation reconciliation
in one storage transaction. Calls that may have been billed without trustworthy usage retain their
reservation as unknown capacity instead of failing open.
Enforcement model
The request preflight rejects obviously exhausted or unsafe runs before creating a new conversation
thread or message. It is an early user-experience optimization, not the concurrency authority.
The worker boundary remains authoritative:
The same recorder and admission controller are used by conversational turns and structured workflow
Agent nodes.
What changes
ai.usage_limit_exceededandai.usage_limit_unavailableas stable coded failures.the catalog change.
generic execution failures.
Retry-Afterfrom theearliest applicable reset.
Failure and safety behavior
estimated safely.
budgets or guarantee that one response cannot exceed its conservative reservation.
Reviewer guide
Suggested review order:
model-call-admission.tsandmodel-call-limits.tsfor estimation and policy selection.models-dev/pricing.tsfor conservative pre-call cost bounds.model-call-recorder.ts, accounting, and recovery for the reservation lifecycle.Generated client files should be reviewed as derived output; the source contracts are the Agent
failure unions and route response schemas.
Validation performed
bun run generate:client— passed; derived failure contracts were regenerated from source.bun run typecheck— passed on this branch.bun run check— passed on this branch.publish-boundary validation, and the guarded unit suite.
Not in this PR
reconciliation.
Stack
This is PR 3 of 5 in GitHub stack #514. It depends on
#510. Next:
#512, which exposes the protected operator API after
enforcement is authoritative.