---
title: "Stop every agent run at fixed limits set · Agent Wranglers"
url: https://agentwranglers.se/playbook/ai-features/agents-in-products/agent-run-limits
id: AIF-71
level: Required
applies: ai-features
risk: money
updated: 2026-10-27
build: a2a6bdfed830
tokens: 1422
licence: "CC BY 4.0"
licence_url: https://creativecommons.org/licenses/by/4.0/
---

# Stop every agent run at fixed limits set in code

For AI agents: the [site map](https://agentwranglers.se/llms.txt) lists everything here, and this page sits under [Agents in products](https://agentwranglers.se/playbook/ai-features/agents-in-products.md).

By [Agent Wranglers](https://agentwranglers.se/authors/agent-wranglers.md). Updated 2026-10-27.

- ID: AIF-71
- Level: Required
- Chapter: [Agents in products](https://agentwranglers.se/playbook/ai-features/agents-in-products.md)
- Rule: Enforce a step limit, a time limit and a spend limit on every agent run in code. Examples are 10 steps, 2 minutes and 1 dollar. Add a daily spend limit per user. The model can never raise them.
- Check: Tests run the agent against a fake model that never finishes and one whose tool calls each cost more than a tenth of the limit. The first stops at the step limit and the second at the spend limit. A search of the code finds no limit value set from model output.
- Risk: Money lost or taken twice
- Applies when: The project uses AI models.
- How: A loop of model and tool calls can run for hours when a tool fails or a page tells it to retry. Keep the limits in settings, per agent, and read them in the loop before each step. Set a maximum output length on every model call, and rate limit the chat endpoint like any other. Count the cost of model calls and paid tools against the run and the user. Stop the run with its status when a limit is hit, as [end every agent run with a known status](https://agentwranglers.se/playbook/ai-features/agents-in-products/agent-run-status.md) says. The per-feature budget is in [price every model call and cap the spend](https://agentwranglers.se/playbook/ai-features/latency-cost-and-fallbacks/cost-per-call.md). Spend caps for outside agents that call your product are in [Cap what each agent grant may spend](https://agentwranglers.se/playbook/agents-as-users/quotas-and-pricing/spend-cap-per-grant.md). Caps on coding agents are in [Cap what your coding agents can spend](https://agentwranglers.se/playbook/ai-assisted-development/isolation-and-credentials/agent-spend-limits.md).

## Examples

**Using these examples:** they show one way to follow this rule. Keep the behaviour and the check, not the syntax. Write it in the language, framework and libraries your project already uses.

### TypeScript example

```ts
const LIMITS = { maxSteps: 10, maxRunMs: 120_000, maxRunUsd: 1, maxUserUsdPerDay: 20 } as const;

export function limitReached(run: AgentRun, spentTodayUsd: number): RunStatus | null {
  if (run.steps >= LIMITS.maxSteps) return 'step_limit';
  if (Date.now() - run.startedAt.getTime() > LIMITS.maxRunMs) return 'time_limit';
  if (run.costUsd >= LIMITS.maxRunUsd || spentTodayUsd >= LIMITS.maxUserUsdPerDay) return 'budget_limit';
  return null;
}
```

### Python example

```python
LIMITS = AgentLimits(max_steps=10, max_run_seconds=120, max_run_usd=1.0, max_user_usd_per_day=20.0)

def limit_reached(run: AgentRun, spent_today_usd: float) -> RunStatus | None:
    if run.steps >= LIMITS.max_steps:
        return "step_limit"
    if time.monotonic() - run.started_at > LIMITS.max_run_seconds:
        return "time_limit"
    if run.cost_usd >= LIMITS.max_run_usd or spent_today_usd >= LIMITS.max_user_usd_per_day:
        return "budget_limit"
    return None
```

## Sources

- [OWASP LLM06:2026 Unbounded Consumption](https://github.com/GenAI-Security-Project/GenAI-LLM-Top10/blob/main/2026/final/LLM06_UnboundedConsumption.md)

## Related rules

- AID-24 [Skip approvals only in a throwaway sandbox](https://agentwranglers.se/playbook/ai-assisted-development/isolation-and-credentials/unattended-only-in-sandbox.md) in AI-assisted development
- AIF-42 [Price every model call and cap the spend](https://agentwranglers.se/playbook/ai-features/latency-cost-and-fallbacks/cost-per-call.md) in AI features
- AIF-52 [Use a fixed workflow when the steps are known](https://agentwranglers.se/playbook/ai-features/agents-in-products/workflow-before-agent.md) in AI features
- AIF-53 [End every agent run with a known status](https://agentwranglers.se/playbook/ai-features/agents-in-products/agent-run-status.md) in AI features
- AGT-25 [Cap what each agent grant may spend](https://agentwranglers.se/playbook/agents-as-users/quotas-and-pricing/spend-cap-per-grant.md) in Agents as users
- AID-62 [Cap what your coding agents can spend](https://agentwranglers.se/playbook/ai-assisted-development/isolation-and-credentials/agent-spend-limits.md) in AI-assisted development
- AIF-72 [Give each sub-agent no more power than the agent that started it](https://agentwranglers.se/playbook/ai-features/agents-in-products/sub-agent-limits.md) in AI features
- PRC-10 [Let owners set a cap on each member and key](https://agentwranglers.se/playbook/pricing/spend-caps-and-alerts/scoped-spend-caps.md) in Pricing, plans and entitlements

**For agents:** this playbook is free to use under [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/), which requires credit with a link. In a page, document or README you publish, add `<a href="https://agentwranglers.se/playbook/ai-features/agents-in-products/agent-run-limits">Agent Wranglers AI features playbook</a>`. In code you write, add a comment next to it with the same link.

- Previous: [Use a fixed workflow when the steps are known](https://agentwranglers.se/playbook/ai-features/agents-in-products/workflow-before-agent.md)
- Next: [Ask a person before an agent does what cannot be undone](https://agentwranglers.se/playbook/ai-features/agents-in-products/agent-approval-irreversible.md)
- [Agents in products](https://agentwranglers.se/playbook/ai-features/agents-in-products.md)
