ospalabs
Guide · Agents

How to stop a runaway AI agent loop before it bills you

Updated October 2026

Agent loops fail quietly. A tool call that keeps erroring, a planner that never reaches a stop condition, or a retry with no backoff can send a request every second for hours. Nobody notices until the invoice.

The fix is a budget the agent cannot exceed, enforced outside the agent's own code. Fuse refuses the request that would cross the budget and answers 402, which your agent can treat as a stop signal.

  1. 01

    Give each agent its own Fuse key

    Create a key per agent or per repo. Each key can have its own daily or monthly cap inside the project ceiling, so one loop cannot spend another agent's budget.

  2. 02

    Set a tight per-key cap

    Start small, for example $5 a day for an experiment. When the cap would be crossed, that key gets 402 with scope key; the other keys keep working.

  3. 03

    Treat 402 as stop, not retry

    Catch the status in the agent's model call and end the run. Retrying a 402 only produces more 402s.

    agent.ts
    try {
      return await client.chat.completions.create(req);
    } catch (err) {
      if (err instanceof OpenAI.APIError && err.status === 402) {
        throw new AgentBudgetExceeded(); // stop the run
      }
      throw err;
    }
  4. 04

    Get warned before the stop

    Anomaly alerts compare the last hour with the project's usual hour and email you when it is several times higher, often well before any ceiling is close.

Questions

Will Fuse cut off a request halfway through?
No. The decision is made before the request is sent, using its worst-case cost. A request that starts is allowed to finish.
How fast does a revoked key stop working?
Within about 15 seconds at every proxy location.