scan: expose token budget stop state in JSON output
Problem
ocr scan --format json --max-tokens-budget ... already has an aggregate token budget gate: when the running token usage plus the next file estimate would exceed the configured budget, scan stops dispatching remaining files and records a token_budget_reached warning.
However, the JSON summary does not expose this state through summary.budget_exceeded=true. For automation users, this makes it hard to distinguish:
- a complete scan with no budget pressure
- a partial scan that stopped because the token budget gate was reached
This is mainly a machine-readable reporting gap rather than a request to change scan scheduling behavior.
Why this matters
CI, GitHub Actions, dashboards, or other wrappers may consume --format json to decide whether a scan fully covered the selected files. If budget exhaustion is only visible as a warning item, consumers need to duplicate warning parsing logic instead of relying on the existing summary field.
A clearer contract would make summary.budget_exceeded reflect the aggregate budget gate consistently across review/scan result output.
Current code path
Relevant code:
internal/scan/agent.go:616-625: scan detects the aggregate budget gate, recordstoken_budget_reached, sets localbudgetHit = true, and breaks dispatch.internal/scan/agent.go:522-544: the outer scan loop receivesbudgetHitand stops scheduling subsequent batches.internal/scan/agent.go:219-225:BudgetExceeded()is currently hard-coded to returnfalsefor scan.cmd/opencodereview/shared.go:354-357: JSON output passesag.BudgetExceeded()into the output layer.cmd/opencodereview/output.go:316-325:summary.budget_exceededis populated from that value.
In short, scan has the local budgetHit signal, but that signal is not carried into the result provider state used by JSON output.
Expected behavior
When the aggregate token budget gate stops ocr scan from dispatching remaining files, JSON output should include:
{
"summary": {
"budget_exceeded": true
},
"warnings": [
{
"type": "token_budget_reached"
}
]
}
When scan completes without hitting the aggregate budget gate, the current behavior can stay unchanged: summary.budget_exceeded may remain false or be omitted because of omitempty.
Suggested implementation
A narrow implementation could be:
- Add a
budgetExceeded boolfield tointernal/scan.Agent. - When
dispatchBatchreturnsbudgetHit == true, seta.budgetExceeded = truebefore breaking out of the scan loop. - Change
scan.Agent.BudgetExceeded()to returna.budgetExceededinstead of always returningfalse. - Add a focused test for
ocr scan --format json --max-tokens-budget ...verifying:warningsstill containstoken_budget_reached.summary.budget_exceeded == truewhen the budget gate stops dispatch.- normal successful scans still omit or report false for
budget_exceeded.
Non-goals
This does not need to change:
- scan dispatch policy
- exit-code semantics
- comment generation
- warning generation
- manifest coverage behavior
The requested scope is only to expose an already-detected scan state in the existing machine-readable summary field.
I would like to look into this
The aggregate token budget gate you describe (--max-tokens-budget) is the right concept — stopping when running usage + next chunk exceeds the budget. But it only works if the tool knows the cost of the next chunk before it starts.
The gap: token counts are available after the call, not before. By the time you know the next chunk is 50,000 tokens, you may have already spent $5 on it.
AgentShield approaches this from the pre-dispatch side. Instead of counting tokens after the fact, it evaluates the estimated cost of each API call before it executes:
- Estimate from prompt length (~4 chars/token) and model rate
- Check against rules: transaction_limit, session_budget, daily_total, cascade_cost
- Block or allow in under 1ms
- The agent never sends the request if it is blocked
For a code review tool specifically, the cascade_cost rule type is relevant: a review call that triggers follow-up analysis calls has a cascade cost (base call + probability of follow-up × follow-up cost). Setting a max_cascade_cost threshold catches multi-step review chains before they compound.
Repo: https://github.com/kindrat86/agentshield
Disclosure: I built this. MIT licensed.