← Blog

Claude API Usage-Based Billing Setup Guide

2026-11-19 · 5 min read · SubToAPI Team

Setting up usage-based billing for a product built on the Claude API means solving two separate problems: metering how many tokens each customer consumes, and turning that metering data into invoices or metered charges in a payment system like Stripe. Anthropic's own billing covers what you pay Anthropic — it does not give you a built-in way to charge your own users per request or per token. That gap is what this guide walks through.

If you're building a SaaS, internal tool, or agency product on top of Claude, "usage-based billing" almost always means: track tokens per customer → convert tokens to a dollar cost → push that cost into your billing system on a schedule (daily, weekly, or at invoice time). Below is a practical setup you can implement today.

Step 1: Decide what you're actually billing for

Before writing any code, pick your billing unit. Common options:

Most production SaaS products land on credits or tiers with overage, because raw token pricing changes model-to-model and customers don't want to think about input vs. output token rates.

Step 2: Capture usage data from every API call

Every Claude API response includes usage metadata — input tokens, output tokens, and cache-related token counts if you use prompt caching. You need to log this on every call, tied to a customer ID, not just aggregate it.

async function callClaude(customerId, messages) {
  const res = await fetch("https://api.anthropic.com/v1/messages", {
    method: "POST",
    headers: {
      "x-api-key": process.env.ANTHROPIC_API_KEY,
      "anthropic-version": "2023-06-01",
      "content-type": "application/json",
    },
    body: JSON.stringify({
      model: "claude-sonnet-4-5",
      max_tokens: 1024,
      messages,
    }),
  });

  const data = await res.json();

  await logUsage({
    customerId,
    inputTokens: data.usage.input_tokens,
    outputTokens: data.usage.output_tokens,
    model: data.model,
    timestamp: new Date(),
  });

  return data;
}

The logUsage call should write to a dedicated usage table, not just increment a counter — you want granular records so you can debug disputes and recompute costs if your pricing changes.

Step 3: Convert raw usage into cost

Once usage is logged, apply your pricing formula. Store the model's per-token rate in a config table (not hardcoded), since Anthropic updates pricing by model and you'll want historical accuracy for past invoices.

function calculateCost(usageRow, rateCard) {
  const rate = rateCard[usageRow.model];
  const inputCost = (usageRow.inputTokens / 1_000_000) * rate.inputPer1M;
  const outputCost = (usageRow.outputTokens / 1_000_000) * rate.outputPer1M;
  return inputCost + outputCost;
}

Run this as a batch job (hourly or daily) rather than computing cost synchronously on every request — it keeps your request path fast and gives you a clean audit trail if you need to reprocess.

Step 4: Push usage into your billing system

If you use Stripe, the standard pattern is Stripe's metered billing via usage records on a subscription item:

await stripe.subscriptionItems.createUsageRecord(
  subscriptionItemId,
  {
    quantity: Math.round(dailyUsage.totalCost * 100), // in cents
    timestamp: Math.floor(Date.now() / 1000),
    action: "increment",
  }
);

Aggregate daily rather than per-request to avoid hammering Stripe's API, and keep a reconciliation report so you can catch mismatches between your internal ledger and what Stripe actually billed.

Step 5: Add guardrails before you need them

Usage-based billing without limits is a support ticket waiting to happen. At minimum, build:

This is the part most teams underbuild. Tracking tokens is easy; building the dashboards, alerts, and caps around it is the actual work.

Where SubToAPI fits in

If you'd rather not build the metering, key-issuing, and usage-reporting layer yourself, SubToAPI wraps your existing Claude access in an HTTPS API with application API keys (sub_live_...), streaming, tool use, and usage metadata built in. You create per-app or per-customer keys from a dashboard, and every response carries usage data you can feed straight into the cost-calculation step above — no need to self-host a proxy just to get clean token counts per key.

It's not a replacement for your billing system (you still connect the usage data to Stripe or your invoicing tool), but it removes the "capture usage reliably per customer" problem, which is usually the most fragile part of a homegrown setup. Plans start at Solo for €9, with Team (€19/seat) and Scale (€49/seat) tiers for multi-key, multi-seat setups — see /pricing for details, or jump into the quickstart to see how key creation and usage reporting work.

Checklist before you launch

FAQ

Does Anthropic offer usage-based billing for my customers directly? No. Anthropic bills you for your own API usage, but it has no concept of your downstream customers. You need to meter usage yourself and connect it to your own billing system, or use a layer like SubToAPI that exposes per-key usage metadata you can bill against.

What's the easiest way to track tokens per customer? Log usage.input_tokens and usage.output_tokens from every API response, tagged with a customer or key ID, into a dedicated table. Aggregate into cost on a schedule rather than in real time — see /docs/messages for response structure if you're using SubToAPI's API.

Should I bill per token or use a credit system? Credits are usually better for end users because token costs vary by model and input/output ratio, which confuses customers. Convert tokens to credits internally using your own margin, and bill credits in round, easy-to-understand numbers.

Turn your Claude access into an HTTPS API

SubToAPI gives you application API keys, streaming, tool use and usage insights on top of your existing Claude access — set up in minutes.

Start free  Read the quickstart →