Skip to content

API Reference

Profile

pub enum Profile {
    Prose,  // ~4 chars per token
    Code,   // ~3 chars per token
    Mixed,  // ~3.5 chars per token
}

Profile::chars_per_token() -> f64

The characters-per-token ratio assumed for this profile.

estimate(text: &str, profile: Profile) -> usize

Estimated token count. Returns 0 for empty or whitespace-only input; any non-empty input estimates at least 1. Whitespace runs are collapsed before counting, and non-ASCII density blends the ratio toward one token per character.

estimate_auto(text: &str) -> usize

estimate with the profile inferred by infer_profile.

infer_profile(text: &str) -> Profile

Infers a profile from the density of code-ish punctuation ({}()<>;=[]|_). Above 8% selects Code; above 3% selects Mixed; otherwise Prose.

fits(text: &str, budget: usize, profile: Profile) -> bool

Whether the estimate is within budget.

remaining(text: &str, budget: usize, profile: Profile) -> usize

Budget left after text, saturating at zero.

estimate_with_margin(text: &str, profile: Profile, margin_pct: u32) -> usize

A conservative upper bound, inflating the estimate by margin_pct. Useful when overflowing a context window costs more than sending less; 20 is a reasonable default for prose.