PortGate

Guides · AI · 4 min

How to Stop a Runaway LLM Bill Before It Happens

Retry loops, agent loops and abuse can turn a $20 month into a $2,000 night. Here is the maths, and five limits that stop it.

Most surprise AI bills are not caused by heavy normal use. They come from one of three things: a retry loop that never gives up, an agent that keeps calling itself, or someone outside using your endpoint as a free model. All three have the same shape: the same kind of request, sent fast, for hours, while you sleep.

The maths of one bad night

Take a mid-priced model at $2 per million input tokens and $10 per million output tokens. A request with 3,000 tokens of context and a 500-token answer costs:

  • input: 3,000 × $2 / 1,000,000 = $0.006
  • output: 500 × $10 / 1,000,000 = $0.005
  • total: about $0.011 per request

That is nothing once. Now a bug sends one request per second:

  • per hour: 3,600 × $0.011 = $39.60
  • over an 8-hour night: about $317
  • with five workers doing the same: about $1,580

Agent loops are worse, because each step usually sends the whole conversation again, so the input grows with every turn. Put your own model and traffic into the LLM cost calculator to see your numbers, including what a loop costs with and without a cap.

Limit 1: the provider's own budget

Start with the setting in your provider's console. Most providers let you set a monthly budget and alert emails per project or key. Read carefully whether the limit stops requests or only sends an email; some are notifications, and an email at 3 AM does not stop anything.

Use one key per app and per environment. A leaked staging key should not be able to spend your production budget.

Limit 2: cap the output of every call

Always set a maximum output length (max_tokens, max_output_tokens or the provider's equivalent). Without it, a model that gets stuck repeating itself can produce the longest answer it is allowed to on every call.

Limit 3: retries that give up

A retry policy should have a maximum number of attempts, growing delays between them, and should not retry errors that will never succeed:

import random, time

RETRYABLE = {429, 500, 502, 503, 504}

def call_with_retry(send, attempts=4):
    for attempt in range(attempts):
        response = send()
        if response.status_code not in RETRYABLE:
            return response
        time.sleep(min(30, 2 ** attempt) + random.random())
    return response

A 400 error caused by a bad request will fail the same way every time. Retrying it forever is the classic runaway bill.

Limit 4: per-user and per-feature limits

If users can trigger model calls, rate limit them per account and per IP, the same way you would rate limit a login form. Give each feature its own budget too: a summary button should not be able to spend what the main chat is allowed.

Limit 5: a hard daily cap in front of the key

The limits above live in your code, and a bug in your code is what causes the problem. The safest cap sits outside the app, between it and the provider, and answers 429 once the day's allowance is used.

You want three numbers:

  • requests per day, for loops of small calls
  • tokens per day, for a few huge calls
  • identical prompts per minute, which catches loops early, after a few repeats instead of after thousands

What to do when it fires

A cap turns a big bill into an outage, which is better but still an outage. Make sure it is loud: alert at 80% of the daily cap, so you look before users are blocked, and again at 100%. Then fix the cause, because raising the cap only moves the problem.

How PortGate helps

The AI & Edge Guard is a small proxy you run next to your app. You point your OpenAI, Anthropic or OpenRouter client at it by changing one base URL. It enforces a daily request cap, a daily token cap and a limit on identical prompts per minute (10 by default), answering 429 with a clear reason when a limit is hit. Streaming still works. You set the caps from the dashboard, and PortGate emails you at 80% and 100% of your monthly request allowance. Your prompts stay on your server; only counts are reported.