tiktoken-stream 0.1.0

Streaming token counter for partial LLM responses. Accumulates token count across chunks without holding the full text. Pluggable estimator function. Zero deps.
Documentation
  • Coverage
  • 100%
    8 out of 8 items documented1 out of 8 items with examples
  • Size
  • Source code size: 19.97 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 300.38 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 19s Average build duration of successful builds.
  • all releases: 19s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • MukundaKatta/tiktoken-stream
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • MukundaKatta

tiktoken-stream

crates.io docs.rs

Streaming token counter for partial LLM responses. Pluggable estimator, zero deps. Use it for progress bars, soft caps, and live cost displays that update as deltas arrive.

Usage

use tiktoken_stream::TokenStream;

let mut s = TokenStream::new(); // default: 4-chars-per-token ceil
for delta in ["Hello, ", "world!"] {
    let count = s.push(delta);
    println!("~{count} tokens so far");
}

Plug in tiktoken (or anything else) for accuracy:

use tiktoken_stream::TokenStream;

let mut tok = /* your tokenizer */;
let mut s = TokenStream::with_estimator(move |chunk: &str| {
    tok.encode_ordinary(chunk).len() as u64
});

License

MIT or Apache-2.0.