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

countokens

v1.3.1

Published

Cloc for counting tokens

Readme

Countokens

npm version License: ISC

A command-line tool, similar to cloc (Count Lines of Code), but designed to count OpenAI Tiktoken tokens in files within a directory.

Features

  • Recursively scans directories to find files.
  • Counts tokens using specified OpenAI models (defaults to gpt-4o-mini).
  • Provides both human-readable and JSON output formats.
  • Allows custom ignore patterns (globs).
  • Ignores node_modules by default.
  • Handles binary or unreadable files gracefully by skipping them.
  • Supports tree view output (--tree).
  • Limits tree depth with -d, --depth <number> option.

Installation

You can install countokens globally using npm:

npm install -g countokens

Alternatively, you can run it directly without installation using npx:

npx countokens [options] [path]

Usage

countokens [options] [path]

Arguments:

  • path: The root directory to scan for files. Defaults to the current directory (.).

Options:

  • -m, --model <name>: Specify the OpenAI model name for tokenization. (Default: "gpt-4o-mini")
  • -i, --ignore <globs>: Provide a comma-separated list of glob patterns to ignore. Example: -i "*.log,dist/**"
  • --json: Output the results in JSON format (includes total count and per-file counts).
  • --tree: Display the token counts in a file tree structure.
  • -d, --depth <number>: When used with --tree, limit how many directory levels to display (default: all levels).
  • -h, --help: Display help information.

Examples

1. Count tokens in the current directory using the default model:

npx countokens

Output (Example):

Token count (gpt-4o-mini): 15,823 tokens

src/cli.ts       1700
package.json      666
README.md         350
.gitignore        128

2. Count tokens in a specific directory (./src) using gpt-4:

npx countokens --model gpt-4 ./src

3. Count tokens, ignoring log files and the build directory:

npx countokens -i "*.log,build/**"

4. Get JSON output:

npx countokens --json

Output (Example):

{
  "total": 15823,
  "files": {
    "src/cli.ts": 1700,
    "package.json": 666,
    "README.md": 350,
    ".gitignore": 128
  }
}

5. Display token counts as a file tree:

npx countokens --tree

Output (Example):

Token count (gpt-4o-mini): 2,500 tokens
└─ src (2,000)
   ├─ cli.ts (1,700)
   └─ utils (300)

6. Display tree up to a maximum depth of 2:

npx countokens --tree --depth 2

Output (Example):

Token count (gpt-4o-mini): 2,500 tokens
└─ src (2,000)
   ├─ cli.ts (1,700)
   └─ utils (300)

Ignoring Files

By default, countokens ignores files and directories matching the node_modules/** pattern. You can add more patterns using the -i or --ignore flag, providing a comma-separated list of globs.

Tokenization Models

The tool uses the tiktoken library for tokenization. You can specify which model's tokenizer to use via the --model flag. Refer to the Tiktoken documentation for available model names. The default is gpt-4o-mini.

Contributing

Issues and pull requests are welcome! Please refer to the GitHub repository and the issue tracker.

License

This project is licensed under the ISC License - see the LICENSE file for details (or check package.json).