Sub topic 12
Rate Limits and Budgets as Blast-Radius Control
This chapter assumes you know how an agent loop turns model output into tool calls, and that you have already scoped tools and enforced authorization in code. Those controls decide what an agent may do. Usage limits decide how much of it can happen before someone notices.
Why limits belong in a least-privilege design
A correctly scoped tool can still do damage in volume. An agent allowed to call send_email ten times for a legitimate task can, after a prompt injection or a looping bug, call it ten thousand times. Each call passes the allowlist check. The harm comes from the count.
Rate limits and budgets cap that count. They do not stop a bad action. They bound its blast radius, which is the total damage one compromised or confused run can cause.
Kinds of limits
| Limit | What it bounds | Example |
|---|---|---|
| Rate limit | Calls per time window | 5 `search_docs` calls per minute |
| Call budget | Total calls per task | 20 tool calls per run |
| Step limit | Loop iterations | Stop after 15 model turns |
| Cost budget | Money or tokens | 50,000 tokens per task |
| Per-tool quota | Calls to one risky tool | 3 `send_email` calls per task |
Per-tool quotas matter most. Reads are cheap and reversible. Writes and outbound messages are not, so give them much tighter limits than reads.
Enforce limits in the runtime
As with authorization, limits must live in ordinary code outside the model. A prompt that says "never call more than 3 emails" is a request, not a control. The runtime counts calls and refuses the ones over the limit.
class BudgetExceeded(Exception):
pass
class UsageLimiter:
def __init__(self, max_calls: int, per_tool: dict[str, int]):
self.max_calls = max_calls
self.per_tool = per_tool
self.total = 0
self.counts: dict[str, int] = {}
def check(self, name: str) -> None:
if self.total >= self.max_calls:
raise BudgetExceeded("task call budget used up")
used = self.counts.get(name, 0)
limit = self.per_tool.get(name)
if limit is not None and used >= limit:
raise BudgetExceeded(f"quota for {name!r} used up")
self.total += 1
self.counts[name] = used + 1
limiter = UsageLimiter(max_calls=20, per_tool={"send_email": 3})
for _ in range(5):
try:
limiter.check("send_email")
print("send_email allowed")
except BudgetExceeded as err:
print("refused:", err)The first three calls pass and the last two are refused. Call limiter.check(name) inside run_tool_call, after the allowlist check and before the tool runs.
Choosing values and handling refusals
Set limits from observed normal use. Run the task on typical inputs, record how many calls it makes, and set the cap at a small multiple of the maximum you saw. A cap far above normal use gives little protection.
When a limit trips, do not silently retry or let the model argue its way past it. Return a clear error, stop the run or escalate to a person, and write the event to the audit log. A tripped limit is a useful signal. Normal tasks rarely hit their caps, so a hit often means a loop or an attack.
Also set limits at the account level. Provider spend caps and API quotas on the agent's credentials catch cases where your own runtime counter is bypassed or buggy.
Limits do not replace other controls
Limits reduce volume, not impact per call. One delete_record call can be one too many, so pair quotas with human approval for irreversible actions. A slow, low-volume exfiltration can also stay under any rate limit, so data-flow separation is still needed.
Exercise
An agent that summarizes support tickets has tools read_ticket and send_email. Normal runs read up to 12 tickets and send 1 email. Propose a call budget and per-tool quotas, and explain why.
Show answer
For example, a total budget of 30 calls, with read_ticket allowed up to 20 and send_email limited to 2. Reads are reversible, so a loose cap with some margin over the observed 12 is fine. The email quota stays at a small multiple of the normal 1 because outbound messages cannot be undone. Enforce the caps in the runtime, and log and stop the run when one trips.