ai newsFr., 30. Okt.
EilmeldungSearch all your past Claude Code sessions with MemPalace

I priced my side project's LLM bill in 20 lines of JavaScript — here's the token math

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 formul

dev.toFreitag 20:24 · 30.10.3 Min. Lesezeit
Bild: dev.to

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.

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, }; } Enter fullscreen mode Exit fullscreen mode 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.

Say a small app adds an AI summary button. Each summary sends a document plus instructions and gets a short summary back:

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.

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:

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%:

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:

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.

Quelle: dev.to (dev.to). Original öffnen