LemonAi.Tokens (lemon_ai v0.1.0)

View Source

Fast token estimation helpers.

These functions centralize Lemon's rough 4-units-per-token heuristic. It is useful for thresholds, diagnostics, and fallback estimates, but it is not a tokenizer and will differ from model-specific token counts.

Use estimate_chars/1 when the caller is budgeting user-visible text by Unicode character count. Use estimate_bytes/1 when the existing boundary is payload bytes, serialized data, or transport size.

Summary

Functions

Estimates tokens from a precomputed byte count.

Estimates tokens from byte size using the 4 bytes/token heuristic.

Estimates tokens from a precomputed Unicode character count.

Estimates tokens from Unicode character count using the 4 chars/token heuristic.

Functions

estimate_byte_count(count)

@spec estimate_byte_count(non_neg_integer()) :: non_neg_integer()

Estimates tokens from a precomputed byte count.

estimate_bytes(binary)

@spec estimate_bytes(binary()) :: non_neg_integer()

Estimates tokens from byte size using the 4 bytes/token heuristic.

estimate_char_count(count)

@spec estimate_char_count(non_neg_integer()) :: non_neg_integer()

Estimates tokens from a precomputed Unicode character count.

estimate_chars(text)

@spec estimate_chars(String.t()) :: non_neg_integer()

Estimates tokens from Unicode character count using the 4 chars/token heuristic.