Topic 2 of 5 · 4 min read
Per-run and per-agent spending limits
Goal: Set per-agent and per-run limits for an orchestrated job and state what the system does when each limit is reached.
A budget is a set of numeric limits that the orchestrator enforces in code, each with a defined action when it is hit. A line in a prompt such as "be economical" is not a limit. Models do not count their own spend reliably and will not stop a loop they believe is making progress.
Check yourself · 1 of 3
What makes a spending limit real in a multi-agent system?
Why it matters
A single stuck worker can retry a failing tool for hours. A lead that spawns agents without a cap can spawn them in a loop. Without hard limits, the first sign of trouble is the invoice.
Limits to set
- Turns per agent. A worker with a clear job rarely needs more than 10 to 20 turns. Set the cap near twice the normal run.
- Tokens per agent. Input plus output, summed over the agent's turns.
- Agents spawned per run, and spawn depth. Cap the total and forbid workers from spawning workers unless the design needs it.
- Tokens or money per run. The total across all agents. This is the limit that protects the budget.
- Wall-clock time. Catches hangs that burn no tokens but hold resources.
Decide the action in advance
Each limit needs one of: stop and report (return partial results with a note), degrade (switch the remaining workers to a cheaper model or fewer sources), or ask a human (pause for approval to continue). Pick per limit. A turn cap on a worker usually means "stop and return what you have"; a run-level money cap usually means "stop everything and alert".
Example in code
const limits = {
worker: { maxTurns: 15, maxTokens: 150_000 },
run: { maxWorkers: 6, maxTokens: 1_200_000, maxSeconds: 600 },
};
function check(run: Run, agent: Agent): "ok" | "stop_agent" | "stop_run" {
if (run.tokens > limits.run.maxTokens) return "stop_run";
if (run.workers > limits.run.maxWorkers) return "stop_run";
if (agent.turns > limits.worker.maxTurns) return "stop_agent";
if (agent.tokens > limits.worker.maxTokens) return "stop_agent";
return "ok";
}
Call check before every model call, using token counts the API returns. Do not rely on the agent to report them.
Before and after
Weak: the system prompt says "Keep costs low and stop when you have enough." The worker loops 60 turns on a flaky site.
Better: the orchestrator ends the worker at turn 15, records "stopped: turn limit", and passes the lead the partial notes. The lead decides whether to retry with a different source.
What goes wrong
- Only a run-level cap. One worker can use the whole budget and starve the others. Add per-agent caps.
- Limits checked after the call. Check before each call, or one large call overshoots.
- No defined action. A limit that just throws an error loses the partial work already paid for.
- Caps set at the average. Normal variation then trips them. Set them above the normal tail, below the disaster.
Examples
Partial results on a turn limit
A research worker hits its 15-turn cap after finding 4 of 6 sources. The orchestrator returns {status: "stopped_turn_limit", findings: [...4 items...]}. The lead notes the gap and either accepts it or spawns one new worker for the missing two.
Degrade instead of stop
At 80% of the run's token budget, the orchestrator switches remaining workers to a cheaper model and a shorter output limit. At 100% it stops the run and sends an alert. Two thresholds give the run a chance to finish before the hard stop.
Key points
- Limits must be enforced by the orchestrator in code; a prompt instruction to save tokens is not a limit.
- Cap turns, tokens and wall-clock time per agent, and agents spawned, depth and total spend per run.
- Define the action for each limit in advance: stop and report, degrade, or ask a human.
- Check limits before each model call, using usage numbers from the API rather than the agent's own report.
- Set caps above the normal tail of measured runs but well below the cost of a runaway.
Common mistakes
- Relying on a prompt line such as "stay under budget" instead of enforced limits.
- Setting only a run total so one worker can consume everything.
- Throwing an error on a limit and discarding the partial work already paid for.
- Setting caps at the average run, so ordinary variation trips them.
Check yourself · 2 of 3
A worker hits its turn cap with 4 of 6 sources found. What should the orchestrator do by default?
Check yourself · 3 of 3
Which set of limits best protects a run from both one stuck worker and a spawn loop?
Practice items, not counted towards your mastery.
Start here
- How we built our multi-agent research system (Anthropic) (opens in a new tab)ArticleFree
Reports that agents use about 4x and multi-agent systems about 15x the tokens of chat, and that value must justify it.
- Prompt caching (Claude docs) (opens in a new tab)DocsFree
How cache writes and reads are priced, and how a shared prefix is reused across calls.
- Batch API (OpenAI) (opens in a new tab)DocsFree
Asynchronous batch jobs for work that can wait, at a lower price than live calls.
More for this topic
- Manage costs effectively (Claude Code docs) (opens in a new tab)DocsFree
Shows spend-tracking and limit options in a real agent tool.