0.3% of 4,818 JS/TS repos built with AI coding agents; 39 counting our dependency samples
Gid Score
44 /100
#2138 of 3,794 tools, #31 in prompts, structured output and evals · ranking
Rank in prompts, structured output and evals
#6 of 72
by use in those projects
Downloads
1.2M
npm, last 7 days, gpt-tokenizer
GitHub stars
855
63 forks · last push Oct 18, 2026
How GPT Tokenizer is used
GPT Tokenizer is a dependency in 14 of the 6,208 public GitHub projects set up for AI coding agents that we scanned, too few to describe it from them alone. Our scan also samples projects by dependency (Solana 2,661, Ethereum and EVM 1,496, 3D and games 2,818, agent frameworks and SDKs 10,983); counting those, it appears in 39 projects, and what it is used with below comes from all of them. The most used alternative in our scan is tiktoken (JavaScript) (10 projects). Projects that use it most often also use OpenAI (46.2%), Anthropic SDK (43.6%), Husky (43.6%).
Share of public repos configured for each agent that list GPT Tokenizer as a dependency.
What GPT Tokenizer projects add to it
Share of the 14 public projects built with AI coding agents that use GPT Tokenizer and also depend on each package. From our scan of 6,208 repos, updated Oct 20, 2026.
## GPT Tokenizer
- A bare `import ... from 'gpt-tokenizer'` uses `o200k_base` (GPT-5, GPT-4.1, GPT-4o, o-series). For other models import `gpt-tokenizer/model/<model>`; for an unlisted model with a known encoding import `gpt-tokenizer/encoding/<encoding>`.
- For limit checks use `isWithinTokenLimit(input, limit)`, not `encode(text).length`. It returns `false` when over the limit, otherwise the token count.
- When calling `encodeChat` from the default import, pass the model name. Otherwise import the model-specific module.
- Special tokens are disallowed by default. Pass `{ allowedSpecial: ALL_SPECIAL_TOKENS }` or a custom `Set` only when the input is meant to contain them.
- Use `countChatCompletionTokens` for requests with function definitions, only on models that support `function_calling`.
- `estimateCost` needs a model spec, from a model import or as the second argument. Treat fields like `main?.input` as possibly missing.
- In the browser, import the ES module (`https://unpkg.com/gpt-tokenizer/esm/main.js`) and pin a version. UMD bundles were removed in 4.0.0.
Minimal code example: count tokens for a function-calling request
import {
countChatCompletionTokens,
type ChatCompletionRequest,
} from 'gpt-tokenizer/model/gpt-4o'
const request: ChatCompletionRequest = {
messages: [
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: 'Find the weather for San Francisco.' },
],
functions: [
{
name: 'get_weather',
description: 'Look up the weather for a city.',
parameters: {
type: 'object',
required: ['city'],
properties: {
city: { type: 'string' },
unit: { type: 'string', enum: ['celsius', 'fahrenheit'] },
},
},
},
],
}
const promptTokenEstimate = countChatCompletionTokens(request)
gpt-tokenizer is a TypeScript Byte Pair Encoding encoder and decoder for OpenAI models, published on npm as gpt-tokenizer under the MIT license. The repository describes it as a "Port of OpenAI's tiktoken with additional features". It was created on 2023-03-22. The latest release is 4.0.0, published 2026-08-16.
It turns text into token integers and back. A plain import uses o200k_base, the encoding the README gives for GPT-5, GPT-4.1, GPT-4o and the o-series. Other models get their own import path, such as gpt-tokenizer/model/gpt-3.5-turbo, and any encoding can be loaded directly, such as gpt-tokenizer/encoding/cl100k_base. Beyond encode and decode, there are helpers for chats, token limits, function-calling requests, streaming generators and cost estimates.
It is for JavaScript and TypeScript developers who need token counts for OpenAI models in Node or in the browser. The builders cited below used it for browser-based token counters and for an n8n node that counts tokens and cost before calling OpenAI. [1][2]
Why teams pick it
Limit checks that stop early.isWithinTokenLimit takes a string or a chat and returns false once the limit is exceeded, or the token count if it fits. The README says it does this without encoding the entire input. [1]
Counts function-calling requests, not just text.countChatCompletionTokens takes a whole chat completion request and counts message overhead, function definitions and pinned function calls. It is only available on models that support function_calling. [1]
Prices and limits bundled in. The package carries an OpenAI model catalog with context limits, capabilities and pricing. estimateCost returns USD figures for the main and batch APIs, including cached tokens where data exists. [1]
Synchronous and streaming. The README says it can be loaded and used without async/await. It also has generator versions of the encoder and decoder, including decodeAsyncGenerator for async token streams. [1]
No global cache. The maintainers point out that the package keeps no global cache. They contrast this with the memory leaks of the original GPT-3-Encoder implementation. [1]
Runs client-side in a Web Worker. LaunchBoosts runs the library in a Web Worker in the browser to count pasted text, so long pastes do not block the page. [6]
Know before you start
Exact only for OpenAI encodings. The package implements OpenAI's encodings. LaunchBoosts applies the o200k count to Claude and Gemini only as an estimate, and tells users to check with each provider's own counting API. [6]
4.0.0 dropped UMD bundles. Release 4.0.0 is a breaking change for script-tag users. Browser use now goes through the ES modules, and the README says to pin a version in production. [3]
Special tokens are refused by default. Text containing special tokens is not encoded as such unless you allow them through EncodeOptions. The README also notes that not every model supports every special token. [1]
Chat encoding needs a model.encodeChat from the default import needs the model name passed in. Otherwise you must import the model-specific module. [1]
Cost data can be missing.estimateCost needs a model spec, either from a model import or as an argument. Even then, the README warns that pricing may not exist for every model. [1]
Built with GPT Tokenizer
Real uses, from the builders' own posts
Koala (koala.sh)
Free online GPT-4/GPT-3.5 token counter
The question
Koala writes that OpenAI's own tokenizer tool is for legacy models and is not accurate for GPT-4 or GPT-3.5.
What they built
A free web token counter and a POST API at <code>https://koala.sh/api/tokens/</code>, built on gpt-tokenizer with the <code>cl100k_base</code> encoder.
The author found it hard to know how many tokens a prompt would take before sending it to OpenAI, and says estimating with textLength/4 was not good enough.
What they built
A community node, <code>n8n-nodes-gpt-tokenizer</code>. It encodes and decodes BPE tokens, measures token length, calculates cost before an OpenAI call, and splits text into chunks that match a set token limit. It uses gpt-tokenizer under the hood.
A free counter that encodes text with <code>o200k_base</code> using gpt-tokenizer in a Web Worker. It shows context-window use and cost per call across GPT, Claude and Gemini models.
Result
The page says the count is exact for OpenAI's <code>o200k_base</code> models, and that nothing pasted is uploaded or stored. Claude and Gemini rows are marked as estimates.
Tools that do the same job, so a project picks one or the other. Add-ons and libraries used alongside GPT Tokenizer are not listed here; they are in "Often used with".
Description written by AgentGid from these official pages, checked Oct 13, 2026. Usage numbers come from our own scan. Claims by the maintainers are quoted, not tested by us.