niieani/gpt-tokenizer

The fastest JavaScript BPE Tokenizer Encoder Decoder for OpenAI's GPT models (gpt-5, gpt-o*, gpt-4o, etc.). Port of OpenAI's tiktoken with additional features.

What it is

gpt‑tokenizer is a pure‑JavaScript/TypeScript library that implements the same Byte‑Pair‑Encoding (BPE) tokenisation used by OpenAI’s GPT models. It lets you turn a string (or a chat‑style message list) into the integer token IDs that the model expects, and turn those IDs back into text.

Why it matters

  • Model‑compatible – supports every current OpenAI model family (GPT‑5, GPT‑4.1, GPT‑4o, the o‑series, GPT‑OSS, audio, vision, legacy GPT‑2/3/4, etc.) and the exact encodings each model uses.
  • Fast & tiny – advertised as the fastest pure‑JS tokenizer with low memory use, suitable for Node 26 and browser environments.
  • Convenient helpers – functions to check token limits without full encoding, estimate API cost, count tokens, and work with streaming data via generators.
  • Zero‑runtime globals – no hidden caches that leak memory, and you can control the internal merge‑cache size.
  • Typed – written in TypeScript, so IDEs give you autocomplete and type safety.

Core features (as described in the README)

Feature What it does
encode / decode Basic text‑to‑tokens and tokens‑to‑text conversion.
encodeChat Tokenises an array of {role, content} messages exactly as OpenAI’s chat API expects.
isWithinTokenLimit Quickly tells you whether a string or chat fits a model’s token limit, returning the token count or false.
countTokens / createIncrementalCounter Precise token counting, with a mutable counter for incremental edits.
Generators (encodeGenerator, decodeGenerator, decodeAsyncGenerator) Stream tokenisation or detokenisation in chunks – useful for large inputs or async pipelines.
estimateCost Computes USD cost for a given token count using the model’s pricing data.
Model catalog Exposes a full list of OpenAI models, their context windows, capabilities, and pricing (src/models.ts).
Special‑token handling Fine‑grained control over which special tokens are allowed or disallowed during encoding.
Cache control (setMergeCacheSize, clearMergeCache) Tune or disable the internal BPE merge cache for memory‑critical workloads.

Typical usage pattern

import { encode, decode, isWithinTokenLimit, encodeChat } from 'gpt-tokenizer';

const text = "Hello, world!";
const tokens = encode(text);               // → [15496, 995]
const back = decode(tokens);               // → "Hello, world!"

const chat = [
  {role: 'system', content: 'You are a helpful assistant.'},
  {role: 'user',   content: 'What is tokenisation?'}
];
const chatTokens = encodeChat(chat);
const ok = isWithinTokenLimit(chat, 4096); // true/false or token count

Who uses it?

The README lists several high‑profile adopters: Microsoft (Teams‑AI, GenAIScript), Elastic (Kibana), Effect TS, CodeRabbit, and Ironclad’s Rivet.

Where to get it

  • NPM – npm install gpt-tokenizer
  • Browser – import from the CDN URL https://unpkg.com/gpt-tokenizer/esm/main.js
  • Playground – an interactive demo at https://gpt-tokenizer.dev/

All information above is taken directly from the repository’s README; no additional features are inferred.

Related

  • Project
  • Project
  • Project
  • Project
  • Dispatch