How to Calculate API Costs
Learn how to calculate API cost using requests, input tokens, output tokens, fixed costs, monthly usage, and target margins.
Start with the billable unit
Every API cost calculation starts with the unit that actually drives cost. Traditional data APIs often charge by request. AI APIs frequently charge by token. Infrastructure APIs may charge by event, gigabyte, minute, image, or successful operation.
If you are building on top of another provider, use the provider's real unit first. You can hide that complexity from your customers later, but you should not hide it from your own margin model.
Separate variable and fixed costs
Variable costs rise with usage. Fixed costs exist even when usage is low. A practical API pricing model includes both, because a product can have healthy per-request margin and still lose money after support, servers, monitoring, and payment fees.
| Cost type | Examples | How to model it |
|---|---|---|
| Variable | Tokens, requests, storage writes | Multiply by usage |
| Fixed per request | Fraud checks, queue jobs | Add to request cost |
| Monthly fixed | Servers, observability, support | Spread across customers |
| Payment-related | Processor fee, refunds, chargebacks | Subtract from revenue |
Use a formula that matches your API
For token-based AI APIs, calculate input and output tokens separately. Output tokens are often more expensive because generation is computationally heavier than reading input context. If your product also calls multiple models, calculate each model line separately and sum the result.
Example calculation
Suppose your API handles 10,000 requests per day for 30 days. Each request averages 900 input tokens and 500 output tokens. Input costs $2.00 per million tokens and output costs $8.00 per million tokens. You also spend $150 per month on hosting and observability.
Monthly input token cost is 10,000 x 30 x 900 / 1,000,000 x $2.00, or $540. Monthly output token cost is 10,000 x 30 x 500 / 1,000,000 x $8.00, or $1,200. Add $150 fixed costs and the total monthly cost is $1,890.
Turn cost into price
A target margin tells you how much revenue you need above cost. If your cost is $1,890 and you want a 40% gross margin, revenue should be cost divided by 1 minus margin. That means $1,890 / 0.60 = $3,150. Charging less than that may still be acceptable for a launch promotion, but it should be an intentional decision.
- Keep a buffer for retries, prompt growth, abuse, and failed payments.
- Publish clear overage rules before users exceed a quota.
- Recalculate costs whenever model prices or average token lengths change.