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

@huen123/llm-token-counter

v0.1.0

Published

Count prompt and chat message tokens for mainstream LLM families with explicit precision metadata.

Readme

@huen123/llm-token-counter

Count prompt text and simple chat-message tokens for mainstream LLM families without making remote API calls.

@huen123/llm-token-counter is a Node.js-first npm package for quick token budgeting across OpenAI, Anthropic, Gemini, Mistral, Cohere, and Llama model families. Every result includes both the token count and metadata that tells you whether the count is exact or estimated.

Install

npm install @huen123/llm-token-counter

What It Counts

  • Plain text with countTokens({ model, input })
  • Structured chat messages with countChatTokens({ model, messages })
  • Supported model metadata with getModelInfo(model)
  • Curated canonical model families with listSupportedModels()

Supported Precision

| Provider family | Plain text | Chat messages | Strategy | | --- | --- | --- | --- | | OpenAI (gpt-*, o*, chatgpt-*) | Exact | Estimated | openai-tiktoken | | Anthropic (claude-*) | Estimated | Estimated | anthropic-tokenizer | | Google (gemini-*) | Estimated | Estimated | gemini-char4 | | Mistral (mistral-*) | Estimated | Estimated | cl100k-heuristic | | Cohere (command-*, aya-*) | Estimated | Estimated | cl100k-heuristic | | Meta (llama-*) | Estimated | Estimated | cl100k-heuristic |

OpenAI plain-text counts use the model tokenizer directly. Chat-message counts are estimated for every provider because message wrappers vary across APIs and versions.

Quick Start

import {
  countChatTokens,
  countTokens,
  getModelInfo,
  listSupportedModels,
} from "@huen123/llm-token-counter";

const promptResult = countTokens({
  model: "gpt-4o",
  input: "hello world!",
});

console.log(promptResult);
// {
//   requestedModel: 'gpt-4o',
//   resolvedModel: 'gpt-4o',
//   provider: 'openai',
//   family: 'gpt-4o',
//   tokenCount: 3,
//   precision: 'exact',
//   strategy: 'openai-tiktoken'
// }

const chatResult = countChatTokens({
  model: "claude-3-5-sonnet-latest",
  messages: [
    { role: "system", content: "Be concise." },
    { role: "user", content: "Summarize this paragraph." },
  ],
});

console.log(chatResult.precision); // "estimated"

console.log(getModelInfo("chatgpt-4o-latest"));
console.log(listSupportedModels());

API

countTokens({ model, input })

Counts tokens for a plain string and returns:

type TokenCountResult = {
  requestedModel: string;
  resolvedModel: string;
  provider: "openai" | "anthropic" | "google" | "mistral" | "cohere" | "meta";
  family: string;
  tokenCount: number;
  precision: "exact" | "estimated";
  strategy:
    | "openai-tiktoken"
    | "anthropic-tokenizer"
    | "gemini-char4"
    | "cl100k-heuristic";
};

countChatTokens({ model, messages })

Accepts only simple string chat messages:

type ChatMessage = {
  role: "system" | "user" | "assistant";
  content: string;
};

Chat counts add a lightweight per-message overhead estimate on top of each message content count.

getModelInfo(model)

Resolves aliases to canonical families and returns:

type ModelInfo = {
  resolvedModel: string;
  provider: "openai" | "anthropic" | "google" | "mistral" | "cohere" | "meta";
  family: string;
  precision: "exact" | "estimated";
  strategy:
    | "openai-tiktoken"
    | "anthropic-tokenizer"
    | "gemini-char4"
    | "cl100k-heuristic";
  aliases: string[];
};

listSupportedModels()

Returns the curated canonical families bundled in the current package release.

Aliases and Unknown Models

  • Known aliases are normalized to canonical families before counting.
  • Unknown models throw an error with close suggestions instead of silently guessing providers.
  • New provider releases usually only need registry updates in a new package version.

Unsupported in v1

  • Tool-call accounting
  • Multimodal inputs such as images, files, and audio
  • Full provider request-body parsing
  • Pricing estimates
  • Remote model-catalog updates
  • Browser runtime support

Development

npm test
npm run build
npm pack

Publish Checklist

npm test
npm run build
npm publish --access public

prepublishOnly already runs the build and test steps before npm publish.