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

@ferrow/token-estimator

v1.0.0

Published

Heuristic LLM prompt token count estimation with no tokenizer dependency — char/word/CJK/code aware, per-model calibration, message-array overhead, budget helpers.

Downloads

92

Readme

token-estimator

CI

Heuristic token count estimation for LLM prompts — no tokenizer dependency, zero runtime deps, strict TypeScript.

This blends a chars/4 baseline with word-boundary, whitespace-run, CJK, and code-density signals, then applies a per-model calibration factor. It ships with an explicit +/-15% error margin. Use it for quick budget checks (does this prompt fit in my context window?) — not for billing-critical accuracy. For that, use the provider's real tokenizer (e.g. tiktoken for OpenAI, Anthropic's token-counting endpoint for Claude).

Quickstart

import { estimateTokens, estimateMessages, fitsWithin } from "token-estimator";

estimateTokens("The quick brown fox jumps over the lazy dog.");
// => { tokens: 11, errorMargin: 0.15, low: 9, high: 13 }

const messages = [
  { role: "system", content: "You are a helpful assistant." },
  { role: "user", content: "Explain quantum computing." },
];

estimateMessages(messages, { model: "claude" });
// => { tokens: ..., errorMargin: 0.15, low: ..., high: ... }

fitsWithin(messages, 8000, 1000);
// => { fits: true, estimatedTokens: ..., limit: 8000, reserve: 1000, available: 7000 }

API

estimateTokens(text, options?)

Estimates the token count of a single string.

  • text: string
  • options.model?: "claude" | "gpt" | "generic" — calibration curve, default "generic".
  • options.isCode?: boolean — force code-density mode; auto-detected if omitted.
  • Returns { tokens, errorMargin, low, high }.

estimateMessages(messages, options?)

Estimates a chat message array, adding MESSAGE_OVERHEAD_TOKENS (4) per message for role/delimiter wrapping.

  • messages: { role: string; content: string }[]
  • Returns the same TokenEstimate shape.

fitsWithin(messages, limit, reserve?, options?)

Budget helper: does this message array fit within limit tokens after reserving reserve tokens (e.g. for the model's response)?

  • Returns { fits, estimatedTokens, limit, reserve, available }.

Constants

  • ERROR_MARGIN0.15
  • MESSAGE_OVERHEAD_TOKENS4

Types

ModelFamily, EstimateOptions, TokenEstimate, ChatMessage, FitsWithinResult.

Limits

  • Estimates only, +/-15% stated margin. Never use this for exact billing, quota enforcement, or hard context-limit truncation — use the provider's real tokenizer for anything billing-critical.
  • Calibration factors (claude/gpt) are informal approximations, not vendor-published constants, and drift as vendors change tokenizers.
  • CJK detection is range-based and does not distinguish scripts with meaningfully different tokenization behavior beyond the CJK/non-CJK split.
  • Code detection is a density heuristic on the first 2000 characters; it can misclassify short snippets or unusual languages.

Part of the ferrow-toolkit collection · Sponsored by Ferrow