
LLM Cost Estimator is a small, zero-dependency JavaScript library that estimates token counts and API cost for OpenAI and Anthropic (Claude) models before you send a request. Use it to budget prompts, compare models, set spending guards and show users a cost preview, all without installing a heavy tokenizer. It ships with TypeScript types and a dated pricing table you can override at any time.

Install the LLM cost estimator
Install the package from npm. It works in Node.js 16 and later and has no runtime dependencies.
npm install @faisal-nadeem/llm-cost-estimatorQuick example: estimate the cost of a prompt
Pass a model name, your prompt (a string or an array of chat messages) and the maximum output you expect. The result includes token counts and the cost in US dollars.
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); // USDWhat the library does
- Estimates input tokens from text or chat messages (about 4 characters per token), or accepts exact token counts and your own tokenizer for precise results.
- Calculates input, cached-input and output cost for every request.
- Supports current Claude and OpenAI models, including long-prompt price tiers such as Claude Haiku 5.5 above 100,000 prompt tokens.
- Handles custom and fine-tuned models through a simple pricing override.
- Includes TypeScript definitions and a test suite.
Example prices included in the package
The table below shows a sample of the bundled standard-tier prices. They were checked against the official Anthropic and OpenAI pricing pages in November 2026.
| Model | Provider | Input | Output |
|---|---|---|---|
| claude-opus-5-5 | Anthropic | $4.00 | $20.00 |
| claude-sonnet-5-5 | Anthropic | $2.00 | $10.00 |
| claude-haiku-5-5 | Anthropic | $0.10 | $0.50 |
| gpt-6-astra | OpenAI | $10.00 | $50.00 |
| gpt-6.1-sol | OpenAI | $2.00 | $10.00 |
| gpt-6-luna | OpenAI | $0.10 | $0.50 |
Common use cases for an LLM cost estimator
Developers use a pre-flight cost estimate to keep AI features affordable. Typical examples include rejecting or trimming a prompt that would cost more than a set budget, choosing a cheaper model when a request is simple, showing customers an estimated price before a long document is processed, and forecasting monthly spend from sample traffic. Because the calculation happens locally in a few microseconds, you can run it on every request without adding latency or extra API calls.
It is also useful in tests and dashboards. Pair it with the token counts returned by your provider to compare the estimated and actual cost of each call, and tune your prompts based on what the numbers show.
Keeping LLM prices up to date
Providers change prices often, so the bundled table is a dated snapshot. You can use your own prices in four ways: pass a pricing object on each call, set it once with createEstimator, load your own JSON file with loadPricing, or update the package for a refreshed table. Your entries always win over the bundled ones, and pricingAgeDays() tells you how old the snapshot is. The README has copy-paste examples for each option.
Frequently asked questions
How accurate is the token estimate?
Without a tokenizer the library assumes about 4 characters per token, which is reasonable for English text and rougher for code or non-Latin scripts. For exact counts, pass your own token numbers or a tokenizer function. Treat results as a pre-flight estimate, not an invoice.
Does it call the OpenAI or Anthropic APIs?
No. It runs locally, makes no network requests and needs no API key.
Which prices does it cover?
Standard-tier text token prices. Batch discounts, priority tiers, data-residency uplifts, cache-write surcharges and non-text modalities are not included.
Is it free to use?
Yes. The package is open source under the MIT license.
Links
- LLM Cost Estimator on npm (opens in a new tab)
- LLM Cost Estimator source code on GitHub (opens in a new tab)
Need AI in your product?
I build SaaS platforms with RAG pipelines, AI agents and MCP integrations. Get in touch about AI development if you want help adding AI features to your product.