DEV Community

Cover image for How to Stop AI Agents From Running Up Your LLM Bill
Rana Reebaal
Rana Reebaal

Posted on Originally published at Medium Fully Autonomous

How to Stop AI Agents From Running Up Your LLM Bill

Hard budgets per customer and per agent for OpenAI, Anthropic and Gemini, before the invoice arrives.

Here is a scenario every team running AI agents eventually lives through. An agent gets stuck in a retry loop on a Friday night. It calls the model again, and again, each time with a slightly longer context. Nobody notices until Monday, when the OpenAI or Anthropic dashboard shows a number that makes the whole team go quiet.

The worst part is not the money. It is that nobody can answer the obvious questions: Which agent did it? Which customer was it for? Why didn't anything stop it?

If you are searching for how to limit your OpenAI API spend, cap Claude API costs, or track LLM cost per customer, this post explains why the usual fixes fall short and what a real spend guardrail for AI agents looks like.

Why AI agent costs are so hard to control

A normal API bill grows with traffic. An agent's bill grows with the agent's own decisions, and that changes everything:

  • Loops and retries. One bad tool response can make an agent call the model dozens of times for a single task.
  • Context that keeps growing. Every step resends the whole conversation, so step 20 costs far more than step 1.
  • Several providers at once. Most teams mix OpenAI, Anthropic and Gemini, and each has its own dashboard, prices and usage format.
  • No customer in the picture. Provider dashboards show spend per API key, not per customer, per agent or per pricing tier. If you sell an AI product, you can't see your margin per customer.

The usual fixes, and where they break

Provider spending limits. OpenAI and Anthropic let you set limits for the whole organization. That is a circuit breaker for the entire company: when one runaway agent hits it, every product and every customer goes down together. It also can't tell your free tier from your enterprise customer.

One API key per customer. It works for three customers and becomes a nightmare at thirty. Keys multiply across providers, and you still have to stitch the bills together by hand.

Counting tokens in your own code. Most teams start here: wrap each call, read the usage, multiply by a price table, write it to a database. Then streaming responses break the counting, prices change, a new model arrives, and the check happens after the money is already spent. You have built a small billing system, and you are now maintaining it.

Observability tools. Tracing tools are great for seeing what happened. Most of them don't stop a call before it happens. A dashboard that tells you on Monday what went wrong on Friday is a report, not a guardrail.

What real spend control for AI agents needs

  1. Metering across every provider, streaming included, priced with current per-model rates.
  2. Attribution of every call to the customer, the agent and the trace that caused it.
  3. Budgets that are checked before the call, so a call that would break the budget is rejected instead of billed.
  4. One place to see it all, so cost per customer and per agent is a query, not a spreadsheet.

That list is exactly why I built Silvanexum.

Silvanexum: spend control for AI agents

Silvanexum meters every LLM call your agents make across OpenAI, Anthropic and Gemini, ties the cost to a customer, agent and trace, and enforces hard budgets. A call that would breach a block-mode budget is rejected with HTTP 402 before it runs, so the runaway loop stops at the limit you chose instead of at the end of the month.

Option 1: the gateway (zero code changes). Point your existing provider SDK at the Silvanexum gateway. Your calls still run on your own provider key, streaming works, and every call is metered and counted against your budgets:

import os
from anthropic import Anthropic
from silvanexum import Silvanexum

sx = Silvanexum()  # reads SILVANEXUM_API_KEY

claude = Anthropic(
    base_url=sx.gateway.anthropic_base_url(),
    api_key=os.environ["SILVANEXUM_API_KEY"],
    default_headers={
        "x-silvanexum-customer": "acme-corp",
        "x-silvanexum-agent": "support-bot",
    },
)
Enter fullscreen mode Exit fullscreen mode

The same works for OpenAI (sx.gateway.openai_base_url()) and Gemini (sx.gateway.google_base_url()).

Option 2: SDK-only mode (nothing leaves your servers). Keep calling the provider directly. Silvanexum only receives the usage numbers, prices them, and checks the budget first:

from silvanexum import Silvanexum, BudgetExceededError

sx = Silvanexum()

try:
    with sx.meter.track("openai", "gpt-5-mini",
                        customer="acme-corp", agent="support-bot",
                        check=True) as t:
        resp = openai_client.chat.completions.create(...)
        t.response(resp)
except BudgetExceededError:
    # Budget spent: skip the call, downgrade the model, or tell the user.
    ...
Enter fullscreen mode Exit fullscreen mode

Your prompts and your provider keys never touch Silvanexum. Only token counts do.

Then set the caps. Budgets can cover your whole organization, a project, an agent, or a single customer, daily or monthly, with an alert or a hard block:

sx.budgets.create(
    name="support-agents",
    scope_type="agent",
    scope_id="agt_123",
    period_type="monthly",
    limit_credits=50_000,
    enforcement="block",
)

# Who actually spent the money?
for row in sx.spending.summary(group_by="agent"):
    print(row.label, row.costCredits)
Enter fullscreen mode Exit fullscreen mode

What changes when you have it

  • A stuck agent hits its cap and stops. Nobody gets the Monday-morning surprise.
  • Your free tier can't eat the budget meant for paying customers.
  • You can answer "what does customer X cost us in AI?" in one query, which is what you need to price your product properly.
  • Switching between OpenAI, Claude and Gemini doesn't break your cost tracking.

Already using NVIDIA NeMo Relay or Claude Code?

Silvanexum also ships silvanexum-relay, a plugin for NVIDIA NeMo Relay that meters every call and can reject one before it runs when a budget is spent. There is also an MCP server (@silvanexum/mcp), so tools like Claude Code can check budgets and spending directly.

Try it

pip install silvanexum
# or
npm install @silvanexum/sdk
Enter fullscreen mode Exit fullscreen mode

Docs: docs.silvanexum.com ยท Sign up: silvanexum.com

If you run AI agents in production, I'd genuinely like to hear how you handle cost today: what broke, what you built, what you wish existed. Drop a comment below.


Disclosure: I'm the founder of Silvanexum. This post was drafted by an AI assistant (Claude) at my direction and approved by me. Originally published on Medium.

Top comments (0)