toklab
Fast bulk tokenizer + token counter for OpenAI BPE encodings. Rust core wrapping tiktoken-rs, Python frontend.
The problem
You need to count tokens for 100k strings to plan a context budget,
truncate inputs, or estimate cost. Pure-Python tiktoken does this fine
for one-shot calls, but a Python loop over a long list spends most of
its time in interpreter overhead and per-call init.
toklab keeps the same encoding (it uses tiktoken-rs, which ships the
exact byte tables from the official tiktoken release) but exposes a bulk
API that releases the GIL and parallelizes across cores.
Install
pip install toklab
30-second quickstart
from toklab import Tokenizer
tok = Tokenizer.for_model("gpt-4")
print(tok.count("hello world")) # 2
texts = ["hello", "world", "lorem ipsum"]
print(tok.count_many(texts)) # [1, 1, 4]
print(tok.count_many(texts, parallel=True)) # same, distributed across cores
# Length-budgeting helpers.
print(tok.fits("hello world", budget=5)) # True
print(tok.truncate_to("a long sentence" * 100, budget=20))
API
class Tokenizer:
@classmethod
def for_model(cls, model: str) -> Tokenizer: ...
@classmethod
def for_encoding(cls, name: str) -> Tokenizer: ...
# name in {"cl100k_base", "o200k_base"}
def count(self, text: str) -> int: ...
def count_many(self, texts: list[str], *, parallel: bool = False) -> list[int]: ...
def encode(self, text: str) -> list[int]: ...
def decode(self, tokens: list[int]) -> str: ...
def fits(self, text: str, *, budget: int) -> bool: ...
def truncate_to(self, text: str, *, budget: int) -> str: ...
License
Dual-licensed under MIT or Apache-2.0 at your option.
Metadata
Release files for toklab 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| toklab-0.1.1-cp310-abi3-macosx_11_0_arm64.whl | CPython 3.10 | abi3 | macOS 11.0+ ARM64 | Details |
Release files / toklab-0.1.1-cp310-abi3-macosx_11_0_arm64.whl
| Download URL | toklab-0.1.1-cp310-abi3-macosx_11_0_arm64.whl |
|---|---|
| Size | 4.2 MB |
| Tags | CPython 3.10 abi3 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
fced022058f36f2e0d80591bc31124b12428a5593408b02b52d67b4e92e18263
|
|
BLAKE2b-256 checksum How to use checksums |
10fd8e2d5a1f03e153484cb501fcfa8cc512a169df90f84b799c2defd19ccf46
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.4
|
Security for toklab 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.