Skip to content
mentoor

Topic 3 of 5 · 4 min read

Cost abuse and unbounded consumption

Goal: Set token, request and agent-step limits that stop a denial-of-wallet attack without blocking normal use.

LLM10, Unbounded Consumption, is the risk that anyone, or any bug, can make your app spend without limit; the fix is a cap at every layer where cost is created. The OWASP entry includes denial of wallet, where an attacker drives up a pay-per-use bill, continuous input overflow, and model extraction through heavy API use.

Check yourself · 1 of 3

What is denial of wallet?

Choose one answer

Why it matters

Hosted models charge per token. A single request can carry a very large prompt and ask for a very long answer. An agent can loop, calling the model dozens of times for one user message. Without limits, a script, a bug or an attacker turns your product into an open tap.

Where to put limits

  1. Input size. Reject prompts above a set number of tokens before calling the model.
  2. Output size. Always send max_tokens (or the provider's equivalent).
  3. Per-user rate limits. Requests per minute and tokens per day, by account, not just by IP.
  4. Agent step limits. A maximum number of model calls and tool calls per task, and a wall-clock timeout.
  5. Budgets and alerts. A provider-side spending cap and an alert at, say, half the monthly budget.
  6. Anonymous access. Require sign-in, or use much lower limits, for free endpoints.

Pick numbers from real usage: look at the 99th percentile of normal requests, then set the cap a little above it.

Before and after

// Weak: no input check, no output cap, no loop guard
async function runAgent(task: string) {
  while (true) {
    const step = await llm.chat({ messages: history(task) });
    if (step.done) return step.text;
    await callTool(step.tool, step.args);
  }
}
// Better: bounded input, output, steps and time
const MAX_INPUT_TOKENS = 4_000;
const MAX_STEPS = 12;
const DEADLINE_MS = 60_000;

async function runAgent(task: string, userId: string) {
  if (countTokens(task) > MAX_INPUT_TOKENS) throw new LimitError("input_too_large");
  await rateLimiter.consume(userId, countTokens(task));
  const deadline = Date.now() + DEADLINE_MS;
  for (let i = 0; i < MAX_STEPS; i++) {
    if (Date.now() > deadline) throw new LimitError("timeout");
    const step = await llm.chat({ messages: history(task), max_tokens: 1_000 });
    if (step.done) return step.text;
    await callTool(step.tool, step.args);
  }
  throw new LimitError("step_limit");
}

When a limit is hit, return a specific error that says which limit and what to do, not a generic failure.

What goes wrong

  • Limits only at the gateway. The agent loop inside your service still spends without bound.
  • Caps set so tight that real users hit them. Measure first and watch the rejection rate.
  • No provider-side hard cap. If your code has a bug, only the account limit protects the bill.

Examples

Choosing limits for a support assistant

Logs show that 99 percent of questions are under 600 tokens and answers under 400. Set the input cap at 2,000 tokens, max_tokens at 800, ten requests per minute per signed-in user, and at most 8 agent steps. Add a budget alert at half the monthly spend. Normal users never notice; a script sending 50,000-token prompts is rejected at the first check.

Key points

  • Unbounded consumption covers denial of wallet, input overflow and extraction through heavy use.
  • Cap input tokens, output tokens, requests per user, agent steps and wall-clock time.
  • Set limits from measured normal usage so real users are not blocked.
  • A provider-side spending cap and alerts protect you when your own code has a bug.
  • Return specific limit errors that name the limit and the next step.

Common mistakes

  • Rate limiting by IP only, so one account behind many IPs still drains the budget.
  • Forgetting to cap the agent loop because the gateway already has a limit.
  • Choosing caps without looking at real usage data.

Check yourself · 2 of 3

Which control stops an agent from looping and calling the model hundreds of times for one request?

Choose one answer

Check yourself · 3 of 3

How should you choose a per-request input token cap?

Choose one answer

Practice items, not counted towards your mastery.

Start here