Claude API Stripe Billing Integration: A Practical Guide
Direct answer: how to bill for Claude API usage with Stripe
If you're building a product on top of Claude and need to charge customers for what they use, the integration pattern is: track tokens or requests per customer on your side, then report that usage to Stripe as metered billing events (or charge a flat per-seat fee if usage isn't the pricing axis). Claude's API itself has no billing layer for your end users — Anthropic bills you, and you're responsible for billing your customers, whether that's per-request, per-token, or per-seat.
There are two common models developers reach for:
- Usage-based billing — you meter tokens/requests and push usage events to Stripe's Billing Meters API, which calculates the invoice automatically at the end of the period.
- Seat/subscription billing — you charge a flat fee per user or per plan tier (Solo/Team/Scale style pricing) and use Claude usage internally just for rate-limiting or cost control, not for customer-facing billing.
Most SaaS products that wrap Claude use a hybrid: a subscription base fee plus metered overage. The rest of this guide walks through building that.
Step 1: capture usage data per customer
Every Claude API response includes token counts in the usage field of the message object — input_tokens and output_tokens. You need to persist this against the customer/account that made the call, not just log it globally.
{
"id": "msg_01...",
"usage": {
"input_tokens": 512,
"output_tokens": 128
}
}
If you're calling Claude directly, you have to build this attribution layer yourself: a wrapper around your API calls that tags each request with a customer ID and writes the usage to your database.
If you're using SubToAPI as the layer between your app and Claude, this part is mostly done for you — every response includes usage metadata tied to the application API key (sub_live_...) that made the call, so you can aggregate per-key usage without building your own token-counting pipeline. See /docs/messages for the response shape.
Step 2: decide your billing unit
Stripe's Billing Meters work on arbitrary numeric events, so you choose the unit:
- Tokens — most accurate, mirrors Anthropic's own pricing structure, but harder for customers to reason about.
- Requests — simpler to explain, but a 50-token request and a 50,000-token request cost you the same, so margins get messy.
- A blended "credit" unit — many SaaS products convert tokens into an internal credit (e.g., 1 credit = 1,000 tokens) and sell credits. This decouples your pricing from Anthropic's raw token cost, giving you room for markup and simpler customer-facing numbers.
Whatever you choose, compute it right after each API call so you're reporting near-real-time usage, not reconstructing it from logs later.
Step 3: report usage to Stripe
Create a Billing Meter in the Stripe dashboard (or via API), then send meter events as usage happens:
curl https://api.stripe.com/v1/billing/meter_events \
-u "$STRIPE_SECRET_KEY:" \
-d event_name="claude_tokens" \
-d "payload[stripe_customer_id]=cus_12345" \
-d "payload[value]=640" \
-d timestamp=1716400000
Batch this if you're high-volume — Stripe recommends aggregating events over short windows (e.g., every few minutes) rather than firing one event per API call, both for cost and for staying under rate limits.
async function reportUsage(customerId, totalTokens) {
await fetch("https://api.stripe.com/v1/billing/meter_events", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.STRIPE_SECRET_KEY}`,
"Content-Type": "application/x-www-form-urlencoded",
},
body: new URLSearchParams({
event_name: "claude_tokens",
"payload[stripe_customer_id]": customerId,
"payload[value]": String(totalTokens),
timestamp: String(Math.floor(Date.now() / 1000)),
}),
});
}
Stripe then rolls these events into an invoice line item based on the pricing you attach to the meter (per-unit, tiered, or graduated pricing all work).
Step 4: handle idempotency and retries
Two failure modes will bite you if you don't plan for them:
- Double reporting — if your usage reporter retries on a network timeout, you can send the same tokens twice. Give each meter event a deterministic idempotency key derived from your internal request ID, not a random UUID, so retries are safe.
- Delayed reporting — if Claude responses stream and you only know final token counts at the end, make sure you're reading the final usage block in the stream, not an intermediate delta. This matters more once you add streaming (see
/docs/streamingif you're proxying through SubToAPI, which normalizes streamed usage the same way as non-streamed).
Step 5: reconcile with webhooks
Set up a Stripe webhook listener for invoice.created and invoice.finalized so you can cross-check the invoiced usage against your own database before the charge goes out. This catches bugs — a meter event with a missing customer ID, a duplicate batch, a timezone mismatch in period boundaries — before a customer gets an incorrect bill.
app.post("/webhooks/stripe", (req, res) => {
const event = req.body;
if (event.type === "invoice.created") {
const invoice = event.data.object;
// compare invoice.lines against your internal usage ledger
}
res.sendStatus(200);
});
Where a billing-ready API layer helps
Building the metering pipeline above — token attribution, aggregation, idempotent reporting — is straightforward but it's also exactly the kind of plumbing that's easy to get subtly wrong under load. If you'd rather not own that layer, routing Claude access through SubToAPI gives you per-key usage metadata out of the box, so your Stripe reporting job just reads usage totals per sub_live_... key instead of instrumenting every call site yourself. Team and Scale plans also add multi-seat API key management, which maps cleanly onto Stripe's per-seat subscription items if you're billing by user rather than by usage. Check /pricing for plan details or start with a free trial at /signup.
Common pitfalls to avoid
- Billing on estimated tokens instead of actual usage — always use the
usagefield from the API response, not a pre-call estimate from a tokenizer library, which can drift from Anthropic's actual counting. - Mixing input and output token pricing — output tokens typically cost more than input tokens; if your Stripe meter uses a single blended rate, make sure your margin accounts for the ratio your customers actually use.
- Not handling failed Claude requests — a request that errors out before generating output shouldn't be billed as full usage, but partial input tokens may still apply depending on where the failure occurred.
FAQs
Does Claude's API support usage-based billing natively? No. Claude's API returns token usage per response, but billing your own customers is entirely your responsibility — you build the metering and invoicing layer, typically with Stripe.
What's the easiest way to meter tokens per customer? Attribute every API call to a customer ID at the point of the request, sum input_tokens and output_tokens from each response, and batch-report totals to Stripe's Billing Meters API on a short interval.
Should I bill by tokens or by a flat subscription? It depends on usage variance. If customer usage is roughly uniform, flat per-seat pricing (like Solo/Team/Scale tiers) is simpler to sell. If usage varies widely, metered billing protects your margins better.