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

@shivam.dixit/token-budget-tiktoken

v0.1.4

Published

tiktoken Tokenizer adapter for token-budget, backed by pure-JS js-tiktoken by default (with an optional Node-only native/WASM path).

Readme

token-budget-tiktoken

Exact tiktoken counting for token-budget, backed by pure-JS js-tiktoken by default, with an optional Node-only native/WASM path.

Install

npm install @shivam.dixit/token-budget @shivam.dixit/token-budget-tiktoken

token-budget is a peer dependency. js-tiktoken is a regular dependency (it is this package's job). The optional ./native subpath additionally needs the tiktoken package — install it yourself if you want it.

Usage

import { TokenBudget } from '@shivam.dixit/token-budget';
import { createTiktokenTokenizer } from '@shivam.dixit/token-budget-tiktoken';

// Async: resolves the encoding for the model and loads its rank table
// (dynamic import — only the encoding you use is fetched, not all of them).
const tokenizer = await createTiktokenTokenizer({ model: 'gpt-4o' });

const budget = new TokenBudget({ maxTokens: 128000, tokenizer });

count()/encode() on the returned tokenizer are fully synchronous once resolved — the async factory is the only initialization step, matching the core Tokenizer interface exactly with no other code changes required.

Pass encoding instead of/as well as model to pick one explicitly:

await createTiktokenTokenizer({ encoding: 'cl100k_base' });

Native (Node-only, opt-in)

For Node-only, performance-critical use, swap in the native/WASM tiktoken package via the /native subpath. Install tiktoken yourself first (npm install tiktoken) — it's an optional peer dependency, not pulled in by default:

import { createTiktokenNativeTokenizer } from '@shivam.dixit/token-budget-tiktoken/native';

// Fully synchronous — no async factory needed; the native build loads
// its WASM eagerly at import time in Node.
const tokenizer = createTiktokenNativeTokenizer({ model: 'gpt-4o' });
const budget = new TokenBudget({ maxTokens: 128000, tokenizer });

API

| Export | Description | | --- | --- | | createTiktokenTokenizer(options?) | Promise<Tokenizer> — pure-JS, works in Node/browser/edge. { model? } (default 'gpt-4o') auto-selects an encoding; { encoding } overrides it. | | resolveEncodingForModel(model) | Returns the encoding name a model would resolve to, without loading it. | | createTiktokenNativeTokenizer(options?) (from /native) | Tokenizer, synchronous — same options, Node-only. |

Tokenizer instances are cached per encoding (not per call, and concurrent loads of the same uncached encoding are de-duplicated) — construct as many createTiktokenTokenizer() calls as you like; loading each encoding's rank table only happens once.

Throughput

Measured on this package's own CI-shaped test run (test/benchmark.test.ts), counting a ~2,200-word English sample repeatedly, o200k_base encoding:

| Path | Throughput | | --- | --- | | js-tiktoken (pure JS) | ~480,000 tokens/sec | | native tiktoken (WASM) | ~330,000–470,000 tokens/sec |

Take these as an order-of-magnitude reference, not a guarantee — both comfortably exceed real-time chat-application throughput needs; reach for the native path only if you're counting tokens for very large volumes of text in a tight Node-only hot path. Re-run npm run test -- benchmark in this package to measure on your own hardware.

Accuracy vs. the heuristic estimator

Exact tiktoken counts vs. token-budget's built-in chars/4 heuristic estimator, on a shared sample corpus (see test/benchmark.test.ts for the throughput half; this table was generated with the snippet in packages/token-budget/README.md):

| Sample | Exact (tiktoken, o200k_base) | Heuristic (chars/4) | Error | | --- | --- | --- | --- | | English prose | 201 | 225 | +11.9% | | Code | 280 | 195 | −30.4% | | JSON | 560 | 345 | −38.4% | | Non-Latin (Japanese) | 320 | 150 | −53.1% |

The heuristic is within Phase 1's documented ≤10% tolerance for plain English prose, but degrades sharply for code, structured data, and non-Latin scripts — exactly the cases this package (or token-budget-claude's calibration utility) exists for.

The wider project

Part of the token-budget monorepo — the core package, the other framework/tokenizer adapters, benchmarks, and the flagship coding-agent example all live there. See the compatibility matrix for exactly what every adapter is tested against.

License

MIT