Why tracking your AI spend is already too late (and what to do instead)
Most teams hit this pattern eventually.
You add Stripe metered billing to your agent. You set a monthly cap. You feel good about it.
Then one customer sends a query that kicks off a recursive research loop. The agent runs for 40 minutes. By the time your cap triggers, you've already burned $80 of compute for a customer on a $10 plan.
Stripe didn't fail you. You asked it to track spend. It tracked spend. The problem is that tracking is a receipt. You needed a pre-authorization.
The actual fix: check before the run, not after.
from agentbill import meter
u/meter(
event="research_run",
customer_id_from="customer_id",
ceiling=5.00,
preflight=True
)
async def run_agent(customer_id: str, query: str) -> str:
return await your_agent(query)
If the customer is over budget, CeilingExceededError is raised before a single token is consumed. The function never runs. No charges. No surprise invoice.
The mental model shift:
Monthly caps answer: "Did this customer spend too much this month?"
Per-request ceilings answer: "Should I even start this run?"
Those are different questions. The second one is the one that saves you money.
What this looks like in practice:
- Customer A has 83 units left. Query comes in estimated at 5 units. Run starts.
- Customer B has 3 units left. Same query. Blocked before execution. Returns a clean error your frontend can handle.
- Customer C is on pay-as-you-go. No limit. Run starts. Event recorded after completion.
All three cases, one decorator.
What about outcome-based billing?
One more pattern worth knowing. If you're building something like a support agent, you probably don't want to charge for failed attempts.
@meter(
event="support_ticket",
customer_id_from="customer_id",
units=lambda result: 5 if result.get("resolved") else 0
)
async def handle_ticket(customer_id: str, ticket: dict) -> dict:
...
Charge 5 credits if the ticket got resolved. Charge 0 if it didn't. Your customers pay for results, not attempts.
Been building AgentBill to solve exactly this — preflight governance for AI agents. Happy to answer questions or talk architecture in the comments.
What billing patterns are you using right now for your agents?