Skip to contents

Budgets are hard limits, checked before every model call. When a limit would be exceeded, the agent raises a condition of class llmragent_budget_error instead of calling the API, so a runaway loop cannot spend without leaving a record. Catch it with tryCatch() if you want graceful degradation.

Usage

budget(
  max_calls = Inf,
  max_tokens = Inf,
  max_tool_calls = Inf,
  max_seconds = Inf
)

Arguments

max_calls

Maximum number of model calls.

max_tokens

Maximum total tokens (sent + received) across calls.

max_tool_calls

Maximum executed tool invocations.

max_seconds

Wall-clock ceiling, measured from the agent's first call.

Value

An object of class agent_budget.

Examples

b <- budget(max_calls = 10, max_tokens = 50000)

if (FALSE) { # \dontrun{
cfg <- LLMR::llm_config("groq", "openai/gpt-oss-20b")
frugal <- agent("Frugal", cfg, budget = budget(max_calls = 2))
frugal$chat("one")
frugal$chat("two")
tryCatch(frugal$chat("three"),
         llmragent_budget_error = function(e) "refused before spending")
} # }