Skip to content

feat(chat): Limit turns by spend and wrap up at the limit - #2152

Merged
dcramer merged 10 commits into
mainfrom
feat/turn-spend-budget
Oct 15, 2026
Merged

dcramer merged 10 commits into
mainfrom
feat/turn-spend-budget

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Child agents doing real work keep hitting the 250 tool-call cap from #1752. The turn fails, the parent sees "model failed", and the child's work is lost. The cap was meant to stop runaway loops, and what those loops really cost was model spend.

  • Spend limit: each turn can spend maxTurnCostUsd of model cost, $100 by default, across all its execution slices. Retries that Junior throws away count too. The tool-call cap goes from 250 to 1,000 and only matters for models without cost data.
  • Wrap up instead of failing: the first model step past a limit has its tool calls blocked with the same message as a tool that disableTools (fix(chat): Keep the tool list stable across Turn origins #2148) disabled. The tool list stays the same, so the provider cache stays valid. The model replies with what it finished, what is left, and how to continue, and a child agent sends that reply to its parent as a normal result. Any later tool call in the turn fails it. The turn checkpoint saves the wrap-up, so a later slice does not wrap up again.
  • Child limit failures are final: a non-final attempt used to rethrow the limit error. The retry then marked the child's Turn as "lost its worker before it could pause", so the parent never saw the limit.

The $100 default comes from Sentry span cost estimates: one child agent that made 250 tool calls cost about $50.

Not in this PR:

  • One budget shared by the root conversation and its child agents. Each child agent gets its own budget.
  • The root and scheduler path in task-execution/conversation-turn.ts, which still retries limit failures.

The new subagent eval uses a 3-call limit to stand in for a spent budget.

Context: JUNIOR-96, event 49c00ccc6e12485e9f4b18a97c3010d6.

via David Cramer.

--

View Junior Session [Sentry]

sentry-junior Bot and others added 3 commits October 15, 2026 16:16
A turn now stops at a model spend budget (default $100) instead of a
250 tool-call cap. The tool-call cap stays as a 1,000-call backstop for
models without cost data.

When a turn reaches a limit, Junior blocks the next tool call and tells
the model to reply with its progress. A child agent sends that reply to
its parent as a normal result. Only a model that keeps calling tools
after that fails the turn.

An execution-limit failure in a child agent is now final on the first
attempt. Before, the retry marked the Turn as a lost worker and hid the
limit from the parent.

Co-Authored-By: David Cramer <[email protected]>
@vercel

vercel Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated
junior-docs Ready Ready Preview Oct 15, 2026 6:30pm UTC

Request Review

@dcramer
dcramer marked this pull request as ready for review October 15, 2026 17:26
@github-actions github-actions Bot added the risk: high PR risk score: high label Oct 15, 2026

@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.

Stale Bugbot comment from a previous run.

Comment thread packages/junior/src/chat/agent/index.ts Resolved
The integration evals recorded these model responses in https://github.com/getsentry/junior/actions/runs/38777214674.
The evals of this commit replay them in strict mode.

Eval-Suite: integration
@sentry-evals

sentry-evals Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Sentry Evals

junior-behavioral

Run Status Passed Failed Errored Cost Tokens Duration
Head Complete 64 10 0 $0.62 0 240.76s

Base commit a96aaac has no eval run to compare against.

junior-guardian

Run Status Passed Failed Errored Cost Tokens Duration
Head Complete 53 0 5 $0.01 0 2.13s

Base commit a96aaac has no eval run to compare against.

junior-integration

Run Status Passed Failed Errored Cost Tokens Duration
Head Complete 98 1 0 $0.95 0 190.61s

Base commit a96aaac has no eval run to compare against.

junior-router

Run Status Passed Failed Errored Cost Tokens Duration
Head Complete 12 0 0 $0.00 0 0.51s

Base commit a96aaac has no eval run to compare against.

…lices

Add discarded retry usage to the live spend check so provider,
empty-output, and confirmation retries count toward maxTurnCostUsd.

Persist the turn-limit wrap-up on the turn checkpoint. A resumed slice
now stops instead of granting another wrap-up, so a model that keeps
calling tools cannot loop through cooperative yields.

@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 8f03725. Configure here.

The integration evals recorded these model responses in https://github.com/getsentry/junior/actions/runs/38781177646.
The evals of this commit replay them in strict mode.

Eval-Suite: integration
Once a step was told to wrap up, a later tool-calling step stops the
turn even when the live totals no longer read as over the limit, such as
a resumed slice whose saved usage missed the wrap-up step.
The integration evals recorded these model responses in https://github.com/getsentry/junior/actions/runs/38782319339.
The evals of this commit replay them in strict mode.

Eval-Suite: integration
Pass the saved wrap-up to the gate once per slice instead of on every
call, and trim the limit comments.

Co-Authored-By: David Cramer <[email protected]>
The behavioral evals recorded these model responses in https://github.com/getsentry/junior/actions/runs/38783763368.
The evals of this commit replay them in strict mode.

Eval-Suite: behavioral
@dcramer
dcramer merged commit d867adb into main Oct 15, 2026
53 of 55 checks passed
@dcramer
dcramer deleted the feat/turn-spend-budget branch October 15, 2026 19:49

This branch was successfully deployed

1 active deployment
Preview – junior-docs — bcaf9f31 Deployed Oct 15, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: high PR risk score: high

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant