I priced my side project's LLM bill in 20 lines of JavaScript — here's the token math
Adding an AI summary feature to a side project cost me $156 a month at a volume I would have guessed was "basically free." The formula behind that bill fits in one function. The part I got wrong was not the formula — it was which tokens I was counting.
Every LLM API bills the same way: dollars per million tokens, with input and output priced separately. Output costs several times more than input, because generating text is the expensive operation and reading your prompt is cheap by comparison. That much is on every pricing page. What the pricing page does not show you is how fast the "cheap" side adds up when a fixed block of text rides along on every single request.
The billing function
function monthlyCost({ requests, inputTokens, outputTokens, inputPrice, outputPrice }) {
const inputMillions = (requests * inputTokens) / 1_000_000;
const outputMillions = (requests * outputTokens) / 1_000_000;
return {
input: inputMillions * inputPrice,
output: outputMillions * outputPrice,
total: inputMillions * inputPrice + outputMillions * outputPrice,
};
}
Two counters, two prices. The engineering work is not this function — it is getting honest numbers for inputTokens and outputTokens before you ship, instead of discovering them on the invoice. A rough token estimate for English prose is characters divided by 4; code and JSON tokenize denser, so measure with your provider's tokenizer on real payloads rather than trusting the heuristic for production estimates.
Worked example: 20,000 summaries a month
Say a small app adds an AI summary button. Each summary sends a document plus instructions and gets a short summary back:
- 20,000 requests a month
- 2,500 input tokens per request → 50,000,000 input tokens
- 350 output tokens per request → 7,000,000 output tokens
- Illustrative prices: $2.00 per million input tokens, $8.00 per million output tokens
Input cost: 50 × $2.00 = $100.00
Output cost: 7 × $8.00 = $56.00
Total: $156.00 a month — $0.0078 per summary
Less than a cent per summary sounds trivial until you multiply it: at 200,000 summaries the same feature is $1,560 a month, and at that point the line item has a manager. The per-unit cost is the number to design around, not the first month's total.
The figure that surprised me: the system prompt is 20% of the bill
Here is the breakdown I did not expect. Of those 2,500 input tokens per request, 800 are the system prompt — the fixed instructions, formatting rules, and few-shot examples prepended to every call. They never change, they are never the user's content, and they cost:
- 800 tokens × 20,000 requests = 16,000,000 tokens
- At $2.00 per million: $32.00 a month — 20.5% of the entire bill — for text I wrote once
That fixed block is the cheapest money in the whole system to save. Most providers now offer prompt caching: repeated prefix tokens are billed at a fraction of the full input price, commonly around 10%. At 10%:
- Cached system prompt: 16 × $0.20 = $3.20 instead of $32.00
- Saving: $28.80 a month
- New monthly total: $127.20 — an 18.5% cut from one configuration change, no model swap, no quality trade-off
The general rule: any token you send on every request deserves scrutiny proportional to your request count, not its size. A 2,000-token instruction block at 1,000 requests a month is 2 million tokens of pure overhead. The prompt you treat as free because you wrote it once is the most expensive text in your system per word.
Two other multipliers worth modeling before you ship:
- Regenerations. Every "try again" is a full new request at full price. If 15% of summaries get regenerated once, that adds 0.15 × $156.00 = $23.40 a month — a 15% surcharge for a button users barely notice.
- History growth. Chat-style features resend prior turns with each new message, so input tokens per request grow with conversation length. A feature that looks cheap in a one-turn demo can cost several times more in a real twenty-turn session. If your feature keeps history, model the long session, not the demo.
Honest caveats
- Prices move. Model prices drop, tiers change, and cached-input pricing differs by provider. Treat every dollar figure here as an illustrative snapshot, not a quote — plug in your provider's current prices.
- Token counts are estimates until measured. The characters-divided-by-4 heuristic is fine for a first pass; real payloads through the real tokenizer are the only honest input to a budget.
- The calculator estimates; the dashboard decides. Your provider's usage page is ground truth. Reconcile your estimate against it monthly, and investigate when they diverge — a divergence usually means a prompt grew or a retry loop appeared.
- Small volumes, small stakes. At $156 a month, none of this is a crisis. The point is that the structure scales: the same ratios at 100× volume are a budget line, and the fixes (caching, trimming fixed prompts, modeling regenerations) are cheapest to build in before volume arrives.
Run your own numbers
Plug in your request count, your real per-request token counts, and your model's current per-million prices:
AI API cost calculator — free, no signup, runs entirely in your browser.
Count the tokens you send on every request first. They are the ones that compound.
Top comments (0)