Skip to content

tokens

Fast token estimation for LLM context budgeting.

Uses a character-based heuristic (~4 chars per token for English) that is WASM-safe and requires zero external dependencies. Accuracy is within ±15% of actual BPE tokenizer counts for typical English text.

For precise counting, use a tokenizer library (e.g. tiktoken-rs) at the caller level and feed exact counts into [ContextBudget].

fn estimate_tokens(text: &str) -> u32

Estimate token count for a text string.

Uses the widely-accepted heuristic of ~4 characters per token for English text, with a small overhead for BPE tokenizer framing. Non-ASCII text uses a slightly higher ratio (3 chars/token) to account for multi-byte characters.

fn estimate_message_tokens(role: &str, content: &str) -> u32

Estimate tokens for a single OpenAI-format chat message.

Accounts for role overhead (~4 tokens: role tag, separators, etc.) plus the content tokens.

fn estimate_message_json_tokens(msg: &serde_json::Value) -> u32

Estimate tokens for a JSON-serialized message value (OpenAI format).

Handles role, content, and tool_calls fields. Tool calls add additional overhead for function name and arguments.

fn estimate_messages_tokens(messages: &[serde_json::Value]) -> u32

Estimate total token count for a sequence of OpenAI-format message values.

Includes a ~3 token overhead for the overall messages array framing.

fn estimate_tool_schema_tokens(tool_json: &serde_json::Value) -> u32

Estimate tokens for a tool schema definition (for budget reservation).

fn estimate_tools_tokens(tools_json: &[serde_json::Value]) -> u32

Estimate total tokens for all tool schemas.