npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tokencost

v0.1.11

Published

Client side token counting & price estimation for LLMs

Downloads

48

Readme

TokenCost for Nodejs

TokenCost helps calculate the USD cost of using major Large Language Model (LLMs) APIs by calculating the estimated cost of prompts and completions.

API forked from TokenCost for Python with code adapted for Nodejs apps. Originally written for StartKit.

Improvements

We've added some improvements over the python version:

  • Added support for calculating cost of sending Images with Chat Prompts
  • Added Image generation support
  • Added Audio/Whisper support

LLM Costs

Prices are forked from from LiteLLM's cost dictionary.

Installation

npm i tokencost

Usage

You can calculate the cost of prompts and completions from OpenAI requests:

import { calculatePromptCost, calculateCompletionCost } from "tokencost";

const messages = [
  { role: "system", content: "You are a helpful assistant." },
  {
    role: "user",
    content: "What is the prime directive?\n",
  },
];
const promptCost = calculatePromptCost(messages, "gpt-4o");

const completion = await openai.chat.completions.create({
  messages,
  model: "gpt-4o",
});

const { content } = completion.choices[0].message;
const completionCost = calculateCompletionCost(content, "gpt-4o");

console.log(`Total cost:`, promptCost + completionCost);

Note: If you're including images as part of a chat completion then they will also be included in the cost calculation!

Images

You can also calculate the cost of generating images:

import { calculateImageGenerationCost } from "tokencost";

const imageOptions = { size: "1024x1024", quality: "standard" };
const cost = calculateImageGenerationCost(imageOptions, "dall-e-3");

And of identifying images with Vision:

import { calculateImageDetectionCost } from "tokencost";

const image = readFileSync("image.jpg");
const cost = calculateImageDetectionCost(image, "gpt-4o");

Audio

const openai = new OpenAI({ apiKey });

const transcription = await openai.audio.transcriptions.create({
  file: fs.createReadStream("audio.mp3"),
  model: "whisper-1",
});

const { duration } = transcription;
const cost = calculateCompletionCost({ duration }, "whisper-1");

Models

Fetch a list of all currently tracked models (updates from LiteLLM's cost dictionary).

import { update, models, getModel, getModels } from "tokencost";

// the last fetched model list (updated when the module is installed)
console.log(models);
// [
//   "gpt-4": {
//     "max_tokens": 4096,
//     "max_input_tokens": 8192,
//     "max_output_tokens": 4096,
//     "input_cost_per_token": 0.00003,
//     "output_cost_per_token": 0.00006,
//     "mode": "chat",
//     "supports_function_calling": true,
//     "provider": "openai"
//   },
//   ...
// ]

// or get all the models of a specific type:
const chatModels = getModels("chat");

// or type and provider
const openAiImageModels = getModels("image-generation", { provider: "openai" });
// or you can grab a specific model by its key
const model = getModel("gpt-4o");

// fetching image models is a little more complicated as they are
// keyed on their quality and size
let imageModel = getModel("hd/1024-x-1792/dall-e-3");
// or
imageModel = getImageModel("dall-e-3", { quality: "hd", size: "1024x1792" });

// you can also manually update the model list (don't do it too often):
await update();

Contributing

Contributions to TokenCost are welcome! Feel free to create an issue for any bug reports, complaints, or feature suggestions.

License

TokenCost is released under the MIT License.