JavaScript tokenizer libraries

Updated Nov 2026

JavaScript tokenizer libraries split text into the tokens a language model reads and map each token to an integer ID. A byte-pair encoder merges frequent character sequences into tokens from the vocabulary of one model family. These libraries let apps and AI agents count tokens before a model call.

Popularity

js-tiktoken has the most monthly downloads in this comparison, at 49.4M.

LibraryMonthly downloadsStarsLast releaseLicense
js-tiktoken
49.4M
1.1KAug 2025MIT
Transformers.js
19.1M
16.4KOct 2026Apache-2.0
Tokenizers.js
15.4M
62Sep 2026Apache-2.0
gpt-tokenizer
10.6M
862Nov 2026MIT
tiktoken
8.7M
1.1KAug 2025MIT
@anthropic-ai/tokenizer
1.4M
108Jul 2023Apache-2.0
GPT-3-Encoder
640.1K
722Jan 2023MIT
Downloads: npm, last thirty days. Stars and releases: GitHub. Amber: no release in over a year.

Libraries and code examples

Provides a JavaScript port of the tiktoken tokenizer for OpenAI models.

Downloads / mo
49.4M
Stars
1.1K
Last release
1.0.21
License
MIT

Quickstart

From README.md
$ npm install js-tiktoken@1.0.21
import { getEncoding } from "js-tiktoken";
const tokenizer = getEncoding("o200k_base");
console.log(tokenizer.encode("Hello world"));

js-tiktoken examples

From open-source projects on GitHub

Transformers.js

Loads pretrained model tokenizers and formats model-specific chat prompts.

Downloads / mo
19.1M
Stars
16.4K
Last release
4.3.2
License
Apache-2.0

Strengths

  • Loads tokenizers by model ID. AutoTokenizer selects an implementation from tokenizer_config.json.
  • Applies model-specific chat templates. Render roles and control tokens in the model’s prompt format.

Limitations

  • Tool formatting depends on templates. Tools have no effect when templates lack function-calling support.
  • Requires pretrained tokenizer files. Loading fails when required tokenizer files are missing.

Quickstart

From tokenizers.md
$ npm install @huggingface/transformers@4.3.2
import { AutoTokenizer } from "@huggingface/transformers";
const tokenizer = await AutoTokenizer.from_pretrained("Xenova/bert-base-uncased");
const { input_ids } = await tokenizer("Hello world");
console.log(input_ids);

Transformers.js examples

From open-source projects on GitHub

Implements common model tokenizers in pure JavaScript and TypeScript.

Downloads / mo
15.4M
Stars
62
Last release
0.2.0
License
Apache-2.0

Quickstart

From README.md
$ npm install @huggingface/tokenizers@0.2.0
import { Tokenizer } from "@huggingface/tokenizers";
const base = "https://huggingface.co/HuggingFaceTB/SmolLM3-3B/resolve/main";
const json = await fetch(`${base}/tokenizer.json`).then(response => response.json());
const config = await fetch(`${base}/tokenizer_config.json`).then(response => response.json());
const tokenizer = new Tokenizer(json, config);
console.log(tokenizer.encode("Hello world").ids);

Tokenizers.js examples

From open-source projects on GitHub

Encodes and decodes model text with a pure-JavaScript byte-pair tokenizer.

Downloads / mo
10.6M
Stars
862
Last release
4.1.2
License
MIT

Strengths

  • Checks budgets before full encoding. Check text or chat limits without encoding the entire input.
  • Updates counts after text edits. Track appended text and edits with an incremental counter.

Limitations

  • Reserved tokens require explicit permission. Configure allowedSpecial when reserved token strings should be accepted.
  • Incremental edits can rescan text. Long words or whitespace runs can require rescanning a large region.

Quickstart

From README.md
$ npm install gpt-tokenizer@4.1.2
import { encode, decode } from "gpt-tokenizer";
const ids = encode("Hello world");
console.log(ids, decode(ids));

gpt-tokenizer examples

From open-source projects on GitHub

Provides JavaScript and WASM bindings to OpenAI-model byte-pair tokenizers.

Downloads / mo
8.7M
Stars
1.1K
Last release
1.0.22
License
MIT

Strengths

  • Maps models to their encodings. Resolve a model tokenizer with encoding_for_model.
  • Loads individual encoding tables. Use tiktoken/lite to avoid loading every encoder at once.

Limitations

  • Encoders need explicit cleanup. Call free() when an encoder is no longer needed.
  • Vite requires extra build plugins. Add WASM and top-level-await plugins when bundling with Vite.

Quickstart

From README.md
$ npm install tiktoken@1.0.22
import { get_encoding } from "tiktoken";
const tokenizer = get_encoding("o200k_base");
console.log(tokenizer.encode("Hello world"));
tokenizer.free();

tiktoken examples

From open-source projects on GitHub

@anthropic-ai/tokenizer

Tokenizes text for Claude models.

Downloads / mo
1.4M
Stars
108
Last release
0.0.4
License
Apache-2.0

Quickstart

From README.md
$ npm install @anthropic-ai/tokenizer@0.0.4
import { countTokens } from "@anthropic-ai/tokenizer";
console.log(countTokens("Hello world"));

Implements the original GPT-2/GPT-3 text encoder and decoder in JavaScript.

Downloads / mo
640.1K
Stars
722
Last release
1.1.4
License
MIT

Strengths

  • Fits legacy GPT-2/3 workflows. Encode text and decode token IDs for those model families.
  • Exposes individual decoded token pieces. Decode token IDs separately when inspecting a sequence.

Limitations

  • Targets legacy GPT-2/3 encodings. Match the model tokenizer before relying on context-budget estimates.

Quickstart

From README.md
$ npm install gpt-3-encoder@1.1.4
const { encode, decode } = require("gpt-3-encoder");
const ids = encode("Hello world");
console.log(ids, decode(ids));

GPT-3-Encoder examples

From open-source projects on GitHub

FAQ

js-tiktoken and gpt-tokenizer suit counting tokens for OpenAI models in pure JavaScript. Transformers.js suits open models that publish their own tokenizer on Hugging Face. tiktoken suits Node.js code that can load WASM bindings.

Encode the text with the tokenizer for the target model and take the length of the result. js-tiktoken and gpt-tokenizer do this for OpenAI encodings.

tiktoken is the byte-pair tokenizer for OpenAI models. Its npm package provides WASM bindings, and js-tiktoken is a pure JavaScript port.

Byte pair encoding builds a vocabulary by repeatedly merging the most frequent pair of adjacent symbols. A tokenizer applies those merges to split new text into tokens.

The Claude API's token counting endpoint counts tokens for current models. @anthropic-ai/tokenizer tokenizes text locally but matches only older Claude models.

For coding agents

Found your tokenizer library? Help your agent use it.

Connect Exa so your coding agent can find the library’s documentation, check its API, and look up implementations while you build.

Run in your terminal

claude plugin install exa@claude-plugins-official