Tokcost — zero-dependency CLI for exact LLM token counts (hand-rolled BPE)

I built a CLI, tokcost, that counts tokens and estimates cost for
LLM text — files, stdin, or a live-wrapped command. Cargo.toml has an
empty [dependencies] section: the BPE encoder, JSON output, arg parsing,
and the live ticker's ANSI are all hand-rolled.

Why zero deps

Mostly because the alternative for exact token counts today is
pip install tiktoken or shelling out to Python, and I wanted something
that's a single static binary with no runtime to bring along. The two
real tiktoken vocabularies (cl100k_base, o200k_base, ~300k merge rules
combined) are embedded at build time from the canonical rank files and
checked in CI against real tiktoken output.

The one design choice worth discussing

The pretokenizer doesn't reproduce tiktoken's split regexes boundary-for-
boundary — it's deliberately coarser in one place. Real o200k_base cuts
a word at every case transition (myVariable → my | Variable); mine
keeps the whole alphabetic run as one piece. That's safe rather than
approximate: BPE vocabularies are trained on text that's pretokenized by
this same regex first, so no merge rule in the shipped rank tables can
ever bridge a boundary the real splitter always draws. Coalescing across
it can't change the merge sequence — there's nothing there to merge.
Splitting finer than the real regex would be unsafe (it can suppress a
real merge); I only ever go coarser. Golden tests check the final token
IDs against real tiktoken output on ~40 cases including camelCase and
case-folded contractions.

Repo (MIT): GitHub - abhiprd2000/tokcost: Exact BPE token counts and cost estimates for LLM text. Zero dependencies, single Rust binary · GitHub , crate : crates.io: Rust Package Registry

cargo install tokcost

Genuinely interested in pushback on the pretokenizer approach above, and
in clippy/idiom nits — this is a smaller codebase than most things I post
about, so it's a good one to get right.

Please consider starring the repo if you find it useful. It means a lot to me.

The coarser case-transition split is the right thing to document up front, but it means your counts drift from tiktoken on exactly the input people care most about, which is source code. myVariable and my_variable are everywhere the moment a prompt contains a diff.

Do you have a number for the drift? Mean and p99 delta over a corpus of real source files would settle whether coarse is fine or whether it needs fixing, and given that exactness is the pitch, that number belongs in the README.

Embedding both vocabs at build time and checking against real tiktoken in CI is a nice touch. That is the part most reimplementations skip.

That is off, and that's my README's fault.

The coarser split doesn't drift the counts. Token IDs come out identical.
Mean, p99 and max delta are all 0.

BPE vocabs are trained on text already pretokenized by that same regex,
so no merge rule can span a boundary the real splitter always draws.
Coalescing across it leaves nothing to merge, so the merge sequence is
unchanged. Coarser is safe; finer isn't, and I never go finer.

myVariable is the case that looks like it should break. Real o200k gives
my + Variable, mine gives one piece, same IDs out.

You're right, and I had the direction of the risk backwards. If the vocab was trained on text pretokenized by that regex, then no merge rule can span a boundary the real splitter always draws, so coalescing across one leaves nothing to merge and the IDs have to come out identical. Coarser being safe while finer is not is the genuinely non-obvious part.

That argument is what belongs in the README, more than the delta number. "Deliberately coarser" reads like a known inaccuracy you are disclosing; "coarser is lossless here, and here is why" reads like the correctness result it actually is. Right now the phrasing undersells it.

One thing I would still want pinned down: does the invariant hold at the leading-space and byte-level boundaries too, or only at case transitions? And does the zero-delta corpus cover non-ASCII, where byte-level fallback makes the chunking harder to reason about?