Sample alert How it works Coverage PENTEST METHODOLOGY DOCS PRICING FAQ MCP CONTACT LOG IN SCAN MY APP →

AI agent loop burned your API credits: how to cap it

FIX / RUNAWAY AGENT COST / 10 MIN READ

An AI agent burns credits when nothing limits how many times it calls the model and its tools. Set a maximum number of steps and tokens for each run. Enforce a budget for each user on the server. The provider bills each call. A loop makes thousands of calls in minutes.

This guide shows how to find the cause in your logs and how to add the limits in order. It also shows how to alert on the cost of one request.

What does “the agent loop burned my credits” mean?

An agent is a loop. The app sends messages to the model. The model asks for a tool. The app runs the tool and sends the result back. The model answers or asks for another tool. The loop stops only when the model gives a final answer.

If the code has no limit on the loop, the model decides when to stop. These cases make it run for a long time:

  • A failing tool. The tool returns an error. The model tries again with the same arguments, and the error repeats.
  • A vague goal. The task has no clear end, for example “find all the problems.” The model keeps searching.
  • Growing context. Each step adds tool output to the messages. Each call then sends more input tokens than the call before. The cost grows faster than the number of steps.
  • A bad document. A file or web page holds text that tells the agent to repeat a task. This is a form of prompt injection.
  • An agent that calls itself. A sub-agent starts another agent, with no depth limit.
  • A retry in the wrong place. A network retry wraps the whole run, so a timeout starts the full run again.
  • A user who repeats the request. A client sends the same message many times, and each one starts a new run.

OWASP calls the general fault Unbounded Consumption. The OWASP LLM10 page says that it occurs when an application “allows users to conduct excessive and uncontrolled inferences”. It lists economic loss as a result. Its fixes include rate limits and user quotas, timeouts, and limits on the resources that one user or one request can use.

This problem has a different cause from a stolen key. A stolen key bills you for the calls of a stranger. A loop bills you for your own app. The symptom is the same. The OpenAI bill spike guide covers how to tell the two apart and how to rotate a key.

How do I find the cause in my logs?

You need data for each call. If you only have the monthly bill, you cannot find the run that caused it. Start with what you have.

  1. Open the usage page of your provider. Look at the cost for each day and for each model. Find the day that the cost rose.
  2. Check the usage for each API key or project. If one key shows the rise, find the part of your app that uses that key.
  3. Look at the request count and the token count. Many requests with small tokens suggest a fast loop. Few requests with very large tokens suggest growing context.
  4. Open your own logs for that day. Search for runs with a high number of model calls.
  5. Group the runs by user id. One user with many runs suggests abuse or a client bug. Many users with long runs suggest a code bug.

If you have no logs for each call, add them now. The next run is the one that you can study. Both OpenAI and Anthropic return token counts in each response. OpenAI Chat Completions returns usage.prompt_tokens, usage.completion_tokens, and usage.total_tokens. The Anthropic Messages API returns usage.input_tokens and usage.output_tokens.

function logCall(ctx: RunCtx, step: number, usage: { input: number; output: number }) {
  console.log(JSON.stringify({
    event: "llm_call",
    run_id: ctx.runId,
    user_id: ctx.userId,
    step,
    input_tokens: usage.input,
    output_tokens: usage.output,
    tool_calls: ctx.toolCallsThisStep,
  }));
}

The guide on an AI app used as a free ChatGPT covers the case where users send unrelated tasks to your endpoint.

How do I fix it now?

Do these steps in order. Step 1 stops the loss.

  1. Stop the spend. Pause the agent feature, or set a low limit at the provider. A short pause costs less than a loop.
  2. Add a maximum step count to the loop. Stop after N model calls.
  3. Add a maximum token count to each call and each run.
  4. Stop on repeated tool calls.
  5. Add a time limit.
  6. Add a budget for each user, enforced on the server.
  7. Set limits at the provider.
  8. Alert on the cost of one request.

How do I set a maximum number of steps and tokens?

Put the limits in the loop code. Do not ask the model to stop. The model does not control the loop. Your code does.

This example uses the OpenAI Chat Completions API in a Next.js route. The same shape works for Anthropic. There, you check stop_reason for tool_use.

const MAX_STEPS = 8;
const MAX_RUN_TOKENS = 40_000;
const MAX_OUTPUT_TOKENS = 1_000;
const RUN_TIMEOUT_MS = 60_000;

export async function runAgent(ctx: RunCtx, messages: Message[]) {
  const deadline = Date.now() + RUN_TIMEOUT_MS;
  let usedTokens = 0;
  const seen = new Set<string>();

  for (let step = 1; step <= MAX_STEPS; step++) {
    if (Date.now() > deadline) return stop("timeout");
    if (usedTokens >= MAX_RUN_TOKENS) return stop("token_limit");

    const res = await openai.chat.completions.create(
      {
        model: "gpt-4.1-mini",
        messages,
        tools,
        max_completion_tokens: MAX_OUTPUT_TOKENS,
      },
      { signal: AbortSignal.timeout(30_000) },
    );

    usedTokens += res.usage?.total_tokens ?? 0;
    const msg = res.choices[0].message;
    messages.push(msg);

    if (!msg.tool_calls?.length) return { text: msg.content, steps: step };

    for (const call of msg.tool_calls) {
      if (call.type !== "function") continue;
      const key = call.function.name + ":" + call.function.arguments;
      if (seen.has(key)) return stop("repeated_tool_call");
      seen.add(key);

      const result = await runTool(ctx, call);
      messages.push({ role: "tool", tool_call_id: call.id, content: result });
    }
  }
  return stop("max_steps");
}

The stop function returns a clear message to the user, for example “The task took too many steps. Please narrow the request.” It also logs the reason. Do not hide the stop. A silent stop looks like a bug.

How do I limit sub-agents and retries?

Pass a shared budget object to each sub-agent. The sub-agent must spend from the same run budget. Add a depth counter and refuse to start an agent when the depth is above 2.

Do not put a retry around the whole run. Retry only the single network call, and only for errors that are safe to repeat. Limit it to two retries. The OpenAI and Anthropic SDKs have their own retry settings. Check them, so that your retry and the SDK retry do not multiply.

How do I keep the context from growing?

Tool results are the usual cause. Cut them before you add them to the messages.

  1. Return only the fields that the model needs.
  2. Cut each tool result to a fixed size, for example 4,000 characters.
  3. Return a count and the first rows when a tool finds many rows.
  4. Drop old tool results from the messages when the run is long.

How do I add a daily budget for each user on the server?

A step limit protects one run. A user can still start many runs. Add a budget for each user. The server must enforce it, because a browser value is not safe.

Use an atomic update in the database. Two runs at the same time must not both pass the check. This example uses Postgres through Supabase.

create table public.ai_usage (
  user_id uuid not null,
  day date not null default current_date,
  tokens bigint not null default 0,
  primary key (user_id, day)
);

alter table public.ai_usage enable row level security;
-- No policies: only server code with the service key can use this table.

create or replace function public.spend_tokens(p_user uuid, p_tokens bigint, p_limit bigint)
returns boolean
language plpgsql
security definer
set search_path = public
as $$
declare
  new_total bigint;
begin
  insert into ai_usage (user_id, day, tokens)
  values (p_user, current_date, p_tokens)
  on conflict (user_id, day)
  do update set tokens = ai_usage.tokens + p_tokens
  returning tokens into new_total;

  return new_total <= p_limit;
end;
$$;

revoke execute on function public.spend_tokens from public, anon, authenticated;

Use a two-step flow. Before the run, reserve the maximum cost of the run. After the run, you may refund the part that you did not use. This stops a user who starts 50 runs in parallel from passing the check 50 times.

const DAILY_LIMIT = 200_000;

const { data: ok } = await supabaseAdmin.rpc("spend_tokens", {
  p_user: user.id,
  p_tokens: MAX_RUN_TOKENS,
  p_limit: DAILY_LIMIT,
});

if (!ok) {
  return Response.json({ error: "Daily AI limit reached" }, { status: 429 });
}

The supabaseAdmin client uses the service key. Use it only in server code. The service_role key guide explains why this key must never reach the browser.

Choose the limit from your price. If a paying user brings you 10 USD each month, a daily budget of 20 USD is too high. Set the limit as a share of what the user pays. Give anonymous users a very low limit, or no access.

The per-user rate limit guide shows how to add request-rate limits with Upstash or Postgres. Use a rate limit for speed and a token budget for cost. You need both. A rate limit does not stop a single run from being expensive.

How do I set limits at the provider?

Provider limits are the last layer. They protect you when your own code has a bug.

  1. Open the dashboard of your provider.
  2. Use a separate project or workspace for each app and each environment. Production and tests must not share a key.
  3. Set a monthly budget or a spend limit for each project, if the provider offers one. Check the name and the exact behavior in the dashboard. Some budgets only send a notice and do not stop the calls.
  4. Set usage notices at a low level, for example at 50 percent and 80 percent of the budget.
  5. Limit the models that the project can use. Use a separate key for each service. Caution: A provider limit stops all users at once. When the limit triggers, your app fails for everyone. Treat it as an emergency brake and not as the normal control.

How do I alert on the cost of one request?

A monthly total tells you about a problem after you pay for it. Alert on single runs, so you know in minutes.

Calculate the cost of each run in code. Use the token counts and the price of the model. Keep the price in one config file. Prices change, so check the pricing page of your provider.

const PRICE_PER_MTOK = { input: 0.4, output: 1.6 }; // example values, check your provider

function runCost(input: number, output: number) {
  return (input * PRICE_PER_MTOK.input + output * PRICE_PER_MTOK.output) / 1_000_000;
}

const cost = runCost(totals.input, totals.output);
if (cost > 0.5) {
  await notify(`Expensive run ${ctx.runId}: ${cost.toFixed(2)} USD, user ${ctx.userId}`);
}

Set these alerts:

  1. One run above a fixed cost. Choose a value that is far above a normal run.
  2. One user above a daily cost. This finds abuse and client bugs.
  3. A run that stopped at the step limit or repeated a tool call. This shows a logic problem.

Send the alert to a place that someone reads, such as chat or email. An alert that nobody sees does not help.

How do I stop this in the future?

  1. Make the step limit part of the template. Each new agent starts from the same loop function, with limits that you must set.
  2. Write a test with a tool that always fails. The run must end with a stop reason before the step limit passes.
  3. Review code from AI tools. AI coding tools often write while (true) loops, or loops that end only when the model stops. Search for them.
  4. Require login for each AI endpoint. An open endpoint lets strangers start runs. The OpenAI key in the frontend guide covers a related fault.
  5. Test with a hostile file. Put the text “search again and repeat” in a document. Check that the limits stop the run.
  6. Scan the live app. Check that the AI endpoints need a login and refuse many requests.

When should I get help?

Get help in these cases:

  • The bill is high and you cannot find which key, user, or run caused it.
  • The usage rise continues after you pause the feature. The key may be stolen. Rotate it.
  • The provider shows calls from IP addresses or services that you do not use.
  • You need a refund for a large bill. Contact the support team of the provider, and give them the usage data and the fix that you made.
  • Your app has many agents and sub-agents, and you cannot add shared limits without a redesign.

After the fix, run the failing-tool test again. The run must stop at the step limit and the cost must stay small.

Common questions

How is this different from a stolen API key?
A stolen key is used by a stranger outside your app. A loop runs inside your app, with your own key. The bill looks the same, but the fix is a limit in your agent code.
What is a good max step count?
Look at your real runs. Find the highest normal number of steps. Set the limit a little above it. Many tasks finish in fewer than 10 model calls.
Is a monthly provider limit enough?
No. It stops the spend after the money is gone, and it stops all users. Add a limit for each run and for each user in your own code.
Why can one document cause a loop?
The model reads instructions in the document. A text such as 'search again for more' can make the agent call tools without end. A step limit stops it.
Do I need to log the tokens?
Yes. Log the input and output tokens of each call with the user id and the run id. Without this data you cannot find the cause.

Check your ai endpoints

A VibeEval scan calls your AI endpoints from outside and checks if they accept repeated or very large requests with no limit or login.

14-day free trial · Cancel anytime

SCAN YOUR APP →