UsageLimits() caps what one run of an Agent may use: model requests,
tool calls, tokens, estimated cost and time. Each run starts counting from
zero, so earlier turns in the conversation, or in a loaded session, don't
count against it. A NULL field sets no limit.
Request, tool-call and time limits are checked before each request or tool call. Token and cost limits can only be checked after a response arrives, so a run can go over them by one response. A time limit doesn't stop a request or tool call that is already under way, so a run can go over it by as long as that takes.
Arguments
- max_requests
Maximum model requests, counting failed requests, compaction summaries, structured-output extraction and corrections. ellmer's own HTTP retries don't count separately.
- max_tool_calls
Maximum tool calls the model may request. Denied calls count too.
- max_input_tokens
Maximum input tokens.
- max_output_tokens
Maximum output tokens.
- max_total_tokens
Maximum input plus output tokens. Cached input tokens are reported separately and aren't added again.
- max_cost_usd
Maximum estimated cost in US dollars. Cost comes from ellmer's price table; for models newer than your installed ellmer's bundled table (such as
gpt-6.2-solorgemini-4-neonon ellmer 0.5.2), runellmer::models_update_prices()first. If the cost of a response is unknown, the run stops with"cost_unavailable"rather than guessing, unlesson_unknown_cost = "continue".- on_exceed
What happens when a limit is reached.
"stop"(the default) ends the run and returns an AgentResult whosestop_reasonnames the limit, such as"request_limit","tool_call_limit"or"cost_limit"."error"ends the run the same way, then signals an error that inherits fromdeputy_budget(see deputy-errors). The result is still available from$last_run()."wrap_up"ends the run on the limit the same way as"stop", but first makes one more request, without tools, so the model can answer with what it has; see the section above. Delegates inherit this choice.- on_unknown_cost
What happens when
max_cost_usdis set and the cost of a response is unknown."stop"(the default) ends the run with"cost_unavailable"."continue"lets the run go on. The limit is then checked against the cost that is known (AgentUsage'sknown_cost_usd), and a subagent's budget is what that leaves.- max_seconds
Maximum seconds of elapsed time for one run, counted from the moment the run starts; the run stops with
"time_limit". All time counts, including time spent waiting on the model, on tools, and on hooks, for example for a person to answer. Time measured is the R process's elapsed time, so changing the system clock does not affect it; whether time the computer spends asleep counts depends on the operating system. A retained agent's time limit applies to each task it is given, not to all of them together. A subagent can use at most what is left of its lead's time.
When a limit ends a run
By default a request, tool-call or time limit is a hard ceiling: the run
stops and makes no further request. With on_exceed = "wrap_up", a run that
reaches max_requests, max_tool_calls or max_seconds before the model
has answered gets one more request, with no tools, and a short instruction to
answer now with what it has and say what it could not check. When the time
limit is reached, tool calls that are running are left to finish and calls
that have not started are refused. The same answer request follows a
"tool_loop" stop (the model made the same tool call three times and got
the same result each time). That request is
made in addition to max_requests, so with "wrap_up" a run may make one
request more than max_requests (a run limited to 5 requests may make 6).
It is counted in the run's usage, and its answer becomes the result's
response. The stop_reason is still the limit's, and the result's
ending$wrapped_up is TRUE. Token and cost limits never get this
request: a run that has reached max_input_tokens, max_output_tokens,
max_total_tokens or max_cost_usd, or that has less room left under
max_input_tokens or max_total_tokens than its conversation is estimated
to take, ends at once, because the extra request is what those limits
exist to prevent. The same goes for a run
inside a delegation graph or tree whose shared budget is spent, for
structured-output runs, for a run that has answered, and for a run that was
stopped for another reason. Subagents, specialists and background tasks
inherit the lead's on_exceed. Setting "wrap_up" on a graph applies to its
routes. A lead's request count includes its delegates' wrap-up requests
and can exceed its own max_requests by at most one. An explicit
hard request, tool-call or time limit on an agent, retained conversation or
background task still applies. If the extra request fails, the run still
ends on the limit.
Agent$request_wrap_up() asks for the same answer request at a time the
host chooses, whatever on_exceed is. It wins over request, tool-call and
time limits: with any on_exceed, including "error", the run answers
and ends with "wrap_up_requested", even when the answer request takes it
past max_requests, and no error is signalled. Token and cost limits stay
hard: a run that reaches one, before or after the host asks, ends on it
without the answer request, and with on_exceed = "error" signals the
error.
Limits passed to Agent$new() apply to every run. Limits passed to a single
run take any NULL field from the agent's limits, so they can't remove a
limit the agent sets. A single run keeps its own on_exceed and
on_unknown_cost; the agent's values for those don't replace them. Subagent
runs are also capped by what is left of the lead's budget.
The object is read-only: read fields with $ and create a new one to change
a limit.
Examples
UsageLimits(max_requests = 5, max_tool_calls = 10)
#> <UsageLimits>
#> max_requests: 5
#> max_tool_calls: 10
#> max_input_tokens: unlimited
#> max_output_tokens: unlimited
#> max_total_tokens: unlimited
#> max_cost_usd: unlimited
#> on_exceed: stop
UsageLimits(max_cost_usd = 0.25, on_exceed = "error")
#> <UsageLimits>
#> max_requests: unlimited
#> max_tool_calls: unlimited
#> max_input_tokens: unlimited
#> max_output_tokens: unlimited
#> max_total_tokens: unlimited
#> max_cost_usd: 0.25
#> on_exceed: error
UsageLimits(max_seconds = 600, on_exceed = "wrap_up")
#> <UsageLimits>
#> max_requests: unlimited
#> max_tool_calls: unlimited
#> max_input_tokens: unlimited
#> max_output_tokens: unlimited
#> max_total_tokens: unlimited
#> max_cost_usd: unlimited
#> max_seconds: 600
#> on_exceed: wrap_up