AI agents and models Required
Limit cost, steps and loops
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 to do it
Set a maximum output length on every model call. Count tool calls per task and stop at a fixed number. Track spend per user and per day, with an alert well before the hard cap. Rate limit the chat endpoint like any other.
Example
const MAX_STEPS = 10;
const MAX_OUTPUT_TOKENS = 1024;
const DAILY_BUDGET_CENTS_PER_USER = 50;
for step in range(MAX_STEPS):
reply = model.call(messages, max_tokens=MAX_OUTPUT_TOKENS)
if reply.done:
break
else:
raise StepLimitReached(MAX_STEPS)
How to 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.
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.