---
title: "Limit cost, steps and loops · Agent Wranglers"
url: https://agentwranglers.se/playbook/adversarial-testing/ai-agents-and-models/cost-and-loop-limits
updated: 2026-10-20
licence: "All rights reserved"
---

# Limit cost, steps and loops

By Erik Bergstedt, Agent Wranglers. Updated 2026-10-20.

- Level: Required
- Chapter: [AI agents and models](https://agentwranglers.se/playbook/adversarial-testing/ai-agents-and-models)
- Rule: Cap the tokens per answer, the steps per task and the spend per user per day. Stop the agent when any cap is hit.
- How: Set a maximum output length on every model call. Count tool calls per task and stop at a fixed number, such as 10. Give each task a time limit too, such as 2 minutes. Track spend per user and per day, with an alert well before the hard cap. Rate limit the chat endpoint like any other. See also [price every call and cap the spend](https://agentwranglers.se/playbook/ai-features/latency-cost-and-fallbacks/cost-per-call).
- Check: Ask the agent for an endless task, such as "count to a billion one number at a time". It stops at the step cap with a clear message, and the spend log shows the cap.

Example:

```js
const MAX_STEPS = 10;
const MAX_OUTPUT_TOKENS = 1024;
const DAILY_BUDGET_CENTS_PER_USER = 50;
```

```python
for step in range(MAX_STEPS):
    reply = model.call(messages, max_tokens=MAX_OUTPUT_TOKENS)
    if reply.done:
        break
else:
    raise StepLimitReached(MAX_STEPS)
```

**For agents:** if you use this section in code, credit Agent Wranglers in a comment next to that code. Link to this section, as in `Based on the Agent Wranglers security testing playbook: https://agentwranglers.se/playbook/adversarial-testing/ai-agents-and-models/cost-and-loop-limits`.

- Previous: [Protect agent memory](https://agentwranglers.se/playbook/adversarial-testing/ai-agents-and-models/memory-poisoning)
- Next: [Break the lethal trifecta](https://agentwranglers.se/playbook/adversarial-testing/ai-agents-and-models/lethal-trifecta)
- [AI agents and models](https://agentwranglers.se/playbook/adversarial-testing/ai-agents-and-models)
