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 🙏

© 2026 – Pkg Stats / Ryan Hefner

llm-token-estimator

v1.0.4

Published

Fast, offline token estimation for popular LLMs

Readme

llm-token-estimator

Offline token estimation for Large Language Models.

Installation

npm install llm-token-estimator

Usage

Basic usage

const { estimateTokens } = require("llm-token-estimator");

const result = estimateTokens(
  "Explain transformers like I'm five.",
  { model: "gpt-4o" }
);

console.log(result);

Output:

{
  tokens: 9,
  characters: 39,
  model: "gpt-4o",
  maxTokens: 128000,
  vendor: "openai",
  warning: null
}

Language-aware estimation

For better accuracy with non-English content:

const result = estimateTokens(
  "Bonjour, comment allez-vous?",
  { 
    model: "gpt-4o",
    language: "fr"  // French
  }
);

// Supported languages: en, es, fr, de, it, pt, ru, zh, ja, ko, ar, hi, code

Using chat-style inputs (array of strings)

Useful when estimating prompts made of multiple messages:

estimateTokens(
  [
    "You are a helpful assistant.",
    "Summarize the following text:",
    articleText
  ],
  { model: "claude-3-sonnet" }
);

Handling context limit warnings

const { warning } = estimateTokens(longPrompt, {
  model: "gpt-4"
});

if (warning) {
  console.warn(warning);
}

Listing supported models

const { listModels } = require("llm-token-estimator");

console.log(listModels());

Accuracy and Limitations

This library provides approximate token counts based on character-to-token ratios. While fast and dependency-free, it has limitations:

  • Good for: Quick estimates, cost approximation, context limit checks
  • Limitations: Language variations, content types, model-specific tokenization

For production applications requiring high accuracy, consider using:

  • tiktoken for OpenAI models
  • Model-specific tokenizers for other providers

Supported Models

Includes 100+ models from major providers:

OpenAI: GPT-5.2, GPT-5, GPT-4.1, o3, o4-mini, GPT-OSS models, and more Anthropic: Claude 4 series (Opus 4.6, Sonnet 4.5, Haiku 4.5) Google: Gemini 3, Gemini 2.5 series Meta: LLaMA 3.x series Mistral: Large 3, Medium 3.1, Ministral 3 series Others: xAI Grok, Cohere, Alibaba Qwen, DeepSeek, Amazon Nova, and more

Use listModels() to see all supported models.


Default behavior

  • Default model: gpt-3.5-turbo
  • Default language: en (English)
  • Input can be:
    • a string
    • an array of strings
  • Output tokens are not included (input only)

Example use cases

  • Pre-flight prompt validation
  • CI checks for context overflows
  • Prompt truncation logic
  • Cost estimation (approximate)
  • Multi-language content estimation
  • Model comparison and selection
  • Rate limiting based on token counts

API Reference

estimateTokens(input, options)

Parameters:

  • input (string | string[]): Text to estimate tokens for
  • options (object):
    • model (string): Model name (default: "gpt-3.5-turbo")
    • language (string): Language code for better estimation (default: "en")

Returns: Object with tokens, characters, model, maxTokens, vendor, warning

listModels()

Returns: Array of all supported model names

Contributing

We welcome contributions! Feel free to:

  • Add new models
  • Improve estimation accuracy
  • Add new languages
  • Fix bugs or enhance documentation