How to calculate SaaS gross margin (with a worked example)
The SaaS gross margin formula, what belongs in COGS, a worked example with AI and cloud costs, and the mistakes that make margin look better than it is.
October 3, 2026 · 2 min read
Gross margin is the share of each revenue dollar left after paying for what it takes to deliver your product. It is the clearest single signal of whether growth will make you richer or just busier.
The formula
Both numbers must cover the same period — usually a calendar month or the last 30 days.
What counts as revenue
Use money actually earned in the period: subscription charges and one-off charges, minus refunds and disputes, excluding sales tax and VAT (you collect it for the government). If you bill annually, either spread the annual charge across months or compare like with like over a full year.
What counts as COGS
COGS is what it costs to serve the customers you have. For a typical SaaS:
- Hosting and infrastructure — compute, databases, storage, bandwidth.
- AI and third-party APIs used by the product — LLM inference, embeddings, OCR, maps.
- Payment processing fees — Stripe's percentage and fixed fees.
- In-product services — transactional email, monitoring of production, auth providers.
- Customer support for existing customers (often included by mature companies; many early-stage founders leave it out — be consistent).
Not COGS: sales and marketing, R&D salaries, staging environments, internal tools. See what counts as COGS for SaaS.
Worked example
An AI writing tool in one month:
| Line | Amount |
|---|---|
| Revenue (net of refunds and tax) | $10,000 |
| OpenAI + Anthropic | $1,400 |
| AWS + Vercel | $800 |
| Stripe fees | $330 |
| Email, monitoring | $120 |
| COGS | $2,650 |
Gross profit = $10,000 − $2,650 = $7,350. Gross margin = $7,350 ÷ $10,000 = 73.5%.
- AI 14¢
- Infrastructure 8¢
- Payment fees 3¢
- Other 1¢
Read it as cents: of every dollar, 14¢ goes to AI, 8¢ to infrastructure, 3¢ to payment fees, 1¢ to other services — you keep about 73¢.
Common mistakes
- Mixing MRR with costs. MRR is a run-rate, not revenue earned.
- Leaving out payment fees. At 3% they are often your third-largest cost.
- Counting credits as savings. Cloud credits expire; your real margin is what it will be without them.
- Partial days. Cost providers report with a delay. Comparing a complete month of revenue with 28 days of costs flatters margin.
- Ignoring the trend. A margin that slips half a point a week is a bigger problem than a stable one that's a little low.
Track it automatically
Calculating this once in a spreadsheet is easy. Keeping it current as prompts, models and traffic change is not. Try the gross margin calculator, or let MarginMeter compute it daily from Stripe, AWS, OpenAI and Anthropic.
Frequently asked questions
What is the formula for SaaS gross margin?
Gross margin % = (Revenue − Cost of goods sold) ÷ Revenue × 100. For SaaS, COGS is the cost of delivering the product: hosting, AI APIs, payment fees, third-party services in the product, and customer support that serves existing customers.
Should I use MRR or revenue?
Use revenue for the same period as your costs. MRR is a run-rate; it doesn't include refunds, one-off charges or timing differences, so mixing it with actual monthly costs distorts the margin.
What is a good gross margin for SaaS?
Traditional SaaS is commonly described as running at 70–80% or more. AI-heavy products often run lower because inference is a variable cost. The trend matters as much as the level.
Related
- What counts as COGS for a SaaS company?A practical list of what belongs in SaaS cost of goods sold — hosting, AI APIs, payment fees, support — what doesn't, and how to treat staging, credits and tools.
- AI SaaS gross margins: what to expect and how to improve themWhy AI products often run lower gross margins than classic SaaS, which costs drive the gap, and practical levers — model mix, caching, pricing — to improve margin.
- How to price an AI SaaS product without losing moneyPrice AI features from cost per user and target margin: estimate inference cost, add payment fees, choose a margin, and protect yourself from heavy users.
- How to calculate LLM cost per userCalculate LLM API cost per request and per user from tokens, model prices, caching and usage — with a worked example and the mistakes that underestimate cost.
See your real margin in five minutes.
Connect Stripe and your cost providers with read-only access. Free for two connections — no card required.