Stripe Billing Meters for AI SaaS Agents: How to report real-time LLM token usage & MCP tool credits without blocking FastAPI response latency?
Like most US AI SaaS teams, we are moving away from unlimited $20/month seats to Stripe Billing Meters (v1/billing/meter_events) to charge accurately for frontier LLM tokens and expensive MCP tool executions (e.g., headless browser runs).
The Latency & Double-Billing Problem
Right now, after our FastAPI agent finishes streaming tokens:
python# Naive inline Stripe call stripe.billing.MeterEvent.create( event_name="agent_tokens_consumed", payload={"stripe_customer_id": cust_id, "value": str(total_tokens)}, )
In production:
- Calling Stripe's REST API inline adds 250–400ms to request teardown, and if Stripe has a transient
502or rate limit, our SSE stream throws an error after the user already got their answer! - Retrying failed background jobs without deterministic event IDs causes duplicate billing for the same agent turn.
How do we aggregate high-frequency token usage in Redis and flush idempotent meter events to Stripe cleanly?