Skip to content
Agents tracked: 440 Downloads (7d): 202M down 19.5% GitHub stars: 10.4M Releases (7d): 607 Agent status: all upSince last limit reset: Claude 28d · Codex 0d Updated Oct 21, 2026

tiktoken (Python)

Fast BPE tokenizer for OpenAI models enabling accurate token counting and text splitting.

Prompts, structured output and evals PyPI MIT

Used in agent projects
57
3.5% of 1,622 Python repos built with AI coding agents
Gid Score
80 /100
#63 of 3,794 tools, #1 in prompts, structured output and evals · ranking
Rank in prompts, structured output and evals
#2 of 72
by use in those projects
Downloads
47.9M
PyPI, last 7 days, tiktoken
GitHub stars
19.5K
1,653 forks · last push Aug 17, 2026

How tiktoken (Python) is used

tiktoken (Python) is a dependency in 57 of the 6,208 public GitHub projects set up for AI coding agents that we scanned (3.5% of the 1,622 Python ones), #2 of 72 in prompts, structured output and evals. Counting the projects our scan samples by dependency (Solana 2,661, Ethereum and EVM 1,496, 3D and games 2,818, agent frameworks and SDKs 10,983), it appears in 1,081; those are left out of every share on this page. The most used alternative in our scan is tokenizers (25 projects). Projects that use it most often also use Pydantic (75.4%), OpenAI Python Library (70.2%), python-dotenv (64.9%). It is more common in projects set up for Roo Code (4.5%) than for Cursor (0.8%).

tiktoken (Python) popularity over time

Top tools by Gid Score

tiktoken (Python): downloads per weekPyPI, tiktoken 47,623,292
tiktoken (Python): GitHub starsopenai/tiktoken 19,493

Gid Score 80: downloads 100, use in agent projects 85, GitHub stars 61, downloads trend 54 (each 0-100). How it is calculated.

Often used with tiktoken (Python)

Pydantic75.4% of tiktoken (Python) projects · 8.7× more than average
OpenAI Python Library70.2% of tiktoken (Python) projects · 17.8× more than average
python-dotenv64.9% of tiktoken (Python) projects · 7.1× more than average
NumPy59.6% of tiktoken (Python) projects · 10.2× more than average
PyYAML57.9% of tiktoken (Python) projects · 9.7× more than average
FastAPI56.1% of tiktoken (Python) projects · 7.5× more than average
Uvicorn54.4% of tiktoken (Python) projects · 7.1× more than average
Requests47.4% of tiktoken (Python) projects · 7.0× more than average

By coding agent

Claude Code0.9% of projects set up for it
GitHub Copilot0.9% of projects set up for it
Cursor0.8% of projects set up for it
Gemini CLI1.7% of projects set up for it
OpenClaw2.3% of projects set up for it
Roo Code4.5% of projects set up for it

Share of public repos configured for each agent that list tiktoken (Python) as a dependency.

What tiktoken (Python) projects add to it

Share of the 57 public projects built with AI coding agents that use tiktoken (Python) and also depend on each package. From our scan of 6,208 repos, updated Oct 20, 2026.

OpenAI API client openai89.1%
Data validation pydantic84.0%
.env config loading python-dotenv80.9%
Numeric arrays numpy75.3%
HTTP client requests75.2%
YAML parsing pyyaml72.3%
ASGI server uvicorn59.1%
Web API framework fastapi54.9%

Using tiktoken (Python) with an AI coding agent

Rules to paste into AGENTS.md or CLAUDE.md

Our summary of the project's own guidance. [1][4] Which file does your agent read?

## tiktoken
- To match a specific OpenAI API model, use `tiktoken.encoding_for_model("gpt-4o")`. Use `tiktoken.get_encoding("o200k_base")` when you know the encoding name.
- If you only need a token count, `enc.encode_to_numpy(text).shape[0]` is an option. Its docstring says it avoids copying tokens into a Python list, and one blogger writes it "barely takes any memory or CPU". It is not in the docs.
- When you change the set of special tokens, build a new `tiktoken.Encoding` with a different `name` that makes the behaviour clear.
- In production, load `Encoding` arguments directly instead of reading private attributes like `_mergeable_ranks`. See `openai_public.py` for examples.
- Prefer passing your `Encoding` object around. Use the `tiktoken_ext` plugin only when `tiktoken.get_encoding` must find it by name.
- For a plugin, create a namespace package with no `tiktoken_ext/__init__.py` and expose `ENCODING_CONSTRUCTORS`. Do not install it in editable mode.
- For API details, read `tiktoken/core.py`. Post questions to the GitHub issue tracker.
Minimal code example: encode, decode and pick a tokeniser by model
import tiktoken
enc = tiktoken.get_encoding("o200k_base")
assert enc.decode(enc.encode("hello world")) == "hello world"

# To get the tokeniser corresponding to a specific model in the OpenAI API:
enc = tiktoken.encoding_for_model("gpt-4o")

Code: MIT, © OpenAI, from the project README [1]

What tiktoken (Python) is

tiktoken is a byte pair encoding (BPE) tokeniser that OpenAI publishes "for use with OpenAI's models". The README says to install it from PyPI with pip install tiktoken. The GitHub repository lists the MIT license and was created on 2022-12-01.

The README explains the idea briefly. Language models read sequences of numbers called tokens, and BPE turns text into them. The README lists four properties of BPE. It is reversible and lossless. It works on arbitrary text. It compresses text, with "each token corresponds to about 4 bytes" on average. It lets the model see common subwords. In tiktoken you load an encoding by name with get_encoding("o200k_base") or by model with encoding_for_model("gpt-4o"), then call encode and decode.

Beyond that core, the README covers two other uses. Its "Extending tiktoken" section shows how to add encodings, such as a cl100k_base variant with <|im_start|> and <|im_end|> special tokens. Its educational submodule is for readers who want to see the BPE procedure step by step. [1][2]

Why teams pick it

  • Look up the tokeniser by model name. tiktoken.encoding_for_model("gpt-4o") returns the tokeniser for a given OpenAI API model. You do not need to know the encoding name first. [1]
  • Speed claim, with stated conditions. The maintainers write that tiktoken is "between 3-6x faster than a comparable open source tokeniser". The measurement used 1GB of text and the GPT-2 tokeniser, compared against GPT2TokenizerFast. [1]
  • New encodings as plain objects. You can construct a tiktoken.Encoding from an existing encoding's parts, add special tokens, and pass the object around. The README calls this the preferred option. The tiktoken_ext plugin is for cases where get_encoding must find your encoding by name. [1]
  • A teaching submodule for BPE. tiktoken._educational can train a small BPE tokeniser on a little text. It can also show how the GPT-4 encoder (cl100k_base) splits a string. [1]
  • A lighter call when you only need a count. Dhaval Singh points to encode_to_numpy, which returns a numpy array. To get a count, he takes .shape[0] of that array. He writes that this approach "barely takes any memory or CPU". [4]

Know before you start

  • The API reference is a source file. For API documentation, the README points to tiktoken/core.py. For example code, it points to an OpenAI Cookbook notebook. Questions go to the issue tracker. [1]
  • Useful methods can go unnoticed. Dhaval Singh writes that encode_to_numpy "is not mentioned in the docs". He found it by reading its docstring in the code. [4]
  • Large strings can spike memory. Dhaval Singh writes that for large strings like 10MB, memory "can spike a lot". With the 4o encoding, he gives peak memory of up to ~80MB and CPU wall time of up to ~1.3s. [4]
  • The benchmark is from an early version. The 3-6x speed figure was measured with tiktoken==0.2.0 on the GPT-2 tokeniser. These are the only figures the README cites. [1]
  • Plugin packaging has strict rules. A tiktoken_ext plugin must be a namespace package with no tiktoken_ext/__init__.py. It needs a module exposing ENCODING_CONSTRUCTORS, and it must not be installed in editable mode. [1]

Built with tiktoken (Python)

Real uses, from the builders' own posts

Counting tokens at scale using tiktoken

Dhaval Singh

Reworking a team token-counting function

The question
He was optimizing a token counting function that his team uses to chunk data for embedding models and to cut off older text. He writes that one problem he ran into was memory spiking on large strings like 10MB. He adds that this "can be a potential problem when scale is high".
What they built
The post describes two ways to deal with it. One is approximating with <code>len(text) // 4</code> where a rough count is enough. The other is counting with tiktoken's <code>model.encode_to_numpy(text).shape[0]</code>.
Result
He writes that the <code>encode_to_numpy</code> approach "barely takes any memory or CPU and is much faster too!"

tiktoken numpy Source [4]

Project facts

Latest version0.14.0, August 17, 2026 [3]
On GitHub sinceDecember 2022 [2]
LicenseMIT [2]

On AgentGid: All prompts, structured output and evals Pydantic OpenAI Python Library python-dotenv

Install

Packagepip install tiktoken
Main stacksPython (53), Next.js (2), Other (1), .NET (1)
Example projectsprompt-security/ps-fuzz, mindroom-ai/mindroom, spire-studio/cloakbot, owid/etl, agentscope-ai/PawBench

Alternatives to tiktoken (Python)

All prompts, structured output and evals

Tools that do the same job, so a project picks one or the other. Add-ons and libraries used alongside tiktoken (Python) are not listed here; they are in "Often used with".

Tool Gid Score Used in agent projectsDownloads / wk GitHub starsLicense
tokenizers pypi
Fast NLP tokenizer library by Hugging Face for text processing.
76 25 (1.5%) 46.6M 11.2K Apache-2.0

More in Prompts, structured output and evals: Ultracite LangSmith Langfuse (Python) GPT Tokenizer TokenLens @graphql-codegen/typescript-operations tiktoken (JavaScript) partial-json

Sources

  1. tiktoken (Python) README (GitHub)
  2. GitHub repository
  3. GitHub releases
  4. Dhaval Singh (personal engineering blog; author's company no: Optimized production token counter used to chunk data before embeddings

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.

Is tiktoken (Python) right for your project? Ask AgentGid →