If you build features on top of LLM APIs, you have probably been surprised by a bill at least once. Nothing is broken; you simply had no idea what a request would cost until the invoice arrived.
So I built a small library that answers one question before you call the API: how much will this request cost?
It is called @faisal-nadeem/llm-cost-estimator (by Faisal Nadeem), it has zero dependencies, and it runs locally with no API key and no network calls.
The problem
LLM APIs charge per million tokens, with different rates for input, cached input and output. Prices differ by model and change often. If you want to cap spend per request, pick a cheaper model for simple prompts, or show a user a price preview, you need the number before you send anything.
Existing tokenizers are accurate but heavy, and they do not know prices. Price tables on websites do not plug into your code.
How it works
Install it:
npm install @faisal-nadeem/llm-cost-estimator
Then estimate a request:
const { estimateCost } = require('@faisal-nadeem/llm-cost-estimator');
const result = estimateCost({
model: 'claude-sonnet-5-5',
prompt: 'Summarize this document...',
maxOutput: 500
});
console.log(result.totalCost); // USD
The result includes input tokens, output tokens, input cost, output cost and the total in US dollars. You can pass chat messages instead of a string, say how many input tokens are cached, or give exact token counts from your provider instead of an estimate.
A budget guard in five lines
const MAX_COST = 0.05; // dollars per request
const { totalCost } = estimateCost({ model, prompt, maxOutput: 800 });
if (totalCost > MAX_COST) {
throw new Error(`Request would cost $${totalCost.toFixed(4)}, over budget`);
}
Prices change, so you stay in control
The package ships a dated price table, checked against the official Anthropic and OpenAI pricing pages. Because prices move, you never depend on me shipping a release. You can override them per call, set them once with createEstimator, or load your own JSON file:
const { createEstimator, loadPricing } = require('@faisal-nadeem/llm-cost-estimator');
const estimator = createEstimator({ pricing: loadPricing('./my-prices.json') });
estimator.estimateCost({ model: 'my-fine-tune', prompt, maxOutput: 200 });
It also handles long-prompt price tiers (for example Claude Haiku 5.5 above 100,000 prompt tokens), and pricingAgeDays() tells you how old the bundled table is so you can warn yourself when it goes stale.
What it does not do
Being honest about limits matters for a tool like this:
- Without a tokenizer it assumes about 4 characters per token. That is fine for English prose and rougher for code or non-Latin text. Pass your own tokenizer function or exact counts when precision matters.
- It covers standard-tier text token prices only. Batch discounts, data-residency uplifts, cache-write surcharges and non-text modalities are not included.
- It is a pre-flight estimate, not an invoice.
Try it and tell me what is missing
- npm: @faisal-nadeem/llm-cost-estimator
- GitHub: dev-avenger/llm-cost-estimator
- More details: faisalnadeem.net/llm-cost-estimator
I would like to know which models you want added, whether you want a built-in tokenizer option, and what would make this useful in your stack. Open an issue or leave a comment.
I'm Faisal, a full-stack and AI integration engineer. I build SaaS platforms with RAG pipelines, AI agents and MCP integrations. You can find me at faisalnadeem.net.
Top comments (0)