A powerful Node.js library to calculate token counts and estimated costs for Large Language Models (LLMs).
- 🎯 Accurate token counting for various LLM models
- 💰 Real-time cost estimation using up-to-date pricing from OpenRouter API
- 🔄 Auto-fetches latest model prices from OpenRouter API
- 🔌 Works offline with built-in pricing data
- ⚡ Fast and efficient with caching support
npm install llm-cost-calculator
import { getEstimatedCost } from 'llm-cost-calculator';
// Calculate cost for a conversation with realtime prices
const result = await getEstimatedCost({
model: 'anthropic/claude-3.5-sonnet',
input: 'Write a function to calculate fibonacci numbers.',
output: `Here's a recursive function to calculate Fibonacci numbers:
function fibonacci(n: number): number {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}`
// options: {
// offline: true // Pass this option to use local pricing data
// }
});
console.log(result);
// Output:
// {
// inputTokens: 9,
// outputTokens: 42,
// cost: 0.000153 // Cost in USD
// }
Main function to calculate tokens and cost.
model
(string): Name of the LLM modelinput
(string, optional): Input/prompt textoutput
(string, optional): Output/completion textoptions
(object, optional):offline
(boolean): Run in offline mode using local pricing datatimeout
(number): API request timeout in milliseconds
{
inputTokens: number; // Number of tokens in input
outputTokens: number; // Number of tokens in output
cost: number; // Estimated cost in USD
}
Supports a wide range of models including:
openai/gpt-4
- GPT-4 base modelopenai/gpt-3.5-turbo
- Fast and cost-effectiveopenai/gpt-3.5-turbo-0125
- Latest GPT-3.5 with 16k context
anthropic/claude-3.5-sonnet
- Latest Claude 3.5 Sonnetanthropic/claude-3-sonnet
- Claude 3 Sonnet baseanthropic/claude-3.5-sonnet:beta
- Self-moderated version
google/gemini-2.0-flash-thinking-exp:free
- Experimental Gemini with thinking processgoogle/palm-2-chat-bison
- PaLM 2 for general chatgoogle/palm-2-codechat-bison
- PaLM 2 specialized for code
meta-llama/llama-2-13b-chat
- Llama 2 13B chat modelmistralai/codestral-2501
- Specialized for coding tasksmicrosoft/phi-4
- Efficient 14B parameter model
deepseek/deepseek-chat
- DeepSeek V3 for general chatqwen/qvq-72b-preview
- Visual reasoning specialistminimax/minimax-01
- Text and image understanding
Pricing data is automatically fetched from OpenRouter API, with fallback to local pricing data. The library caches pricing data to minimize API calls while ensuring you always have access to the latest rates.
Note: Model availability and pricing may vary. Check the OpenRouter API for the most up-to-date list and pricing information.
MIT
Contributions are welcome! Feel free to:
- Open issues for bugs or feature requests
- Submit pull requests
- Improve documentation
- Share your use cases
- Use offline mode when you don't need real-time pricing updates
- Cache results for repeated calculations
- Check token counts before making API calls to estimate costs