← all repositories
niieani/gpt-tokenizer

OpenAI's tiktoken, rebuilt to load synchronously in JS

A TypeScript port of tiktoken that lets JavaScript count GPT tokens without async imports, memory leaks, or a Python runtime.

825 stars TypeScript Language ModelsData Tooling
gpt-tokenizer
Not currently ranked — collecting fresh signals.
star history

What it does

gpt-tokenizer is a TypeScript reimplementation of OpenAI’s tiktoken that encodes and decodes text using the same Byte Pair Encoding rules as GPT-3.5 through GPT-5, o1, o3, and the open-weight Harmony models. It runs in Node, the browser, or any JavaScript environment, and handles both plain text and chat-formatted conversations. The library also estimates API costs and checks token limits without fully encoding the input.

The interesting bit

While most tokenizers force an async import or stash state in a global cache, this one loads synchronously and keeps no global state. It exposes generator-based encode and decode functions, including an async generator that can consume a stream of tokens and yield text chunks on the fly.

Key highlights

  • Synchronous loading and operation, eliminating async/await boilerplate in constrained contexts
  • Generator and async generator variants for streaming encode and decode without buffering entire arrays
  • isWithinTokenLimit short-circuits limit checks, and estimateCost references built-in OpenAI pricing tables
  • No global cache, avoiding the memory leaks the README attributes to earlier GPT-3-Encoder implementations
  • Used in production by Microsoft Teams, Elastic Kibana, and Effect TS

Verdict

Grab it if you need client-side token counting, offline cost estimation, or streaming decode without a Python dependency. If you already proxy all token math through a backend service, you probably don’t need another npm package for it.

Frequently asked

What is niieani/gpt-tokenizer?
A TypeScript port of tiktoken that lets JavaScript count GPT tokens without async imports, memory leaks, or a Python runtime.
Is gpt-tokenizer open source?
Yes — niieani/gpt-tokenizer is open source, released under the MIT license.
What language is gpt-tokenizer written in?
niieani/gpt-tokenizer is primarily written in TypeScript.
How popular is gpt-tokenizer?
niieani/gpt-tokenizer has 825 stars on GitHub.
Where can I find gpt-tokenizer?
niieani/gpt-tokenizer is on GitHub at https://github.com/niieani/gpt-tokenizer.

heatdrop uses Google Analytics to see which pages get read — nothing else. Your call. How we handle data.