Budgets & Timeouts
Every agent has a budget. defineModelAgent() fills in the defaults, and your values are merged over them.
| Field | Default | Kind | Effect |
|---|---|---|---|
maxSteps | 20 | stop condition | Parent agent-loop steps per run, across all executions |
maxTokens | none | stop condition | Parent agent-loop input plus output tokens per run, across all executions |
timeoutMs | 900_000 (15 min) | abort signal | Wall-clock limit per execution |
maxSubtreeTokens | none | dispatch refusal | Input plus output tokens of every run below this one; once reached, no new dispatch |
// defined once at module scope: each call gets its run's context
const lastReport = defineTool({
description: 'Returns the date of the previous report.',
inputSchema: z.object({}),
execute: async (_input, { context }) => (await context.state.get<string>('lastReport')) ?? 'never',
});
const saveReport = defineTool({
description: 'Remembers that a report was written now.',
inputSchema: z.object({}),
execute: async (_input, { context }) => {
await context.state.set('lastReport', context.run.trigger.receivedAt.toISOString());
return { saved: true };
},
});
export const reporter = defineModelAgent({
name: 'weekly-report',
model,
instructions: 'Summarize the open work items in the trigger payload.',
channels: [
http.on(),
cron.on('0 8 * * 1', { timezone: 'Europe/Berlin' }),
],
budget: {
maxSteps: 10,
maxTokens: 50_000,
timeoutMs: 5 * 60 * 1000,
},
tools: { lastReport, saveReport },
});How a budget settles a run
| Situation | Status | error.code |
|---|---|---|
| The step or token limit is reached and the last step asked for tool calls | failed | BudgetExceeded |
| The step or token limit is reached and the last step was a final answer | completed | |
The execution ran longer than timeoutMs | failed | Timeout |
The step budget counts checkpointed steps, so a run resumed after a crash or an approval continues with what is left. The token budget adds the usage of earlier executions (run.usage) to the current one. These are stop conditions checked after a step, not hard provider-side token ceilings.
Step/token budgets and run.usage cover only the parent agent loop. Model calls made inside tools, including extract() and direct AI SDK calls using resolveModel(), are not included. Nested calls must pass context.run.signal to share the execution timeout; extract() does this automatically.
The timeout restarts with every execution. A run that waits two days for an approval is not timed out by that wait; only the time spent executing counts, per execution. The timeout aborts the run.signal your tools receive, so pass it on to long-running work.
One step beyond the limit after an approval
A resumed approval gets exactly one step beyond maxSteps. An approval asked for on the last allowed step is therefore still answered: the approved call runs (or the denial is recorded) and the model gets one more call to react.
Token usage
run.usage holds { inputTokens, outputTokens } summed over every step of the run. Each llm step stores its own usage in output.usage. A provider that reports no usage counts as zero.
Subtree tokens
A run's own budget does not cover the runs it dispatched: each child has its own. run.subtreeUsage is the sum of every descendant's usage (children, their children, and so on), recomputed from the children whenever one settles, so it is never counted twice.
budget.maxSubtreeTokens bounds a whole tree: once the subtreeUsage (input plus output tokens) of a run of this agent reached it, dispatch() and dispatchTo() in that run, and in every run below it, fail with a BudgetExceeded tool error (not fatal: the model sees it and can finish with what it has). It is checked when a dispatch starts, against totals summed from the children at that moment (a stored subtreeUsage a failed update left behind cannot let a dispatch through); children already running go on. A call recorded before the limit (a redo after a crash or a wait) keeps its child.
defineModelAgent({
name: 'auditor',
// ...
budget: { maxSubtreeTokens: 2_000_000 },
});The audit example sets it on the agent that fans out.