Budget cap lags one iteration behind tool payload growth #53
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Discovered during M3.1 stress testing (Issue #44, Q4).
Symptom
Ran
ask "Comprehensive history of AI 1950 to 2026" --budget 5000 --max-iterations 2. Result:budget_exhausted: FalseBudget status: under capBUDGET_EXHAUSTEDgap surfaced.Trace
38235720-6efc-4d7d-b284-6e21b1c83d46:Root cause
researchers/web/agent.py:260-270. The loop checkstotal_tokens >= constraints.token_budgetat the top of each iteration.total_tokensis incremented fromresponse.usageafter each model call, so it reflects the previous iteration's usage.Iter 1's input is tiny (just the user question + system prompt) → ~1145 tokens. The check at the top of iter 2 sees 1145 < 5000, lets iter 2 run. Iter 2's model call has a huge input (all the fetched tool results from iter 1's tool calls), pushing total to 10606. The loop then exits naturally because
iterations < max_iterationsis False (2 < 2). The budget check never sees the inflated count.For small
max_iterations, the cap may never trip even when actual usage is multiples over budget.Out of scope
Synthesis being uncapped is by design (agent.py:254-259) — not part of this bug.
Suggested fix
Either:
messageslength before deciding to enter the next iteration, orOption 1 is simpler and matches the "soft cap" semantic.
Acceptance
A query with
--budget 5000 --max-iterations 2that fetches large pages should setbudget_exhausted=Trueand surface aBUDGET_EXHAUSTEDgap.