token-dollars
v1.0.2
Published
Calculate costs for AI model API usage based on token consumption and pricing.
Downloads
21
Readme
token-dollars
Calculate costs for AI model API usage based on token consumption and pricing.
Installation
npm i token-dollarsUsage
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);
// {
// model: 'gpt-4o',
// total: 3.75,
// breakdown: {
// textInputCost: 2.5,
// cachedTextInputCost: 0.25,
// textOutputCost: 5,
// audioInputCost: 0,
// cachedAudioInputCost: 0,
// audioOutputCost: 0,
// whisperCost: 0
// }
// }Usage Object
The usage parameter accepts the following optional properties:
total_text_input_tokens- Total number of text input tokenstotal_cached_text- Total number of cached text tokenstotal_text_output_tokens- Total number of text output tokenstotal_audio_input_tokens- Total number of audio input tokenstotal_cached_audio- Total number of cached audio tokenstotal_audio_output_tokens- Total number of audio output tokensinput_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 // 2 minutes
};
const result = Cost('gpt-realtime', usage);
console.log(result.total); // Total cost including audio and whisperSupported Models
The package includes pricing for the following models:
OpenAI Models
gpt-5gpt-5-minigpt-5-nanogpt-5-chat-latestgpt-5-progpt-4.1gpt-4.1-minigpt-4.1-nanogpt-4ogpt-4o-2024-05-13gpt-4o-minigpt-realtimegpt-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 resultmodel(string) - The model identifiertotal(number) - Total cost (rounded to 6 decimal places)breakdown(object) - Detailed cost breakdown by componenttextInputCost(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
