Token budget tracker
Token budget tracker
Details
Monitors token consumption and detects when the agent loop should stop due to context exhaustion or diminishing returns.
Methods
Method should_stop()
Determine whether the agent loop should stop.
Usage
BudgetTracker$should_stop(
current_tokens,
max_tokens,
iteration = 1L,
is_subagent = FALSE,
current_cost_usd = NA_real_,
max_budget_usd = NULL
)Arguments
current_tokensInteger. Current total token count.
max_tokensInteger. Maximum allowed tokens (model context limit).
iterationInteger. Current loop iteration (1-indexed).
is_subagentLogical. If TRUE, budget limits are not applied.
current_cost_usdNumeric or NA. Current session spend in US dollars (e.g. from
.current_cost_usd()); NA when unknown/unpriced.max_budget_usdNumeric or NULL. Hard dollar cap; NULL disables the check. When set and
current_cost_usdis known, this fires immediately (bypassingiteration/.BUDGET_MIN_ITERATIONS) – a dollar cap is a hard stop, not a heuristic.