Skip to content

feat(chat): Enforce a hard conversation spend cap - #1068

Open
sentry-junior[bot] wants to merge 5 commits into
mainfrom
feat/turn-spend-cap
Open

feat(chat): Enforce a hard conversation spend cap#1068
sentry-junior[bot] wants to merge 5 commits into
mainfrom
feat/turn-spend-cap

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Add a configurable MAX_SPEND USD cap that terminates conversation work once provider-reported model cost reaches the threshold. Over-budget turns end with a deterministic user-visible response instead of model output or a generic failure.

Core loop enforcement

The agent checks cumulative conversation and in-flight turn usage before the first model call, before tool execution, before each subsequent model turn, and before final assistant delivery. Reaching the cap stops further agent work and delivers the static spend-limit response.

Fail-closed cost reporting

When a cap is configured and a provider reports usage without cost data, the turn stops with the same static response rather than silently treating the call as free. Integration coverage exercises both the threshold and missing-cost paths through the real agent loop.

Configuration

MAX_SPEND accepts a positive USD amount. It is disabled when unset and fails startup validation for zero, negative, or non-numeric values.

Requested by David Cramer via Junior.

--

View Junior Session

Co-Authored-By: David Cramer <david@sentry.io>
@vercel

vercel Bot commented Jul 27, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
junior-docs Ready Ready Preview Jul 28, 2026 4:48pm

Request Review

Comment thread packages/junior/src/chat/agent/index.ts Outdated
Co-Authored-By: David Cramer <david@sentry.io>
Co-Authored-By: David Cramer <david@sentry.io>
@dcramer
dcramer marked this pull request as ready for review July 27, 2026 18:24
@github-actions github-actions Bot added the risk: medium PR risk score: medium label Jul 27, 2026
Comment thread packages/junior/src/chat/agent/index.ts
Comment thread packages/junior/src/chat/agent/index.ts
Validate each new provider usage record before combining it with prior spend. Preserve the capped turn's messages and usage so completion persistence blocks later billable calls.
Comment thread packages/junior/src/chat/services/spend-limit.ts
Derive a total for component-only usage before aggregating slices. This keeps cumulative spend accurate when providers report different cost shapes.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 0b3f74a. Configure here.

Comment thread packages/junior/src/chat/agent/index.ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: medium PR risk score: medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant