DEV Community

Lior Cohen
Lior Cohen

Posted on

Why I stopped using monthly caps and built preflight billing for my AI agent

Every time I ran a LangChain research agent, I was nervous.

Not because the agent was bad. It was good. Too good. One bad prompt, one infinite loop, one customer who accidentally triggered 50 parallel runs, and my monthly OpenAI cap would be toast. By the time the alert fired, the damage was done.

Monthly caps are reactive. They fire after the money is spent.

So I built something different.

The idea: block the run before it starts

Instead of capping after the fact, I wanted to check budget before any tokens are consumed. If the budget says no, the agent never runs. No compute. No cost.

I called it a preflight check, borrowing the term from aviation. Before a plane takes off, the pilot checks everything. Before the agent runs, you check the budget.

from agentbill import AgentBillClient

client = AgentBillClient(api_key="agb_your_key")

check = client.preflight(agent_id="research_agent", customer_id="user_123", estimated_units=10)
if not check.approved:
    raise Exception(f"Blocked: {check.reason}")

# agent runs here, budget is confirmed
result = run_agent(task)
client.record(agent_id="research_agent", units=10, customer_id="user_123")
Enter fullscreen mode Exit fullscreen mode

That's it. Two calls. One before, one after.


Per-customer enforcement

The real power is when you're building a product where your customers are paying for agent runs. Each customer gets their own budget counter. One customer exhausting their budget doesn't affect anyone else.

# Alice and Bob have separate budgets
check_alice = client.preflight(agent_id="research", estimated_units=10, customer_id="alice")
check_bob   = client.preflight(agent_id="research", estimated_units=10, customer_id="bob")
Enter fullscreen mode Exit fullscreen mode

The @gate decorator (zero boilerplate)

If you want even less code, the @gate decorator handles preflight and record automatically:

@client.gate(agent_id="research_agent", estimated_units=10, customer_id="user_123")
def run_research_agent(topic: str) -> str:
    # preflight runs before this
    # record runs after this
    return do_the_work(topic)
Enter fullscreen mode Exit fullscreen mode

What I learned

Monthly caps protect your wallet. Preflight checks protect your customers and your reputation.

When a customer hits their budget mid-month, the right behavior is: block gracefully, show them why, give them a path to upgrade. Not: let the run finish, send them a surprise bill, and explain it in a support ticket three days later.

The project is open source: github.com/marketinglior-pixel/agentbill

Python SDK: pip install agentbill-sdk

Works with LangChain, OpenAI, Claude, and any Python or Node.js agent. Also available as an MCP server on Smithery.

Top comments (0)