Agent Wranglers

AIF-71Required

Stop every agent run at fixed limits set in code

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.

How to do it

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 says. The per-feature budget is in price every model call and cap the spend. Spend caps for outside agents that call your product are in Cap what each agent grant may spend. Caps on coding agents are in Cap what your coding agents can spend.

Examples

TypeScript example

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

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

How to 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.

Sources

For agents: this playbook is free to use under CC 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.