Skip to main content

ctok

ctok reconstructs Claude token counts offline, with no API call or network access. It is unofficial and is not affiliated with Anthropic.

The reconstruction targets counts. Claude does not expose token boundaries, so tokenize() returns one valid minimum-cost tiling, not a claim about Anthropic's exact segmentation. The research behind the model is described in On the biology of Claude's tokenizer.

Count a string

ctok is on PyPI, so uvx fetches and runs it with no install. With no --version it reports both generations, since the same string does not cost the same on each.

uvx ctok "hello, world"
  v3.0
  stream: '⟨bow⟩hello⟨eow⟩,⟨eow⟩⟨bow⟩world⟨eow⟩'
  tokens: ['⟨bow⟩hello⟨eow⟩', ',⟨eow⟩', '⟨bow⟩world⟨eow⟩']

  content 3 + frame 7 = 10

  v4.8
  stream: '⟨bow⟩hello⟨eow⟩,⟨eow⟩⟨bow⟩world⟨eow⟩'
  tokens: ['⟨bow⟩h', 'ello⟨eow⟩', ',⟨eow⟩', '⟨bow⟩world⟨eow⟩']

  content 4 + frame 6 = 10

--version 4.7 picks one family instead.

As a library

from ctok import token_count, tokenize

token_count("hello, world")           # 10, using the v3 family
token_count("hello, world", "4.7")    # 15
token_count("hello, world", "4.8")    # 10

tokens = tokenize("NASA likes tokenizers")  # one readable minimum-cost tiling

Supported families

version is a string such as "4.7". Components are compared as integers, so "4.10" sorts after "4.9". Python reads the float literal 4.10 as 4.1, so passing a non-string version raises TypeError.

requested version family model generation
"3.0" <= version < "4.7" v3, the default Claude 3 through Opus 4.6
"4.7" <= version < "4.8" v4.7 Opus 4.7
version >= "4.8" v4.8 Opus 4.8, Sonnet 5, and Fable 5

v4.8+ uses the v4.7 vocabulary with a six-token frame. Opus 5 alone makes trailing ASCII whitespace free, which ctok intentionally does not model. Opus 5.5 counts two tokens more than Opus 5. This offset is not yet modeled in ctok.

How it works

For one user message, ctok:

  1. normalizes the text, including NFC and family-specific quote folding, then rewrites it with word, case, and byte markers. Run boundaries belong to this rewrite; the tiler only sees its resulting stream. For BMP combining marks, Unicode Alphabetic decides the split: Alphabetic marks stay with the word and non-Alphabetic marks, other than variation selectors, stand outside it;
  2. finds a minimum-cost tiling over the measured vocabulary and UTF-8 byte fallback, then prices the normalized trailing-newline run against the frame and adds the measured message frame.

token_count(text) takes the tiler's cost directly. tokenize(text) renders one matching tiling for inspection; the tiler checks that its internal token list matches that cost. Equal-cost tilings prefer the longest first token, then the longest next token, and so on. ASCII digits retain their three-digit grouping from the right: 12345 splits as 12 | 345. These choices do not change the minimum count. The output notation makes the rewrite visible:

notation meaning
⟨bow⟩, ⟨eow⟩ word boundaries
⟨shift⟩, ⟨caps⟩ case rewrites
⟨0xNN⟩ a byte-fallback token
⟨pad⟩ part of the single-message frame
⟨human⟩, ⟨assistant⟩ legacy turn-marker words in v3 and v4.7

Two content rewrites handle reserved strings, matched case-insensitively on ASCII. The antml: namespace is removed after <, </, or <// when another character follows, except < or >; <// then becomes </. In v3 and v4.7, Human: and Assistant: immediately after two or more newlines use a turn-marker atom followed by one colon. v4.8 treats those words as ordinary text.

Measured accuracy

These results compare ctok with recorded count_tokens responses:

corpus role v3 exact v4.7 exact
Goldfish, 350 languages and 962,054 rows mining 962,053 962,054
MultiPL-E, 22 programming languages held out 22 22
Rosetta Code, 1,741 documents mining 1,741 1,741
Rosetta Code, separate 250 documents held out 250 250
UDHR, 501 languages mining (in-sample since 2026-08-12) 501 501

v4.8+ is omitted because the table gates physical vocabularies. It reuses v4.7's vocabulary.

The stored measurement sets contain no under-counts: 0 of 2,276,929 v3 texts and 0 of 2,328,425 v4.7 texts. This does not guarantee the result for arbitrary input. Goldfish, the main Rosetta sample, and UDHR informed piece selection. MultiPL-E and the separate Rosetta sample did not.

Run the public gates with:

uv run pytest
uv run python tests/gates.py

Vocabulary evidence

The two vocabulary files contain 48,813 v3 pieces and 15,283 v4.7 pieces. Every entry has a fixed membership witness or is one of the structural marker atoms checked by the test suite.

from ctok import pieces, witness

len(pieces("4.7"))                       # 15283
witness("⟨bow⟩the⟨eow⟩", "4.7")
# {'probe': 'the', 'raw': 12, 'kind': 'raw'}

A witness says that one marked piece costs one token in a calibrated probe. It does not prove the encoder rewrite or resolve ties between equal-cost tilings. tests/test_witness.py checks every published witness and requires complete witnessed-or-special coverage.

Download vocabulary files

The release workflow attaches standalone vocabulary assets to each GitHub release:

  • pieces_v3.json and pieces_v4_7.json, identical to the files in the Python package;
  • SHA256SUMS to verify the downloads;
  • LICENSE, covering reuse under MIT.

Other implementations can download these JSON files without installing Python. Pin a release with https://github.com/sanderland/ctok/releases/download/TAG/pieces_v3.json, replacing TAG with the release tag. Older releases may not include these assets.

Each file includes the pieces, membership witnesses, byte-fallback prefixes, and family metadata. The files supply vocabulary data; implementations still need the normalization and tiling rules described above. v4.8+ shares the v4.7 vocabulary but uses different frame and turn-marker rules.

License

MIT

Metadata

Release files for ctok 1.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ctok 1.4.0
File Size Uploaded Provenance Actions
ctok-1.4.0.tar.gz 3.1 MB With provenance Details for ctok-1.4.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ctok 1.4.0
File Interpreter ABI Platform Actions
ctok-1.4.0-py3-none-any.whl Python 3 none any Details for ctok-1.4.0-py3-none-any.whl

Total release size: 3.5 MB

Release files / ctok-1.4.0.tar.gz

Download URL ctok-1.4.0.tar.gz
Size 3.1 MB
Tags Source
SHA-256 checksum
How to use checksums
1b4523d9d109174379666439377a2c86df52a695b16824a60820ace3f4da7a6a
BLAKE2b-256 checksum
How to use checksums
abc22b024df6f240fe81a1e6cef04d356ce4878991850f150885b1f3a22c7272
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.

Transparency log

Release files / ctok-1.4.0-py3-none-any.whl

Download URL ctok-1.4.0-py3-none-any.whl
Size 462.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4da1fab8ef6db9f43a979156fa691a04af694a90b1f037987afdfd985b2499f7
BLAKE2b-256 checksum
How to use checksums
276857bb13f522b531f1d5346108f2a72b02d3b969f6a70b8a9bb0df930b8a68
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.

Transparency log

Security for ctok 1.4.0

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for ctok-1.4.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.

Transparency log

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page