token-dollars
Calculate costs for AI model API usage based on token consumption and pricing.
Installation
npm i token-dollars
Usage
Import
import { Cost, Store } from 'token-dollars';
Calculate Cost
Calculate the total cost for a model based on usage statistics:
const usage = {
total_text_input_tokens: 1000000,
total_text_output_tokens: 500000,
total_cached_text: 200000
};
const result = Cost('gpt-4o', usage);
console.log(result);
Usage Object
The usage parameter accepts the following optional properties:
total_text_input_tokens - Total number of text input tokens
total_cached_text - Total number of cached text tokens
total_text_output_tokens - Total number of text output tokens
total_audio_input_tokens - Total number of audio input tokens
total_cached_audio - Total number of cached audio tokens
total_audio_output_tokens - Total number of audio output tokens
input_Transcript_Duration_whisper - Whisper transcription duration in seconds
Audio and Whisper Example
const usage = {
total_text_input_tokens: 500000,
total_audio_input_tokens: 1000000,
total_audio_output_tokens: 500000,
input_Transcript_Duration_whisper: 120
};
const result = Cost('gpt-realtime', usage);
console.log(result.total);
Supported Models
The package includes pricing for the following models:
OpenAI Models
gpt-5
gpt-5-mini
gpt-5-nano
gpt-5-chat-latest
gpt-5-pro
gpt-4.1
gpt-4.1-mini
gpt-4.1-nano
gpt-4o
gpt-4o-2024-05-13
gpt-4o-mini
gpt-realtime
gpt-realtime-mini
Google Models
gemini-2.5-flash-native-audio-latest
API
Cost(model, usage)
Calculates the total cost for a given model based on usage statistics.
Parameters:
model (string) - The model identifier (must exist in Store)
usage (object) - Usage statistics object containing token counts and transcription duration
Returns:
object - Cost calculation result
model (string) - The model identifier
total (number) - Total cost (rounded to 6 decimal places)
breakdown (object) - Detailed cost breakdown by component
textInputCost (number)
cachedTextInputCost (number)
textOutputCost (number)
audioInputCost (number)
cachedAudioInputCost (number)
audioOutputCost (number)
whisperCost (number)
Store
An object containing pricing information for all supported models. Each model entry includes:
pricing - Pricing per million tokens or per minute (for Whisper)
asp - API service provider (e.g., "openai", "google")
wssUrl - WebSocket URL (for realtime models)
Pricing Notes
- Token costs are calculated per million tokens
- Whisper transcription costs are calculated per minute
- Costs are rounded to 6 decimal places
- Missing usage properties default to 0
License
ISC