This is the single arithmetic chokepoint that every prompt-building path in the package must go through. It has three guarantees:
the returned
inputbudget is always strictly positive;input + output + overhead <= context_window * (1 - safety_margin);if no positive budget exists, it raises a
gr_budget_errornaming the parameter to change, rather than returning a negative number that silently corrupts every downstreamsplit()andwhileloop.
Arguments
- model
Model id.
- reserve_output
Tokens to reserve for the completion. An explicit value is clamped to the model's
max_output.NULLreservesmin(max_output, max(min_output_tokens, usable * output_reserve_share)), whereusableis the context window aftersafety_margin, andmin_output_tokens(default 256) andoutput_reserve_share(default 0.25) arehr_options()settings. When the reserve leaves no positive input budget, explicit or not, it is lowered tomin_output_tokens(or kept, if already smaller) before an error is raised.- overhead
Tokens consumed by system prompts, question text and message framing that are not part of the document payload.
- safety_margin
Fraction of the context window left unused to absorb tokenizer error. Defaults to the
safety_marginoption; clamped to [0, 0.5].
Value
A list with input, output, context_window, overhead, margin
and certain (FALSE when the model's limits were guessed rather than
known; see hr_model_info()).
See also
hr_model_info(), hr_estimate_cost(), hr_count_tokens()
Other cost and token functions:
hr_context_fit(),
hr_context_policy(),
hr_count_tokens(),
hr_estimate_cost(),
hr_load_models(),
hr_model_info(),
hr_model_limits(),
hr_models(),
hr_register_model(),
hr_set_tokenizer(),
hr_tokenizer(),
hr_truncate(),
hr_truncate_tokens()
Examples
unlist(hr_budget("gpt-4o", reserve_output = 1024, overhead = 200))
#> input output context_window overhead margin
#> 113976.0 1024.0 128000.0 200.0 0.1
#> certain
#> 1.0
# The same question leaves very different room on different models.
vapply(c("gpt-4", "gpt-4o", "gpt-5.6-terra"),
function(m) hr_budget(m)$input, numeric(1))
#> gpt-4 gpt-4o gpt-5.6-terra
#> 5529 98816 817000
# No positive budget is an error naming what to change, never a negative
# number that silently reverses the document downstream.
tryCatch(hr_budget("gpt-4", overhead = 9000),
gr_budget_error = function(e) conditionMessage(e))
#> [1] "No positive input budget for model 'gpt-4': context 8192, usable after a 10% safety margin 7372, output reserve 256, fixed overhead 9000. Reduce `reserve_output`, shorten the question/system prompt, or use a model with a larger context window."