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 formula — it was which tokens I was counting. Every LLM API bills the same way: dollars per million…
A side project's Large Language Model (LLM) billing was priced in just 20 lines of JavaScript, revealing the true cost behind what seemed like a free AI summary feature. The cost calculation formula for the billing is simple and fits in one function. The main oversight was not the formula itself, but rather the tokens being counted.
Every LLM API charges based on dollars per million tokens, with input and output priced separately. Output costs significantly more than input due to the complexity of generating text versus simply reading a prompt. While pricing pages outline these rates, they do not disclose how quickly costs can accumulate when a constant block of text accompanies every request.
The JavaScript function, `monthlyCost`, takes in four parameters: requests, inputTokens, outputTokens, inputPrice, and outputPrice. It calculates the input and output costs in millions of tokens and returns the total monthly cost. To estimate token usage, a rough heuristic for English prose is to divide characters by four, but using the provider's tokenizer with real payloads is recommended for production estimates.
For instance, if a small app adds an AI summary button that generates 20,000 summaries per month, with each summary consuming 2,500 input tokens and 350 output tokens, and the prices are $2.00 per million input tokens and $8.00 per million output tokens, the input cost comes to $100.00, while the output cost amounts to $56.00, totaling $156.00 monthly.
This amounts to approximately $0.0078 per summary, which appears negligible at first glance but becomes significant as volume increases. At 200,000 summaries, the same feature costs $1,560 monthly, with the manager-worthy line item representing a mere fraction of the total.
Interestingly, the system prompt comprises 20% of the bill. Out of the 2,500 input tokens per request, 800 are the system prompt, which never changes and costs $32.00 monthly at $2.00 per million tokens, representing 20.5% of the entire bill. This fixed block, written once, is the most expensive text per word in the system. Providers often offer prompt caching, reducing the cost of repeated prefix tokens by around 10%.
Implementing this can save $28.80 monthly, lowering the total to $127.20 and yielding an 18.5% reduction. The lesson here is that any token sent on every request should be scrutinized proportionally to the request count rather than its size.
Additional multipliers to consider before launching include regenerations and history growth. Each retry generates a full new request at full price, and chat-style features that resend prior turns with each new message can significantly increase input tokens per request. To avoid unexpected costs, it's crucial to model the long session rather than a one-turn demo.
Keep in mind that these dollar figures are illustrative snapshots and should be replaced with the current prices from your provider. Lastly, it's essential to reconcile your estimates with your provider's usage page monthly, as discrepancies usually indicate a growing prompt or increased retries.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.