token-estimate 0.1.0

Estimate LLM token counts from text without loading a tokenizer, with explicit accuracy bounds.
Documentation
  • Coverage
  • 100%
    12 out of 12 items documented1 out of 9 items with examples
  • Size
  • Source code size: 11.99 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 363.91 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 7s Average build duration of successful builds.
  • all releases: 7s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • theluckystrike/token-estimate
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • theluckystrike

token-estimate

Estimate LLM token counts from text without loading a tokenizer.

Real tokenizers need a multi-megabyte vocabulary and a non-trivial dependency tree. That is right when you need an exact count, and wrong when you only need to answer "will this roughly fit in the context window" — the common case in CLI tooling, log processing and pre-flight budget checks.

use token_estimate::{estimate, estimate_auto, fits, Profile};

let n = estimate("The quick brown fox jumps over the lazy dog.", Profile::Prose);
assert!(fits("short input", 100, Profile::Prose));

// Profile inferred from punctuation density
let _ = estimate_auto("if (a == b) { c[d] = e; }");

Accuracy, stated honestly

For ordinary English prose this lands within roughly 10-15% of a BPE tokenizer. It degrades predictably:

Input Behaviour
Code and markup Tokenizes denser than prose; estimate runs low. Use Profile::Code.
Non-Latin scripts Often one token per character; a chars/4 heuristic underestimates badly. Density is detected and adjusted, but treat CJK as a rough floor.
Long whitespace or punctuation runs Compress well; estimate runs high. Whitespace runs are collapsed to compensate.

If you need an exact count, use a real tokenizer. This is for budgeting.

Use estimate_with_margin when overflowing a context window costs more than sending less.

License

MIT